Ternary operators, bad practice?

This is a discussion on Ternary operators, bad practice? within the C++ Programming forums, part of the General Programming Boards category; it's also called the conditional operator...

  1. #31
    Shadow12345
    Guest
    it's also called the conditional operator

  2. #32
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by pianorain
    They're not so bad if you add a few parenthesis for readability.

    return st1 ? sv1 : (st2 ? sv2 : (st3 ? sv3 : sv4));
    Depending on the situation, if I need readability, or if the test is lengthy, I usually do something like:
    Code:
    return Condition1 ? ReturnValue1 :
           Condition2 ? ReturnValue2 :
           Condition3 ? ReturnValue3 :
           Condition4 ? ReturnValue4 : Returnvalue5;
    It makes it farily simple to understand, and its shorter than a big if/else ladder.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #33
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I disagree.
    Code readability is very important and these codes are equivalent:
    Code:
    return Condition1 ? ReturnValue1 :
           Condition2 ? ReturnValue2 :
           Condition3 ? ReturnValue3 :
           Condition4 ? ReturnValue4 : Returnvalue5;
    Code:
    if (Condition1) return ReturnValue1;
    if (Condition2) return ReturnValue2;
    if (Condition3) return ReturnValue3;
    if (Condition4) return ReturnValue4;
    return ReturnValue5;
    The purpose of the code using "if" is (more) obvious, even to a programmer not familiar with C/C++/Java/Omicron. It is easier to read and should therefore be used.

    Anyways, I haven't encountered many real-life situations where such return conditions are used.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #34
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Originally posted by quzah
    Code:
    return Condition1 ? ReturnValue1 :
           Condition2 ? ReturnValue2 :
           Condition3 ? ReturnValue3 :
           Condition4 ? ReturnValue4 : Returnvalue5;
    The only time I've done something like that was when I was confusing my second-rate C++ professor. Not saying it's bad...it just has very specialized uses.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #35
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've heard from a couple of people that using the operators "?:" instead of using "if/else" is bad coding practice and should
    >be avoided unless it makes the code MUCH clearer.
    It's funny how questions like these seem to crop up so much and turn into large debates. The conditional operator is a Bad Thing like goto is a Bad Thing, it isn't. It's just another tool that if used without discipline can create impenetrable code. Sometimes there's a better solution, sometimes not, but that's a decision up to the individual programmer.

    -Prelude
    My best code is written with the delete key.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 08:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 05:37 AM
  4. Bad Practice??
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 08:37 AM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21