简体   繁体   English

为什么需要 javascript 编码约定?

[英]Why is there a need for javascript coding conventions?

Suppose I have to write a javascript function:假设我必须编写一个 javascript 函数:

function(){
    var a=1;
    var sum=1;
    for(var i=0;i<6;i++){
        sum=sum+a+1;
    }
    console.log(sum);
}

Someone recommended me to write this function like this:有人建议我这样写这个函数:

function () {

   var a = 1;
   var sum = 1;
   for (var i = 0; i < 6; i++) {
      var sum = sum + a +1;
   }
   console.log(sum);

}

With more blank space, I know this rule, but I don't how it works, or what can I benefit from it?有更多的空白,我知道这个规则,但我不知道它是如何工作的,或者我可以从中受益什么?

The benefit of coding style is enhanced readability.编码风格的好处是增强了可读性。 It does not really matter what style you decide to stick to, as long as you DO stick with a uniform style, and can agree with your coworkers on its readability, which is not always easy.你决定坚持哪种风格并不重要,只要你坚持统一的风格,并且能在可读性上与你的同事达成一致,这并不总是那么容易。

It is a matter of opinion what good style is, but in a general sense picking some style and consistently following it throughout your code makes it easier to read (both for other people and for you when you come back to it later).什么是好的风格是一个见仁见智的问题,但一般来说,选择一些风格并在整个代码中始终如一地遵循它会使阅读更容易(对其他人和您以后再看时)。

In my experience most people find code easier to read with the extra spaces as shown in your second example.根据我的经验,大多数人发现使用额外空格更容易阅读代码,如第二个示例所示。

I don't like putting a space between function and () .我不喜欢在function()之间放置空格。 Or, where there is a function name I don't put a space between the name and the parentheses: function someName() .或者,如果有函数名称,我不会在名称和括号之间添加空格: function someName()

Note also that with modern code editors that have syntax highlighting (like Stack Overflow does) it is much easier than it used to be to read code that doesn't have spaces.另请注意,使用具有语法突出显示功能的现代代码编辑器(如 Stack Overflow 那样),阅读没有空格的代码比过去容易得多。 Compare the following two:比较以下两个:

for(var i=0;i<6;i++)

for(var i=0;i<6;i++)

Reading and editing the latter, all in black and white, really annoys me, but I don't mind the coloured version anywhere near as much.阅读和编辑后者,全是黑白的,真的让我烦恼,但我不介意任何接近的彩色版本。 I still prefer it with the extra spaces though.不过,我仍然更喜欢它有额外的空间。

I'd make some other changes in your function:我会对你的功能做一些其他的改变:

function() {
    var a = 1,
        sum = 1,
        i;

    for(i = 0; i < 6; i++){
       sum += a + 1;
    }
    console.log(sum);
}

These coding conventions are for humans, they increase readability.这些编码约定适用于人类,它们提高了可读性。 Suppose I have written an expression like this:假设我写了一个这样的表达式:

x=(a*b/2)+m-n+c*(d/e);

It looks clumsy and difficult to read.它看起来笨拙且难以阅读。 It would have been easier to understand if we had used spaces around operators like this:如果我们在运算符周围使用空格会更容易理解:

x = (a * b / 2) + m - n + c * (d / e);

Again using blank line increases readability by denoting sections.再次使用空行通过表示部分来提高可读性。 For example:例如:

function foo() {
    var a;
    var b;
    // a blank line here to specify the end of variable declarations
    if (some_cond) {

    } else if (another_cond) {

    }
    // another blank line to specify end of some logic
    //more codes here;
}

If you do not follow these guidelines and all team members do not agree in some convention then it will be very difficult to maintain a big project for long time.如果你不遵循这些指导方针,并且所有团队成员在某些约定上都不同意,那么长时间维护一个大项目将是非常困难的。

Finally note that, the conventions are not for compilers, they are for humans.最后请注意,这些约定不是针对编译器的,而是针对人类的。 That's why it is called coding guidelines, not language syntax.这就是为什么它被称为编码指南,而不是语言语法。

可能你应该阅读更多关于 javascript 闭包的内容,你可以遵循“Google Javascript Style Guide”

Following some uniform style guidelines when coding makes code easier to read and helps you writing beautiful code, and others understanding (and loving!) your code.在编码时遵循一些统一的风格指南可以使代码更易于阅读并帮助您编写漂亮的代码,并帮助其他人理解(并喜欢!)您的代码。

For sure there are loads of resources on the net (just by googling for a while you get some javascript guides or guidelines), but this one is quite easy, simple and complete:可以肯定的是,网络上有大量资源(只需通过谷歌搜索一段时间,您就会获得一些 javascript 指南或指南),但这个非常简单、简单和完整:

http://javascript.crockford.com/code.html http://javascript.crockford.com/code.html

It's not a rule.这不是规则。 It's just coding convention style.这只是编码约定风格。 You don't need to follow if you don't want.如果你不想,你不需要跟随。 But this style can make your code more readable, easier to maintain, and cleaner.但是这种风格可以使您的代码更具可读性、更易于维护和更干净。 To me, I prefer to have space rather than narrow letters.对我来说,我更喜欢有空格而不是窄字母。 Again, it's not a rule.再说一遍,这不是规则。

Coding style is always very personal;编码风格总是非常个人化; one person likes condensed code so that they can see as much as possible on one screen, another needs the opening and closing braces on a separate line, etc.一个人喜欢压缩代码,以便他们可以在一个屏幕上看到尽可能多的内容,另一个人需要在单独的一行中打开和关闭大括号,等等。

When only coding for yourself, you should choose whatever is best for you.当只为自己编码时,你应该选择最适合你的。 But when you start working in teams and others have to maintain your code and visa versa, it becomes important to agree on one coding style ... and this can be hard.但是,当您开始在团队中工作并且其他人必须维护您的代码时,反之亦然,就一种编码风格达成一致变得很重要……这可能很难。

I've sat in coding style discussions and they're very uncomfortable, because you're giving up some of your personal preferences albeit for the greater good.我参加了编码风格的讨论,他们非常不舒服,因为你放弃了一些个人偏好,尽管是为了更大的利益。 After a brief moment of discomfort you will get used to it ;-)在短暂的不适之后,您会习惯的 ;-)

  1. The second version isn't equivalent to the first, as it declares an inner 'sum' variable, unless Javascript doesn't do what it says on the tin with that.第二个版本与第一个版本不同,因为它声明了一个内部的“sum”变量,除非 Javascript 没有按照它在锡上所说的那样做。

  2. The extra blank lines don't contribute anything much IMHO, but I probably wouldn't die in a ditch about them.恕我直言,额外的空白行没有任何贡献,但我可能不会因为它们而死。 However an equally valid concern is download speed, which is made worse by the suggestion.然而,同样有效的问题是下载速度,该建议使下载速度变得更糟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM