Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

What is the proper control structure to impose here?

  1. Dec 9, 2015 #1
    I often encounter situations in my professional development like

    Code (Text):

    if ( condition1 )
    {
        // do this
    }
    if ( condition2 )
    {
       // do that
    }
    if ( !condition1 && !condition2 )
    {
       // do something else
    }
     
    and I handle it in exactly that manner. (Note: this is not an if-elseif-else situation. condition1 and condition2 are not mutually exclusive.)

    However, my programming intuition says there's something wrong when it sees that the truthiness of condition1 and condition2 are each checked twice. The alternative

    Code (Text):

    if ( !condition1 && !condition2 )
    {
        // do something else
    }
    else
    {
        if ( condition1 )
       {
           // do this
       }
       if ( condition2 )
       {
          // do that
        }
    }
     
    is even more like spaghetti code. I have yet a third way that gets rid of the redundant conditions by doing

    Code (Text):

    int condval = 1 * (condition1 ? 1 : 0 ) + 2 * (condition2 ? 1 : 0);
    if ( condval & 1 )
    {
        // do this
    }
    if ( condval & 2 )
    {
       // do that
    }
    if ( !condval  )
    {
       // do something else
    }
     
    Yes, I've actually used the above thingy in real life. I feel bad for anyone who has to read my code and figure out what it does.

    What is the proper way of handling this?
     
  2. jcsd
  3. Dec 9, 2015 #2
    Not necessarilly better, but you could also do this.
    Code (C):

    bool x=c1;
    if (x) {
      Do first thing
    }
    if (c2) {
      Do second thing
    }
    else if (!x) {
      Do third thing
    }
     
     
  4. Dec 9, 2015 #3
    That is not equivalent. If you have !c1 && c2 then the third thing is executed.
     
  5. Dec 9, 2015 #4

    Mark44

    Staff: Mentor

    I don't think so, if I understand what you're saying here.
    If c1 is false (which means that x is false), the first if statement doesn't execute. If c2 is true, the second if statement executes, independent of whether x is true or false, but the third doesn't.

    The third if clause executes if c2 is false and x is false.

    The first two if statements both execute when x is true and c2 is true.

    In case it's not completely obvious, the else if clause is associated with the second if clause.

    In cases where the logic is contorted like this, due to conditions that are not mutually exclusive, it's really essential to add comments that explain why the logic is as it is.

    It might be simpler to do this:
    Code (C):

    if (c1 && c2) {
       // Handle case where both conditions are true
    }
    else (if c1) {
       // Handle case where only c1 is true
    }
    else if (c2) {
       // Handle case where only c2 is true
    }
    else {
       // Handle case where neither c1 nor c2 is true
    }
     
  6. Dec 9, 2015 #5

    Ibix

    User Avatar
    Science Advisor

    PF needs two like buttons for this.
     
  7. Dec 9, 2015 #6
    Ah, I see now! Thanks.
     
  8. Dec 9, 2015 #7

    jtbell

    User Avatar

    Staff: Mentor

    I added mine to help you out. :cool:
     
  9. Dec 9, 2015 #8

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    I like this scheme:

    if( condition1 ){
    do one;
    skip3=true;
    }
    if( condition2 ){
    do two;
    skip3=true;
    }
    if( ! skip3 ){
    do three;
    }
     
  10. Dec 9, 2015 #9
    ^ I'm stealing this from you.
     
  11. Dec 10, 2015 #10

    Svein

    User Avatar
    Science Advisor

    A slight adjustment:
    Code (C):

    int condval = (condition1 ? 1 : 0 ) + (condition2 ? 2 : 0);
    switch (condval) {
      case 3: // condition1 AND condition2
      ...
         break;
      case 2: // condition2 AND (not condition1)
      ...
        break;
      case 1: // condition1 AND (not condition2)
      ...
        break;
      case 0: // (not condition1) AND (not condition2)
      ...
        break;
      default: // How the hell did you get here?
      ...
        break;
        };
     
     
  12. Dec 10, 2015 #11
    One thing to consider is adherence to the DRY principle.
     
  13. Dec 10, 2015 #12

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    I was sloppy and forgot to initialize skip3 to false at the top.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: What is the proper control structure to impose here?
Loading...