Sign in to follow this  
Plasmator

C++ Syntax

Recommended Posts

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!

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
might be worth reading: http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/90cffb5f4759aed1

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this