Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Why you ALWAYS wanna use brackets


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 owl   Banned   -  Reputation: 356

Like
0Likes
Like

Posted 14 May 2011 - 08:49 PM


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

I like the Walrus best.

#2 Wooh   Members   -  Reputation: 277

Like
0Likes
Like

Posted 14 May 2011 - 09:15 PM

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.

#3 Telastyn   Members   -  Reputation: 3329

Like
2Likes
Like

Posted 14 May 2011 - 09:25 PM

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.

#4 owl   Banned   -  Reputation: 356

Like
1Likes
Like

Posted 14 May 2011 - 11:38 PM

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
I like the Walrus best.

#5 Buckeye   Members   -  Reputation: 1189

Like
0Likes
Like

Posted 15 May 2011 - 01:53 AM

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. If you have a question, ask it here in the forums to benefit all!

#6 Hodgman   Moderators   -  Reputation: 13533

Like
0Likes
Like

Posted 15 May 2011 - 02:01 AM

Humor is serious business, apparently...

#7 owl   Banned   -  Reputation: 356

Like
0Likes
Like

Posted 15 May 2011 - 02:06 AM

lol
I like the Walrus best.

#8 lonewolff   Members   -  Reputation: 269

Like
0Likes
Like

Posted 15 May 2011 - 05:43 AM

Humor is serious business, apparently...


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

#9 Nanoha   Members   -  Reputation: 296

Like
0Likes
Like

Posted 15 May 2011 - 05:44 AM

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

#10 owl   Banned   -  Reputation: 356

Like
0Likes
Like

Posted 15 May 2011 - 06:32 AM

Oh man! Posted Image

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
I like the Walrus best.

#11 wqking   Members   -  Reputation: 682

Like
0Likes
Like

Posted 15 May 2011 - 07:48 PM

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.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#12 mhagain   Members   -  Reputation: 3825

Like
0Likes
Like

Posted 16 May 2011 - 04:36 AM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#13 _moagstar_   Members   -  Reputation: 461

Like
0Likes
Like

Posted 16 May 2011 - 05:25 AM

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.

#14 Hodgman   Moderators   -  Reputation: 13533

Like
4Likes
Like

Posted 16 May 2011 - 06:07 AM

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.

#15 Nitage   Members   -  Reputation: 806

Like
0Likes
Like

Posted 16 May 2011 - 08:39 AM

Braces?

#16 Álvaro   Members   -  Reputation: 5802

Like
0Likes
Like

Posted 16 May 2011 - 09:13 AM

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.

#17 owl   Banned   -  Reputation: 356

Like
0Likes
Like

Posted 16 May 2011 - 11:31 AM

At least we all agree that using brackets for a single line will make your bytes straight.
I like the Walrus best.

#18 ChaosEngine   Members   -  Reputation: 1002

Like
0Likes
Like

Posted 17 May 2011 - 01:10 AM

Use python
:P
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS