Why you ALWAYS wanna use brackets

Started by
16 comments, last by ChaosEngine 12 years, 11 months ago
I always use brackets for code blocks even if the block contains only one line code.

if(SomeCondition) {
return;
}


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,

if(SomeCondition) {
SomeCondition;
return;
}

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.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

Advertisement
They're a good way of self-documenting code too; "yes, I know this looks weird, but check out my brackets - it's intentional". 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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


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.


Huh?




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();
}



...

http://www.comeaucom...g.com/tryitout/

...




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


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.
Man, I don't even get the joke. All I see is buggy code :blink:
Owl shoulda posted in the lounge...
Enter humorous parable:
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.
Enter serious replies:
> 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.
Braces?
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.


Braces?


These things have several names.
At least we all agree that using brackets for a single line will make your bytes straight.
[size="2"]I like the Walrus best.
Use python
:P
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement