C++ Syntax

Started by
8 comments, last by xiuhcoatl 18 years ago
Hey, as you all know, when you're doing a while / for-loop or comparison, if your "action" fits in a single statement then you don't need the braces around that block. For example, if(...) a = b; However, when you need to do more then one thing, you add {}'s: if(...) { a = b; b = c; } I've noticed that you can also do this to multiple statements: if(...) a = b, b = c; My questions are: (I'm referring to the last example with the use of a comma) a)will this syntax work on any modern C++ compiler? b)is this valid? Thanks in advance guys!
Advertisement
perfectly valid
Thank you very much for such a quick response! :D

One more qustion, is it recommended style-wise to use this method?

Thanks again.
Quote:Original post by Plasmator
Thank you very much for such a quick response! :D

One more qustion, is it recommended style-wise to use this method?

Thanks again.


No, it's not recommended. Why would you prefer to seperate statements by commas instead of just putting it in a block?
I would say that it is most definitely not recommended. Not everyone is familiar with the details of the comma operator (or that there even is a comma operator), and could thus make the code more confusing or misleading. Also, I'm too lazy to think hard about it, but I wouldn't be surprised if there could be some interesting and hard-to-track down bugs caused by using a comma. It isn't identical to using multiple statements inside braces. It's just that it can be used to do something similar in this case.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Quote:Original post by Plasmator
Thank you very much for such a quick response! :D

One more qustion, is it recommended style-wise to use this method?

Thanks again.


No, Your code will be unreadable to anyone who doesn't follow the same convention. Not all c/c++ programmers are aware of exactly how the comma operator works.

I personally use braces in if blocks with a simgle statement. It's very clear to me what's going on then, and I can add or remove such statements with liberty without needing to change the braces.
Quote:Original post by CTar
Quote:Original post by Plasmator
Thank you very much for such a quick response! :D

One more qustion, is it recommended style-wise to use this method?

Thanks again.


No, it's not recommended. Why would you prefer to seperate statements by commas instead of just putting it in a block?

I guess it's just my taste... :(
I just wanted to know if alot of people used this syntax! - Doesn't seem so...

Thanks for your replies :)
might be worth reading: http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/90cffb5f4759aed1
Just to be safe with ALL compilers, I put brackets around even 1 line statements, not too annoying to code, and helpful when reading.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
I agree with the above posts - In one case, auditing some code for a client, I saw something similar to this:

if( a<b );    modifyValues();


where the modifier of the code tried to refactor correctly but accidently tossed the ';' at the end of the if statement. Going through the change management system before this change was submitted was a complicated series of tests that had commas, braces/no braces, etc.. which was difficult to read. So you could almost see that the maintainer just C&P'd the block into a function and left what remains above accidently. This incidently led to an integer overflow which created a memory allocation that could, in a very difficult way, be leveraged into an exploitable buffer overflow.


Ultimately I wouldn't say dont ever do it, C/C++ can be very terse and at times those language features are very useful. Just do what makes your code the most readable and conveys clearly what you are trying to "speak" in C/C++.


Hope that helps..

#dth-0
"C and C++ programmers seem to think that the shortest distance between two points is the great circle route on a spherical distortion of Euclidean space."Stephen Dewhurst

This topic is closed to new replies.

Advertisement