Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Side effects in conditional expressions

Reply
Thread Tools

Side effects in conditional expressions

 
 
August Karlstrom
Guest
Posts: n/a
 
      10-01-2010
Among C programmers it is not uncommon to use expressions with
side-effects such as `y++' in the statement `x = y++;'. If we look at a
statement like

(x == 0)? x++: y *= 2;

Would this be considered bad coding style? Motivate your answer.


/August

--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
 
Reply With Quote
 
 
 
 
Chïna Blüe Öyster Cult
Guest
Posts: n/a
 
      10-01-2010
In article <i85kc7$mcj$(E-Mail Removed)>,
August Karlstrom <(E-Mail Removed)> wrote:

> (x == 0)? x++: y *= 2;
>
> Would this be considered bad coding style? Motivate your answer.


Can you guarentee what will be assigned to what if x==0 or x==1? If not, you
have been answered.

Also x++ is a rvalue, not an lvalue, so even if ?: has been extended to lvalues,
this won't work.

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. Blessed be.
 
Reply With Quote
 
 
 
 
August Karlstrom
Guest
Posts: n/a
 
      10-01-2010
On 2010-10-02 00:08, Chïna Blüe Öyster Cult wrote:
> In article<i85kc7$mcj$(E-Mail Removed)>,
> August Karlstrom<(E-Mail Removed)> wrote:
>
>> (x == 0)? x++: y *= 2;
>>
>> Would this be considered bad coding style? Motivate your answer.

>
> Can you guarentee what will be assigned to what if x==0 or x==1? If not, you
> have been answered.
>
> Also x++ is a rvalue, not an lvalue, so even if ?: has been extended to lvalues,
> this won't work.


OK, I apparently need parentheses around `y *= 2' so it should read

(x == 0)? x++: (y *= 2);


/August

--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      10-01-2010
On 2010-10-01 23:38, August Karlstrom wrote:
> (x == 0)? x++: y *= 2;


It should be

(x == 0)? x++: (y *= 2);


/August

--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
 
Reply With Quote
 
lawrence.jones@siemens.com
Guest
Posts: n/a
 
      10-02-2010
August Karlstrom <(E-Mail Removed)> wrote:
>
> It should be
>
> (x == 0)? x++: (y *= 2);


No, it should be

if (x == 0) x++;
else y *= 2;

It is considered very poor style to use ?: and ignore the result -- an
if statment is clearer and avoids the precedence issues.
--
Larry Jones

Please tell me I'm adopted. -- Calvin
 
Reply With Quote
 
Mr. Buffoon
Guest
Posts: n/a
 
      10-02-2010
Am 01.10.2010 23:38, schrieb August Karlstrom:
> Among C programmers it is not uncommon to use expressions
> with side-effects such as `y++' in the statement `x = y++;'.
> If we look at a statement like
> (x == 0)? x++: y *= 2;
> Would this be considered bad coding style? Motivate your answer.


Change to a compiler which automatically fixes your buggy code!
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      10-02-2010
On 2010-10-02 02:21, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> August Karlstrom<(E-Mail Removed)> wrote:
>>
>> It should be
>>
>> (x == 0)? x++: (y *= 2);

>
> No, it should be
>
> if (x == 0) x++;
> else y *= 2;
>
> It is considered very poor style to use ?: and ignore the result -- an
> if statment is clearer and avoids the precedence issues.


OK, let's change it to

z = (x == 0)? x++: (y *= 2);

then.

Note that I would never write this kind of code myself. I wouldn't even
write `x = y++;' but `x = y; y++;'. What I'm trying to find out is
whether there is some common acceptance level among seasoned C
programmers of how convoluted the code is allowed to be.


/August

--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
 
Reply With Quote
 
Edward
Guest
Posts: n/a
 
      10-02-2010
August Karlstrom wrote:
> Among C programmers it is not uncommon to use expressions with
> side-effects such as `y++' in the statement `x = y++;'. If we look at a
> statement like
>
> (x == 0)? x++: y *= 2;
>
> Would this be considered bad coding style? Motivate your answer.
>
>
> /August
>

It depends. If you're entering the IOCCC, it's acceptable if a little
too readable. Anywhere else, it's asking for trouble. The harder you
have to work to understand the code, the more likely subtle bugs are to
get past you - as with the precedence problem in your above code that
other posters have identified.
-Edward
--
"Sanity" is the last refuge of the unimaginative.
visit my useless website -- http://dev-null.chu.cam.ac.uk
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      10-02-2010
In article <i871og$lnr$(E-Mail Removed)>,
August Karlstrom <(E-Mail Removed)> wrote:
....
>Note that I would never write this kind of code myself. I wouldn't even
>write `x = y++;' but `x = y; y++;'. What I'm trying to find out is
>whether there is some common acceptance level among seasoned C
>programmers of how convoluted the code is allowed to be.


All you're going to get here is "Don't do that!"

--
"We should always be disposed to believe that which appears to us to be
white is really black, if the hierarchy of the church so decides."

- Saint Ignatius Loyola (1491-1556) Founder of the Jesuit Order -

 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      10-02-2010
On 2010-10-02 14:28, Edward wrote:
> It depends. If you're entering the IOCCC, it's acceptable if a little
> too readable. Anywhere else, it's asking for trouble. The harder you
> have to work to understand the code, the more likely subtle bugs are to
> get past you


Exactly. As I have already mentioned in this thread I wouldn't write
this kind of code myself. Would you also reject the string copy idiom

while (*t++ = *s++);

with the same motivation?


/August

--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Url Rewriting with just an HttpHandler (without the side-effects) Anonieko ASP .Net 0 08-12-2005 01:10 AM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Calling DataBind() multiple times-- strange side effects? Jim Bancroft ASP .Net 2 12-28-2004 12:57 PM
Evaluating expressions with side effects. Jason Heyes C++ 10 12-21-2004 05:17 PM



Advertisments