Why you ALWAYS wanna use brackets

Started by
16 comments, last by ChaosEngine 12 years, 11 months ago


int call_some_stuff()
{
int wtf = 0;
return wtf;
}

int main(int argc, char **argv)
{
int ha = 0;
int value1 = 1;
int value2 = 2;
if (true)
{
if (value1<value2)
return

call_some_stuff();
ha = 1;
}

if (ha)
std::cout << "This will never get printed" <<std::endl;
return ha;
}

[size="2"]I like the Walrus best.
Advertisement
I also always use brackets. I find the code easier to look at and you don't have to add brackets later if you add more than one statement.
The bug in this code isn't the lack of brackets, it's the lack of semi-colon after the first return in main. If anything this is an argument for language designers to require parens for return calls.
Of course it isn't :) I was looking for another more serious bug so I started adding if's to test things out. To make it worst, the function I was messing, besides being big, it was declared as void, so excepting for the missing semi-colon at a glance it was perfectly ok to have the return statement alone.

I spat this quite quickly but as I was already having other problems I was like WTF? O_o

EDIT: The code I was working on was for interfacing with Lua code (which doesn't require semi-colons). :P
[size="2"]I like the Walrus best.
Do you have a question about your code?

As written, the return statement following if( value1 < value2 ) just returns the value of call_some_stuff, as if you'd written:

if( value1 < value2 ) return call_some_stuff();


Because white-space is ignored in this case, that's actually what you coded.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Humor is serious business, apparently...
lol
[size="2"]I like the Walrus best.

Humor is serious business, apparently...


Man, I don't even get the joke. All I see is buggy code :blink:

EDIT: The code I was working on was for interfacing with Lua code (which doesn't require semi-colons). :P


I'm pretty sure there's somethign in lua that says you can only use return just before an "end"

This works:

if something then
print("hello")
return;
end

This shouldn't work:

if something then
print("hello")
return;
print("hello")
end


then/end are working the same way as brackets so a one liner in lua won't work without the "brackets".

if something then
return
doSomehting();

No idea what that will do but its wrong (since it has no matching end).

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Oh man! rolleyes.gif

I was just pointing out that it should be convenient to keep in mind to use brackets (specially when coding fast) to avoid this kind of buggers. Just like writing if (constant == value) instead of if (value == constant) in case you mistype the = sign.

If a programmer is switching between languages that don't require semi-colon and c++, and he is coding fast this could very likely happen. Remembering to use brackets on if statements might save some time.

I hope I made myself clear now! :D
[size="2"]I like the Walrus best.

This topic is closed to new replies.

Advertisement