Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsLint Expected {

Given the following

for(var i=0; i< data.cats.length; i++) list += buildCategories(data.cats[i]);

jsLint tells me

Expected '{' and instead saw 'list'.

Is there an actual disadvantage to using the shorter notation instead of wrapping it it curly braces?

like image 783
darryn.ten Avatar asked Mar 13 '12 19:03

darryn.ten


3 Answers

It is defensive programming - using curly brackets clearly defines which statements are intended to be associated with the for.

If you don't use curly brackets, at a later point someone might mistakenly add another statement underneath list += buildCategories... expecting it to get executed with the for loop as well.

like image 180
D'Arcy Rittich Avatar answered Oct 06 '22 01:10

D'Arcy Rittich


"Is there an actual disadvantage to using the shorter notation..."

It may be the source of bugs if you're not careful about your coding, but omitting them provides cleaner code IMO, and if you adhere to consistent and well thought programming standards, omitting them won't be an issue.

For example, when I have nested if/else statements that are otherwise able to exclude the braces, I prefer to balance the elses over using braces.

if (condition)
    if (condition2)
        inner_if()
    else ;
else
    outer_if()

That code is still cleaner than this IMO...

if (condition) {
    if (condition2) {
        inner_if();
    }
} else {
    outer_if();
}

If someone things they can add another statement to an if or else, then that's an issue of understanding that needs to be fixed.

So really it's just a question of what standards are to be used. Taking advantage of curly braces is certainly one valid option, but we shouldn't be too dogmatic about it.


If you want a more configurable tool, you could consider jsHint.com instead.

like image 35
2 revsuser1106925 Avatar answered Oct 06 '22 01:10

2 revsuser1106925


JSLint checks for following good code style. Inserting curly braces is always good style because it is obvious where the code belongs to. And that it is shorter is not really an argument since most minifiers take care of that anyway.

like image 30
Daff Avatar answered Oct 06 '22 02:10

Daff