orCode:if (var == something) { cout << "something else"; }
Code:if (var == something) cout << "something else";
why is someone using without brackets {}? Is there a difference on those 2?
Would be glad if someone could tell me
Dag
orCode:if (var == something) { cout << "something else"; }
Code:if (var == something) cout << "something else";
why is someone using without brackets {}? Is there a difference on those 2?
Would be glad if someone could tell me
Dag
Try
Code:if (var == something) cout << "this"; cout << "that";
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
if statements don’t need braces when you only have one statement after it. Some chose to include it anyway for one reason or another, formatting etc. At the end of the day I don’t believe there is much difference.
</spoon feed>
Meg: Everybody! Guess what I am?
Stewie: Hm, the end result of a drunken back-seat grope-fest and a broken prophylactic?
At the end of the day, some other programmer looked at the place where you omitted the braces and has added another statement, forgetting to add braces and introducing a bug ...
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
The compiler I use has no problem compiling and executing this
Code:if (var == something) cout << "this"; cout << "that";
Try passing !something through that and see what happens.Originally Posted by Dag_
Ok got it.
thanks
and the next day when his superior is still getting the "this compiler sucks" excuse, he decides to take one look at the code and the n00b is subsequently "let go".Originally Posted by CornedBee
if missing curly braces on an if statement is enough to trip you up, you really need to get out of the field...
basically, I leave curly braces off one (two) liners for readability and to save space... for example, I prefertoCode:if(this) dothis();but I really can't standCode:if(this) { dothis(); }IMO, I see the second example as very n00bish...Code:if(this) dothis();
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
major_small why are you so rude?
I was just wondering why someone was using this over using braces.
Also I'm new to c++ programming - so what? new people is not allowed to ask questions in here? Even if I'm a "n00b", no one asked for your opinion whether I should "stay in the field" or not.
This is my second language and I'm used to indentation over braces. Dont come with this ........
if you think that was rude, then how about this: maybe you should learn the flow of english before attempting to learn the flow of any languageOriginally Posted by Dag_
first, I wasn't talking about you at all. I was talking about CornedBee's example (which was blatantly hinted at by my quote). Let me spell it out for you - in CornedBee's example, he was talking about how you should use braces so another person doesnt' introduce a bug in your program. I simply stated that if somebody couldn't find a bug like that in a program, they have no business doing it as their job.
now to be even more rude: I think you're a stupid n00b that needs to pay more attention.
</rant>
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
I always do it like this...
... hint, I don't regard myself as a newbie.Code:if(this) { dothis(); }
All 3 of the last professional software houses I have worked in have stated in their installations standards that "though shalt" always use a compound statement even if one is not rquired. Failure to do so would cause the code to fail the QA inspection, and you do it again, and probably have the cost of the second QA inspection charged against your budget.
m_s:
No need to be nasty.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
I'd like to second Adrian. Among many other rules and regulations, our coding standard at work says that curly braces are a requirement. Failure to comply with the coding standards means no bonus, even if the project was on time and otherwise working.
There is no harm in setting the braces and it costs nothing. If it saves one tired programmer from making one tiny mistake, it has paid off.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
I do
but that's essentially the same as adrian and nvoigt said.Code:if(this) { that(); }
Actually, I pretty much follow Linus Torvald's coding standards, with some slight alterations.
And yes, such bugs can be damn hard to find. Mis-indentation can happen easily, and by the time you run the code beautifier over it, you probably don't look back at this particular section.
Of course, that's provided someone actually goes looking for the bug, because this particular if-branch just might be one that's taken in 99% of the cases, so it takes weeks to even notice that there's a bug. By that time, hundreds of other little changes have been made, and finding the one responsible is not as easy as you make it seem, major_small.
It costs you practically nothing. Five keystrokes. (Six if you have the opening brace alone.) And for that you have one potential problem less. Don't you think it's worth it?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law