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; I've heard from a couple of people that using the operators "?:" instead of using "if/else" is bad coding practice ...

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    Ternary operators, bad practice?

    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. Others I have talked to say that everybody uses them. Anyone know which one is true?

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I wouldn't say BAD always...

    It tends to be less readable at first glance, especially when nested, but it has advantages (namely the fact that it not only executes but returns it's expression)

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ?: can be used in places where if else cant be used. For instance inside a member initialisation list.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    If you think they aren't useful it's just because you haven't tried them. The simplicity and readability can hardly be ignored. I especially like them in return statements.

    return ( !bad() && a_ok) ? true : false;

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I agree completely. They're really nice for return values. On that note, they're often handy just for general value assignments. It's clean and compact:
    Code:
    if( someTest )
    {
        myVar = firstValue;
    }
    else
    {
        myVar = secondValue;
    }
    
    myVar = someTest ? firstValue : secondValue;
    I know which one I prefer in this case. Beyond that, they can be a great deal of fun.

    return st1 ? sv1 : st2 ? sv2 : st3 ? sv3 : sv4;

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

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Interseting Quzah. I've never used the nested variety. A little unsightly, but interesting.

  7. #7
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    Sometimes you must use ?:
    Code:
    int n;
    // do some stuff
    printf("You have %d item%s.\n", n, n>1?"s":"");
    If you use if/else, you will get a long code.
    But I found that ?: cant be used with cout, is that right?
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Sebastiani

    return ( !bad() && a_ok) ? true : false;

    Why do you code like that?
    Why not:
    Code:
    return !bad && a_ok;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I definately find them useful and use them often within my code. I was just told by a few people that coding standards consider them as unsightly as 'goto' statements, unless they greatly improve readibility. I'm glad that it appears this isn't true

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Originally posted by Sang-drax

    Why do you code like that?
    Why not:
    Code:
    return !bad && a_ok;
    I know it was a bad example. In practice I would have just used the inherent boolean nature of the comparison...but it was late.

  11. #11
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    >But I found that ?: cant be used with cout, is that right?

    nope.. it can be used with cout.

    cout<<(isGood()? "it's good" : "it's not so good")<<endl;
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    every so often you'll need a cast when you're talking about a cout because of course cout::operator << is only overloaded for so many types. But it can definitely be used
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    Shadow12345
    Guest
    Does a ternary operator statement execute faster than a similar if statement?

    which would be faster?:

    FillYourBrain.alive? GreetFill() : BuryFill();

    if(FillYourBrain.alive)
    GreetFill();
    else
    BuryFill();


    I don't like the nested ternary operators, if/else seems more appropriate (easier to read)

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    24
    Originally posted by quzah


    return st1 ? sv1 : st2 ? sv2 : st3 ? sv3 : sv4;


    explain please, lol

  15. #15
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Shadow, your example probably compiles exactly the same. Just a guess really but either way I would use the one that felt more readable for that particular instance.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Page 1 of 3 123 LastLast
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, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  3. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  4. Bad Practice??
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 07: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, 07:31 AM

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