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;
}
Why you ALWAYS wanna use brackets
#1 Banned - Reputation: 356
Posted 14 May 2011 - 08:49 PM
#4 Banned - Reputation: 356
Posted 14 May 2011 - 11:38 PM
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).
#5 Members - Reputation: 1189
Posted 15 May 2011 - 01:53 AM
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.
#9 Members - Reputation: 296
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).
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")
endthen/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 Banned - Reputation: 356
Posted 15 May 2011 - 06:32 AM
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!
#11 Members - Reputation: 682
Posted 15 May 2011 - 07:48 PM
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 Members - Reputation: 3825
Posted 16 May 2011 - 04:36 AM
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 Members - Reputation: 461
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 Moderators - Reputation: 13533
Posted 16 May 2011 - 06:07 AM
Owl shoulda posted in the lounge...Man, I don't even get the joke. All I see is buggy code
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.
#16 Members - Reputation: 5802
Posted 16 May 2011 - 09:13 AM
Braces?
These things have several names.






