View Poll Results: Well?

Voters
7. You may not vote on this poll
  • a=b==1?2:3;

    2 28.57%
  • b==1?a=2:a=3

    2 28.57%
  • huh? Whats that ? doing there?

    1 14.29%
  • Ach I don't like ?. If...else.

    2 28.57%

How do you choose...

This is a discussion on How do you choose... within the C++ Programming forums, part of the General Programming Boards category; In reference to a style difference between Soren and I....

  1. #1
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Arrow How do you choose...

    In reference to a style difference between Soren and I.

  2. #2
    Unregistered
    Guest
    I like the 2nd one, its just clearer and I didnt have to look up operator precedence

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    if (b==1) a = 2;
    else        a = 3;
    So much more readable IMO (I don't think I've ever used the : ? ternary operator in code I've written).. and it doesn't take that much more space.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I prefer a variation of #1 that is a little more explicit and thus easier to read.
    a = ( b == 1 ) ? 2 : 3;
    When I choose to use the ternary that is

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

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >So much more readable IMO (I don't think I've ever used the : ? ternary operator in code I've written)

    Yes, but it isn't always an option. The ternary operator goes where if/else fears to tread.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The ternary operator goes where if/else fears to tread.
    Care to describe one of those cases? The only time I've seen that the ternary is truly appropriate is for incredibly long expressions; you increase readability and efficiency becuse the expression is only evaluated once as opposed to twice. Aside from that if..else works just fine.

    [edit]
    I suppose return values and output that use the ternary are okay as well.
    [/edit]

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

  7. #7
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    for (b=a==1?1:0;b<10;++b) {
    cout << "There are " << b << " item" << g==1?"";"s" << " in the box. \n";
    }

    as opposed to

    if (a==1) {
    b == 1;
    }
    else {
    b == 0;
    }
    for (;b<10;++b) {
    cout << "There are " << g << " item";
    if (g!=1) {
    cout << "s";
    }
    cout << " in the box. \n";
    }

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    I suppose it's mainly a shortcut (ever tried passing a parameter using an if/else to a function). But as Imperito brought this up, would it be possible to implement his code -

    Code:
    int a,b,c,d;
          for(a=1;a<10;++a) {
             for(b=a==1?2:0;b<10;b++) {
                for(c=a==1?3:0;c<10;c++) {
                   for(d=a==1?4:0;d<10;d++) {
                      cout<<a<<b<<c<<d;    
                   }
                }
             }
          }
    using if/elses's without making it harder to read?

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    I voted the second option. The first is confusing. For a simple if/else, the ternary is no more confusing than for loops instead of while or do-while loops. For loops without all three operands confuse me, but are used often enough. If things get complicated, don't use it.
    Wasn't there a contest on the web a while back for the longest functioning for loop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 07:41 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 08:35 AM
  3. Probablity algorithm for N choose M in C or C++
    By kappajacko in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2007, 05:19 PM
  4. Replies: 5
    Last Post: 06-07-2007, 12:10 AM
  5. N Choose R
    By .ZG. in forum C Programming
    Replies: 7
    Last Post: 05-18-2004, 03:43 AM

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