Sign in to follow this  
owl

Why you ALWAYS wanna use brackets

Recommended Posts


[code]
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;
}
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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:
[code]
if( value1 < value2 ) return call_some_stuff();
[/code]

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

Share this post


Link to post
Share on other sites
[quote name='owl' timestamp='1305437900' post='4810965']
EDIT: The code I was working on was for interfacing with Lua code (which doesn't require semi-colons). :P
[/quote]

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

This works:

[code]if something then
print("hello")
return;
end

This shouldn't work:

if something then
print("hello")
return;
print("hello")
end[/code]

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).

Share this post


Link to post
Share on other sites
Oh man! [img]http://public.gamedev.net/public/style_emoticons/default/rolleyes.gif[/img]

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 [b]if (constant == value)[/b] instead of [b]if (value == constant)[/b] 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

Share this post


Link to post
Share on other sites
I always use brackets for code blocks even if the block contains only one line code.
[code]
if(SomeCondition) {
return;
}
[/code]

This,
1, Less error prone.

2, Easy to add debug code.

For example, if SomeCondition is a function call or a complex expression, I want to see why it return true (because it should return false in normal process), then I can add one line before return temporarily,
[code]
if(SomeCondition) {
SomeCondition;
return;
}
[/code]
Then I can set breakpoint there to repeat the condition to see what' s wrong.

3, AFAIR, sometime a return without brackets can't be breakpointed on some compiler and debuggers? Can't remember clearly though.

Share this post


Link to post
Share on other sites
They're a good way of self-documenting code too; "yes, I know this looks weird, but check out my brackets - it's [i]intentional[/i]". Likewise with parentheses even when operator precedence rules apply - you're hinting to the reader that this is the specific order you want things in.

Share this post


Link to post
Share on other sites
[quote name='owl' timestamp='1305437900' post='4810965']
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.
[/quote]

Huh?


[code]

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

void do_stuff()
{
int value1 = 1;
int value2 = 2;

if (value1 < value2)
return

call_some_stuff();
}
[/code]


[size="2"]...[/size]

[url="http://www.comeaucomputing.com/tryitout/"]http://www.comeaucom...g.com/tryitout/[/url]

[size="2"]...[/size]
[size="2"]
[/size]
[code]

Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 14: error: return value type does not match the function type
call_some_stuff();
^

1 error detected in the compilation of "ComeauTest.c".
[/code]

The compiler would pick up that error for you. As examples go for why to use braces with if statements, I have to say this one is pretty lame.

Share this post


Link to post
Share on other sites
[quote name='lonewolff' timestamp='1305459823' post='4811034']Man, I don't even get the joke. All I see is buggy code :blink:[/quote]Owl shoulda posted in the lounge...
[b]Enter humorous [url="http://en.wikipedia.org/wiki/Parable"]parable[/url]:[/b]
Last night I got up during the night to get some water, but I didn't turn the light on and I stubbed my toe really bad and spilt water everywhere.
[b]Enter [url="http://www.ndnguyen.com/wp-content/uploads/2008/11/serious_business.jpg"]serious[/url] replies:[/b]
> This isn't about not turning the light on, it's about not preparing a glass of water on your bedside table. I'm a huge advocate of thirst awareness.
>> Do you need information about light-switches? Generally the down position is on, whereas the up position is off. You may have been confused about that.
>>> I always pre-arrange my furniture to ensure a clean walking path between the bedroom and kitchen, thus avoiding any toe-stubbing events. I also love linoleum BTW. You should get some linoleum.

Share this post


Link to post
Share on other sites
I don't get the humor or the point of this thread. My compiler and even my antiquated editor (emacs) would tell me if I made that mistake. My personal rule is to use curly brackets whenever they would enclose several lines of code, even if they are a single statement.

[quote name='Nitage' timestamp='1305556797' post='4811442']
Braces?
[/quote]

These things have [url="http://en.wikipedia.org/wiki/Bracket#Curly_brackets_.7B_.7D"]several names[/url].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this