The Most Popular JavaScript Coding Standards
Category Programming Techniques
What are the best JavaScript coding standards? This may be a matter of personal taste. So, let's consider a different question: What coding standards are the most popular?
sideeffect.kr has analyzed open-source code hosted on GitHub and has come up with some interesting results. Let's take a look.
Comma at the End of the Line vs. Comma at the Beginning of the Line
Comma at the end of the line:
var foo = 1,
bar = 2,
baz = 3;
var obj = {
foo: 1,
bar: 2,
baz: 3
};
Comma at the beginning of the line:
var foo = 1
, bar = 2
, baz = 3;
var obj = {
foo: 1
, bar: 2
, baz: 3
};
End of line, 92.345%; Beginning of line, 7.655%. (Based on a count of 1,100,251 commits.)
Spaces and Tabs
These days, everyone prefers to use spaces. Using spaces for indentation ensures that different developers and different editor settings will see the same result.
Spaces, 81.1%; Tabs, 18.9%. (Based on a count of 2,019,550 commits.)
Whether to Add a Space After the Function Name
No space
function foo() {
return "bar";
}
With space
function foo () {
return "bar";
}
No space, 67.424%; With space, 32.576%. (Based on a count of 1,212,488 commits.)
Whether to Have a Space Between Parameters and Parentheses
No space
function fn(arg1, arg2) {
//or
if (true) {
With space
function fn( arg1, arg2 ) {
// ...
}
if ( true ) {
// ...
}
No space, 94.31%; With space, 5.69%. (Based on a count of 1,514,971 commits.)
Whether to Have Spaces Around Colons in Object Literals
Space after colon
{
foo: 1,
bar: 2,
baz: 3
}
No space after colon
{
foo:1,
bar:2,
baz:3
}
Space before and after colon
{
foo : 1,
bar : 2,
baz : 3
}
Space after, 62.955%; No space, 22.891%; Space before and after, 14.154%. (Based on a count of 1,300,035 commits.)
I personally think that no space is too crowded and not conducive to quickly distinguishing between keys and values. If there are spaces before and after the colon, it may be necessary to align the colons to look aesthetically pleasing. From the statistical data, most programmers are lazy about aligning colons (or is it that most programmers' IDEs or editors are not smart enough?)
Conditional Statements
With space
if (true) {
//...
}
while (true) {
//...
}
switch (v) {
//...
}
No space
if(true) {
//...
}
while(true) {
//...
}
switch(v) {
//...
}
With space, 78.276%; No space, 21.724%. (Based on a count of 1,163,316 commits.)
Single Quotes, Double Quotes
Single quotes, 56.791%; Double quotes, 43.209%. (Based on a count of 1,705,910 commits.)
Summary
So, the most popular coding standards are:
Comma at the end of the line
Space indentation
No space after the function name
No space between function parameters and parentheses
Add a space after the colon in object literals, not before
Add a space after conditional statement keywords
Popularity does not necessarily mean quality (like the flu), but from a communication perspective, writing code in a popular style can make your code look more familiar to most people.
** Click to Share Notes
-
-
-