为什么许多程序员将逗号移到下一行? [英] Why do lots of programmers move commas to the next line?

查看:109
本文介绍了为什么许多程序员将逗号移到下一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我,下面这种风格的神圣力量是什么:

Tell me please, what is the sacred power of the style below:

var javascript = new Language(
  'Brendan Eich'
, new Date(1995, 0, 1)
, ['C', 'Java', 'Scheme']
);

为什么许多程序员都使用这种风格?它有什么好处?例如,

Why do lots of programmers use that style? What benefits does it have? For example,

var javascript = new Language(
  'Brendan Eich',
  new Date(1995, 0, 1),
  ['C', 'Java', 'Scheme']
);

我比以前更喜欢。谢谢。

I like much more than previous. Thanks.

推荐答案

如果您在最后一行末尾有一个额外的逗号,它将会起作用在某些浏览器但不是在所有浏览器中。使错误比开头的额外逗号更难检测(在所有浏览器上都失败)。大多数开发人员更喜欢立即看到错误(因此他们可以修复它),而不是冒着生产问题而无意中不支持某些浏览器。特别是如果解决方案就像删除逗号一样简单。

If you have an extra comma in the end of the last line it will work in some browsers but not in all browsers. Making the error harder to detect than a extra comma at the beginning (which fails on all browsers). And most developers prefer to see the error right away (so they can fix it), instead of risking a production issue for inadvertently not supporting some browsers. Especially if the solution is as easy as removing a comma.

另外,在行的开头加上逗号,可以更简单地添加一行结束,您必须仅触摸该行(您之前不需要在行中添加逗号)。如果您使用版本控制(例如diff,annotate,bisect),这一点很重要。有人可以争辩说,在数组或对象的开头添加一行将需要触摸2行的相同额外工作(如果你在开头使用逗号),但根据我的经验,在开头插入一行不太可能在最后插入一行。

Plus, having the comma at the beginning of the line, make it simpler to add a line at the end and you will have to touch only that line (you will not need to add the comma in the line before). Which is important if you are using version control (e.g. diff, annotate, bisect). Someone can argue that adding a line at beginning of the array or object will need the same extra work of touching 2 lines (if you use commas at the beginning), but in my experience, inserting a line at the beginning is much less likely that inserting a line at the end.

这篇关于为什么许多程序员将逗号移到下一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆