What's the Purpose of Using Braces (I.E. {}) for a Single-Line If or Loop

What's the purpose of using braces (i.e. {}) for a single-line if or loop?

Let's attempt to also modify i when we increment j:

int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;
i++;

Oh no! Coming from Python, this looks ok, but in fact it isn't, as it's equivalent to:

int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;
i++;

Of course, this is a silly mistake, but one that even an experienced programmer could make.

Another very good reason is pointed out in ta.speot.is's answer.

A third one I can think of is nested if's:

if (cond1)
if (cond2)
doSomething();

Now, assume you now want to doSomethingElse() when cond1 is not met (new feature). So:

if (cond1)
if (cond2)
doSomething();
else
doSomethingElse();

which is obviously wrong, since the else associates with the inner if.


Edit: Since this is getting some attention, I'll clarify my view. The question I was answering is:

What's the benefit of using the 1st version?

Which I have described. There are some benefits. But, IMO, "always" rules don't always apply. So I don't wholly support

Always use a { } block - even for a single line // not OK, why ???

I'm not saying always use a {} block. If it's a simple enough condition & behavior, don't. If you suspect someone might come in later & change your code to add functionality, do.

Mandatory use of braces

I enforce this to a point, with minor exceptions for if statements which evaluate to either return or to continue a loop.

So, this is correct by my standard:

if(true) continue;

As is this

if(true) return;

But the rule is that it is either a return or continue, and it is all on the same line. Otherwise, braces for everything.

The reasoning is both for the sake of having a standard way of doing it, and to avoid the commenting problem you mentioned.

What do {} do in C#

The {} indicates a block of code. If you have more than one statement that you want to be performed if the if is true, then you need to use these.

Example:
Without the {} this block:

if (userValue == "1")
WriteLine ("You won a new car!");
ReadLine ();

Would perform the ReadLine() command whether or not the userValue is equal to 1.

Contrast that with the one with curly braces:

if (userValue == "1")
{
WriteLine ("You won a new car!");
ReadLine ();
}

Now in case the userValue is not equal to 1, both command inside the curly braces are skipped and the execution continues with the next statement after the } sign.

Would it be bad form to put braces on the same line as the statement for single line if statements?

When I come across a one-line if statement, I usually skip the curlys and keep everything on the same line:

if (something == true) DoSomething();

It's quick, easy, and saves space.

JavaScript formatting: must braces be on the same line as the if/function/etc keyword?

Yes, it matters in certain corner cases.

And the problem isn't with "browsers incorrectly interpreting it". The dodgy behaviour is correct according to the ECMAScript specifications. A JavaScript implementation that didn't exhibit this behaviour would not be spec-compliant.

An example. This function is broken:

function returnAnObject {
return
{
foo: 'test'
};
}

It's supposed to return an object, but actually returns nothing. JavaScript interprets it like so:

function returnAnObject {
return;
{
foo: 'test'
};
}

Can Someone Explain This Snippet (Why Are These Braces Here)?

Braces like that indicate that the code inside the braces is now in a different scope. If you tried to access y outside of the braces, you would receive an error.

Curly Bracket in a solidity function

Curly braces are for the scoping rules.

They also allocate a new stack frame. Because the small stack is a pain for developers in EVM. Scoping is needed with deep call structures to avoid stack too deep error.



Related Topics



Leave a reply



Submit