简体   繁体   中英

Why is there a need for javascript coding conventions?

Suppose I have to write a javascript function:

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 () . Or, where there is a function name I don't put a space between the name and the parentheses: 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. 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:

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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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