Are Braces Necessary in One-Line Statements in JavaScript

Are braces necessary in one-line statements in JavaScript?

No

But they are recommended. If you ever expand the statement you will need them.

This is perfectly valid

if (cond) 
alert("Condition met!")
else
alert("Condition not met!")

However it is highly recommended that you always use braces because if you (or someone else) ever expands the statement it will be required.

This same practice follows in all C syntax style languages with bracing. C, C++, Java, even PHP all support one line statement without braces. You have to realize that you are only saving two characters and with some people's bracing styles you aren't even saving a line. I prefer a full brace style (like follows) so it tends to be a bit longer. The tradeoff is met very well with the fact you have extremely clear code readability.

if (cond) 
{
alert("Condition met!")
}
else
{
alert("Condition not met!")
}

Do 'if' statements in JavaScript require curly braces?

Yes, it works, but only up to a single line just after an 'if' or 'else' statement. If multiple lines are required to be used then curly braces are necessary.

The following will work

if(foo)
Dance with me;
else
Sing with me;

The following will NOT work the way you want it to work.

if(foo)
Dance with me;
Sing with me;
else
Sing with me;
You don't know anything;

But if the above is corrected as in the below given way, then it works for you:

if(foo){
Dance with me;
Sing with me;
}else{
Sing with me;
You don't know anything;
}

Is it correct to use one line for loop in JavaScript without curly brackets?

Both methods are valid in Javascript.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Statements

All Javascript cares about is what is immediately after the for statement. It can be a statement block (multiple statements in curly brackets) or a single statement.

This is true for nearly every control statement in Javascript.

JavaScript braces on new line or not?

This is a Holy War to which you will never get a usable answer! Just stick with whatever everyone else in the project is using and don't argue!

For what it's worth, I'm a K&Rite. I find the OTBS puts a lot of visual space between an opening structure and the following statement, when those two lines are often strongly related so would be better presented together, without an intervening almost-blank line. I like to keep my blank lines reserved for separating blocks of related statements.

In a coding style which a lot of whitespace anyway, this may be relatively unimportant. But personally I value terseness, so I can keep more of the program on-screen.

I don't buy that having the open-brace on a different column to the close-brace is an issue. It's still easy to see the block shape just from the indents. Unless you're using hanging indents. Don't do that. But that's another Holy War entirely.

Is it a bad practice to use an if-statement without curly braces?

The problem with the first version is that if you go back and add a second statement to the if or else clauses without remembering to add the curly braces, your code will break in unexpected and amusing ways.

Maintainability-wise, it's always smarter to use the second form.

EDIT: Ned points this out in the comments, but it's worth linking to here, too, I think. This is not just some ivory-tower hypothetical bullshit: https://www.imperialviolet.org/2014/02/22/applebug.html

Is the curly braces always should be on the statement line rule so important?

Code style is just an agreement about how to format code and name variables made in:

  • (Sub)community of those who's coding on this language, developing specific libraries and so on.
  • Your very company.

It is hard to say, which to these two factors is more decisive, but it wouldn't be a bold statement to say that in good companies code style is closely related to the code style "in the wild". Partly this is because in good companies code is often contributed to open source and, thus, it is better not to reinvent it.

There are languages with very firm, thorough and recognized by the overwhelming majority style guides. For example, this_is_very_uncommon_to_java.

As for javascript, this is not the case. There is no such thing like abstract, general "javascript code style". The problem of bracing is still heavily discussed topic in javascript community, as well as semicolons. I guess, this is just because any other issues with js-developing are already resolved ;)

If you want advise (and it looks like you want), just find peace with your colleagues. Make any kind of agreement and follow it. This is the most important thing - good relations in team are overweighting any code styling agreements.

shall brackets be used for one line conditional statements?

No, there is no difference, the compiler will strip out non-meaningful braces, line-breaks etc.

The compile time will be marginally different, but so marginally that you have already lost far more time reading this answer than you will get back in compile speed. As compute power increases, this cost goes down yet further, but the cost of reducing readability does not.

In short, do what is readable, it makes no useful difference in any other sense.

What is the downside of using one-line if statements?

Largely this comes down to developer preference, although many reasons are given for using the curly braces on every control block.

First, it makes the control block more readable. Consider:

    if (some_condition)
runSomeFunction();
runSomeOtherFunction();

Since indentation is not respected in most curly brace languages, this will work, but it really reduces the readability (only runSomeFunction() will happen in the control block). Compare to:

if (some_condition) {
runSomeFunction();
}
runSomeOtherFunction();

Second, when you need to add something to the control block (which almost invariably happens more often than not), adding the curly's can be frustrating or easily forgotten leading to issues like the above.

Still, those largely come down to preference and you can always find exceptions (like if (some_condition) runSomeFunction(); which is much more readable than the first example above while still accomplishing the same goal in a much more concise format that retains readability).

Multiple for loop without bracket

If the loop or the if block will have only one statement we can always get rid of the brackets.

So to explain the following code:

for (x[2] = 0; x[2] < dims[2]; ++x[2]) //There will be only one statement which is the next for loop
for (x[1] = 0; x[1] < dims[1]; ++x[1]) //There will be only one statement which is the next for loop
for (x[0] = 0; x[0] < dims[0]; ++x[0], ++n) //There will be only one statement which is the if statement
if (!!volume[n]) { //There will be multiple statements, so we use brackets
for (var d = 0; d < 3; ++d) {
var t = [x[0], x[1], x[2]],
u = [0, 0, 0],
v = [0, 0, 0];
u[(d + 1) % 3] = 1;
v[(d + 2) % 3] = 1;
for (var s = 0; s < 2; ++s) {
t[d] = x[d] + s;
var tmp = u;
u = v;
v = tmp;
var vertex_count = vertices.length;
vertices.push([t[0], t[1], t[2]]);
vertices.push([t[0] + u[0], t[1] + u[1], t[2] + u[2]]);
vertices.push([t[0] + u[0] + v[0], t[1] + u[1] + v[1], t[2] + u[2] + v[2]]);
vertices.push([t[0] + v[0], t[1] + v[1], t[2] + v[2]]);
faces.push([vertex_count, vertex_count + 1, vertex_count + 2, vertex_count + 3, volume[n]]);
}
}
}

That's because each for and if blocks are considered like a one line statement here.



Related Topics



Leave a reply



Submit