Jump to content
  • Advertisement
Sign in to follow this  
owl

Why you ALWAYS wanna use brackets

This topic is 2596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



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

Share this post


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

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:

if( value1 < value2 ) return call_some_stuff();


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

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!