Today's most popular JavaScript code specification

  • 2020-03-30 02:18:14
  • OfStack

What is the best JavaScript code programming specification? This can be a one-track question. So, on the other hand, what code specification is the most popular?

Sideeffect.kr came up with some interesting results by analyzing the open source code hosted on GitHub. Let's have a look.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201403/20140308102748.png ">

Comma at the end of a line versus comma at the beginning of a line
End quote:


var foo = 1, 
    bar = 2, 
    baz = 3; 

var obj = { 
    foo: 1, 
    bar: 2, 
    baz: 3 
}; 

Opening quote:

var foo = 1 
  , bar = 2 
  , baz = 3; 

var obj = { 
    foo: 1 
  , bar: 2 
  , baz: 3 
};

End of line, 92.345%; First line, 7.655%. (based on 1,100,251 submissions.)

Spaces and tabs
People like to use Spaces these days. Using space indentation ensures that you will see the same results for different developers and different editor Settings.

Space, 81.1 %; Tab, 18.9%. (based on 2,019,550 submissions.)

Whether to add a space after the function
There is no space


function foo() { 
  return "bar"; 
} 

The space

function foo () { 
  return "bar"; 
} 

No Spaces, 67.424%; There is space, 32.576 %. (based on statistics of 1,212,488 submissions.)

Whether there is a space between the argument and the parenthesis
There is no space


function fn(arg1, arg2) { 
//or 
if (true) { 

The space

function fn( arg1, arg2 ) { 
  // ... 
} 

if ( true ) { 
  // ... 
} 

No Spaces, 94.31 %; With Spaces, 5.69 %. (based on 1,514,971 submissions.)

Whether there is a space around the colon in the object literal
Space after colon


{ 
  foo: 1, 
  bar: 2, 
  baz: 3 
} 

There is no space after the colon

{ 
  foo:1, 
  bar:2, 
  baz:3 
} 

There are Spaces before and after the colon

{ 
  foo : 1, 
  bar : 2, 
  baz : 3 
} 

After space, 62.955 %; No Spaces, 22.891 %; Before and after Spaces, 14.154 %. (based on statistics of 1,300,035 submissions.)

Personally, I think no space is too crowded, which is not conducive to quickly distinguish key and value. If you need to line up the colon before and after the space, it will look nice. Statistically speaking, most programmers are too lazy to line up the colon (or are most programmers' ides or editors not smart enough?).

Conditional statements
The space


if (true) { 
  //... 
} 

while (true) { 
  //... 
} 

switch (v) { 
  //... 
} 

There is no space

if(true) { 
  //... 
} 

while(true) { 
  //... 
} 

switch(v) { 
  //... 
} 

With Spaces, 78.276%; No Spaces, 21.724 %. (based on 1,163,316 submissions.)

Single and double quotation marks
Single quote, 56.791 %; Double quotation marks, 43.209 %. (based on 1,705,910 submissions.)

conclusion
So the most popular code specification is:

The & # 8226; The comma at the end of the line
The & # 8226; Space indentation
The & # 8226; There is no space after the function name
The & # 8226; There is no space between function arguments and parentheses
The & # 8226; The colon of an object literal is followed by a space, not by a colon
The & # 8226; Conditional statement keyword followed by a space

What's popular isn't necessarily good (like influenza), but from a communications point of view, coding in a popular style can make your code look more comfortable to most people.


Related articles: