dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: C++ (boolean)? do if true : do if false;

  1. #1
    Join Date
    Jan 2012
    Posts
    3

    C++ (boolean)? do if true : do if false;

    I'm having an issue in one of my functions. I'm trying to format my if statement using the ? and : because I thinks it looks nicer and makes things more readable. But I keep getting an error.

    This is the code:
    Code:
    if(m_search != m_first->prev)
         m_search = m_search->next;
    else
         return;
    I want it to look like this:
    Code:
    (m_search != m_first->prev) ? m_search = m_search->next : return;
    However I get the following error.
    1>c:...\projects\program1\linklist.h(155): error C2059: syntax error : 'return'

    Are you not allowed to return when using that format? Or am I just writing it wrong?

    Oh, and it's a void function by the way.

  2. #2
    Join Date
    Jan 2012
    Posts
    3

    Re: C++ (boolean)? do if true : do if false;

    anyone have a resource on how to format if statements like that?

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: C++ (boolean)? do if true : do if false;

    Quote Originally Posted by sankou View Post
    anyone have a resource on how to format if statements like that?
    It's called the ternary operator. Just search the net.

    The selections must be expressions (evaluate to a value) so return wont work.

    I'd say this is cleaner and simpler,

    Code:
    if (m_search == m_first->prev) return;  // quit
    m_search = m_search->next;

  4. #4
    Join Date
    Jan 2012
    Posts
    3

    Re: C++ (boolean)? do if true : do if false;

    Quote Originally Posted by nuzzle View Post
    It's called the ternary operator. Just search the net.

    The selections must be expressions (evaluate to a value) so return wont work.

    I'd say this is cleaner and simpler,

    Code:
    if (m_search == m_first->prev) return;  // quit
    m_search = m_search->next;
    Cool, thanks for the info. I'd forgotten what the operator was called, been a while since my CS I class.

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: C++ (boolean)? do if true : do if false;

    Quote Originally Posted by sankou View Post
    I'd forgotten what the operator was called, been a while since my CS I class.
    I'd say the general consensus is that you shouldn't overuse/overcomplicate the ternary operator because it quickly gets quite dense. The most common use is in simple assignment selections like this for example,
    Code:
    int i = (b) ? 0 : 1; // assign 0 to i if b is true, otherwise 1

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: C++ (boolean)? do if true : do if false;

    I agree with Nuzzle, just use them for simple assignments where the equivilent 'if' statement would add nothing but extra lines of code.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,545

    Re: C++ (boolean)? do if true : do if false;

    Quote Originally Posted by nuzzle View Post
    ...
    I'd say this is cleaner and simpler,

    Code:
    if (m_search == m_first->prev) return;  // quit
    m_search = m_search->next;
    Well, I agree that it is "simpler".
    But I wouldn't say it looks like "clean" enough. It is a bad style to place more than one operand/command on the same line: it is hard to read and very hard to debug!
    So my suggestion is
    Code:
    if (m_search == m_first->prev)
          return;  // quit
    m_search = m_search->next;
    Victor Nijegorodov

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,554

    Re: C++ (boolean)? do if true : do if false;

    Quote Originally Posted by VictorN View Post
    Well, I agree that it is "simpler".
    But I wouldn't say it looks like "clean" enough. It is a bad style to place more than one operand/command on the same line: it is hard to read and very hard to debug!
    So my suggestion is
    Code:
    if (m_search == m_first->prev)
          return;  // quit
    m_search = m_search->next;
    I agree. One command per line. If you put the return on the same line as the if statement, unless you look closely it looks like the assignment statement belongs to the if.

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: C++ (boolean)? do if true : do if false;

    The thing about ternary operator is that they are designed to return a value: eg, for assignement, like this:

    Code:
    std::string Name = bIsMale ? "Mike" : "Sally";
    One of the MAJOR repercussions from this is that both the right hand sides must not only be evaluable, but the evaluated value must both have the same type.

    If both don't evaluate to the same type, then some very complicated type promotion rules start to come into account. These are usually never expected, and can and will give you some very unexpected behavior...

    Image 3 types, A, B and C, where A and B can be cast to C type. Thus, this makes sense:

    Code:
    A a = some_object;
    B b = some_other_object;
    C c = some_bool ? a : b;
    So does this work? Chances are it won't even compile, because A and B are not compatible. And if it does compile, that means either a or b is silently converted to the other type silently.

    I had this happen to me the other day. My "b" was first converted to A type, before being returned to c. The problem is that b was not a compatible A object, and create an object with status IsValid == false.

    Long story short: Unless your ternary is extremely simple (nothing more than ints, or pointers), double think your choice.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  10. #10
    Join Date
    Oct 2008
    Posts
    1,456

    Re: C++ (boolean)? do if true : do if false;

    Quote Originally Posted by monarch_dodra View Post
    The thing about ternary operator is that they are designed to return a value: eg, for assignement
    actually, those types are allowed to be void or even a throw expression, as in

    Code:
    b ? 0 : throw 1;
    b ? throw 0 : throw 1;
    b ? f() : g(); // both f and g have type void()
    indeed, you can put any expression you like in the ternary operator, provided their types are "compatible", according to those complicated rules monarch_dodra alluded to.

    hence, the reason why you can't put a return in the ?: operator is just that "return" is a statement but it's not an expression.

  11. #11
    Join Date
    Oct 2008
    Posts
    1,456

    Re: C++ (boolean)? do if true : do if false;

    Quote Originally Posted by nuzzle View Post
    I'd say this is cleaner and simpler
    as others said, this is debatable, as putting return statements in the middle of a function is not considered best practice anyway ...
    indeed, looking at that code quickly by focusing on the assignment line my brain sees something like

    Code:
    if (m_search == m_first->prev) ?something?;  // ?something?
    m_search = m_search->next;
    concluding that the assignment gets always executed, just to realize later that there's a return statement in the if body ... so, I wouldn't qualify that code as cleaner or readable ( this issue being mitigated with Victor's suggestion though ).

    IMHO, the only exception I can think of is when such conditional returns are placed at the beginning of a function, say, to validate function parameters without polluting the primary function logic. In any case, even then I usually prefer a different solution though ...

  12. #12
    Join Date
    Aug 2009
    Posts
    440

    Re: C++ (boolean)? do if true : do if false;

    It might be clearer to do:
    Code:
    if( something ) return;
    
    int a = 5;
    The space adds a natural separation. Though, I would probably do something like:
    Code:
    if( something )
    {
        return;
    }
    
    int a = 5
    Yeah, it is more lines of code, but always adding brackets makes the code uniform and avoids issues like:
    Code:
    if( something )
        a = 5
        b = 6
    In addition, most of the editors I use can be setup to always add the brackets, so it isn't really a hassle to use them.

  13. #13
    Join Date
    May 2009
    Posts
    2,413

    Re: C++ (boolean)? do if true : do if false;

    Quote Originally Posted by VictorN View Post
    It is a bad style to place more than one operand/command on the same line:
    I put the return where I did because that's what I always do. My general guidline is "one line, one thought" and within that as safe constructs as possible. Depending on the circumstances I would either do,
    Code:
    if (b) return;
    or
    Code:
    if (b) {
       return;
    }
    and nothing else because that's my personal taste.

    But that wasn't what I was trying to convey to the OP. I wanted to give an alternative to the if statement he didn't like that didn't involve misuse of the ternary operator he was up to. That's all.

    Finally, I don't put in a return just anywhere but I avoid preaching about it on every occasion. You can't turn every thread into a discussion about that kind of stuff. Quite frankly, self-appointed judges of programming style who think their personal taste is law are annoying.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,554

    Re: C++ (boolean)? do if true : do if false;

    Quote Originally Posted by nuzzle View Post
    I put the return where I did because that's what I always do. My general guidline is "one line, one thought" and within that as safe constructs as possible. Depending on the circumstances I would either do,
    Code:
    if (b) return;
    or
    Code:
    if (b) {
       return;
    }
    and nothing else because that's my personal taste.

    But that wasn't what I was trying to convey to the OP. I wanted to give an alternative to the if statement he didn't like that didn't involve misuse of the ternary operator he was up to. That's all.

    Finally, I don't put in a return just anywhere but I avoid preaching about it on every occasion. You can't turn every thread into a discussion about that kind of stuff. Quite frankly, self-appointed judges of programming style who think their personal taste is law are annoying.
    But the point of this forum is to help beginners. Putting it on one line has two problems. First, it's harder to read, and second, if you put a breakpoint on the if statement, you can't tell how it evaluates, although with a return it would be a bit more obvious. Sorry it annoys you, but I wouldn't let somebody that worked for me code like that, and I think it helps beginners to show them good style.

  15. #15
    Join Date
    May 2009
    Posts
    2,413

    Re: C++ (boolean)? do if true : do if false;

    Quote Originally Posted by GCDEF View Post
    But the point of this forum is to help beginners. Putting it on one line has two problems. First, it's harder to read, and second, if you put a breakpoint on the if statement, you can't tell how it evaluates, although with a return it would be a bit more obvious.
    You have to focus on what people are actually asking and give relevant and to the point replies. If you lecture on the bigger picture all the time no one will be listening.

    Besides "where to put the bracket" is not a very important question when it comes to good programming style. And in fact this kind of advice should better be avoided because it's in the personal taste category. Don't sweat the small stuff. That's the first advice in the best book on C++ coding style ever written (*).

    So you're dead wrong. Your're confusing your personal taste with good programming style. Your preferrance is not a god given law of nature. I say drop this pompous attitude not to make a fool out of yourself.

    (*) C++ Coding Standards by Sutter & Alexandrescu.

    Sorry it annoys you, but I wouldn't let somebody that worked for me code like that, and I think it helps beginners to show them good style.
    Well, fortunately you don't have very many people working for you, have you?

    I'm pro a coding standard document on a project basis. Not because I think one is better style than the other but because code consistency is good for a project. This is a good example (even though it's very special purpose),

    http://www2.research.att.com/~bs/JSF-AV-rules.pdf
    Last edited by nuzzle; January 31st, 2012 at 08:52 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)