Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it true that I should use K&R styling when writing javascript? [closed]

I didn't realise it until recently, but I use the Allman style when writing javascript code.

According to http://encosia.com/in-javascript-curly-brace-placement-matters-an-example/ I should be using K&R style. This is because in some situations placement of the curly bracket on the next line can cause problems. The problem for me is that I've grown rather fond of the Allman style and would rather not change.

So, my question(s)

  • Is it true that the allman style can cause errors due to the placement of curly brackets in javascript? How about jquery?

  • Is there any trick that would enable me to use Allman style without problems due to curly brackets?

  • Are there any other problems associated with using these styles?

like image 965
TryHarder Avatar asked Sep 02 '12 06:09

TryHarder


People also ask

Why k-means is not recommended?

Logically speaking, the drawbacks of K-means are : needs linear separability of the clusters. need to specify the number of clusters. Algorithmics : Loyds procedure does not converge to the true global maximum even with a good initialization when there are many points or dimensions.

When Should k-means be used?

K-means clustering is a type of unsupervised learning, which is used when you have unlabeled data (i.e., data without defined categories or groups). The goal of this algorithm is to find groups in the data, with the number of groups represented by the variable K.

Why is k-means so popular?

K-means has been around since the 1970s and fares better than other clustering algorithms like density-based, expectation-maximisation. It is one of the most robust methods, especially for image segmentation and image annotation projects. According to some users, K-means is very simple and easy to implement.

Where is k-means used in real life?

kmeans algorithm is very popular and used in a variety of applications such as market segmentation, document clustering, image segmentation and image compression, etc. The goal usually when we undergo a cluster analysis is either: Get a meaningful intuition of the structure of the data we're dealing with.


2 Answers

Is it true that the allman style can cause errors due to the placement of curly brackets in javascript?

Yes, it is. As your link explains, return statements followed by a newline will confuse the parser as it tries to insert a semicolon. This behavior is not limited to returning object literals, though. The following function:

function foo()
{
    return
        "bar";
}

Will also return undefined.

How about jquery?

The situation remains exactly the same whether or not jQuery is included.

Is there any trick that would enable me to use Allman style without problems due to curly brackets?

Yes, do not use Allman style with object literals. Consider you want to assign an object literal to a variable. Are you really going to write:

var foo =
{
    bar: "quux"
};

Or will you go for:

var foo = {
    bar: "quux"
};

IMHO the second snippet is more readable than the first. You can continue using Allman style with the braces inside for, if, while, function, etc., but make an exception for object literals.

Are there any other problems associated with using these styles?

return is quite a special case, because it is valid with or without an argument. When confronted with a lone return statement on one line, the parser cannot tell if it should add a semicolon to make the single-line statement valid, or continue to the next lines and hope for the best.

For this reason, I think this behavior only becomes a problem with return and maybe throw, not other statements.

like image 171
Frédéric Hamidi Avatar answered Oct 27 '22 04:10

Frédéric Hamidi


Keep in mind ECMAScript specification regarding automatic semicolon insertion:

Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

I laugh at the suggestion above that it is "convenient"...

While you technically could could continue using Allman formatting in cases not covered above, I would preach consistency. Personally I'm a fan of Google JS styleguide - sections relevant to your question are the code formatting and semicolons ones. The examples listed there are worth checking out.

like image 45
Oleg Avatar answered Oct 27 '22 04:10

Oleg