Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
 All Forums
 Visual Assist
 Feature Requests
 Reverse logic of if statement

You must be registered to post a reply.
Click here to register.

Screensize:
UserName:
Password:
Format: BoldItalicizeUnderlineStrikethrough Align leftCenterAlign right Insert horizontal ruleUpload and insert imageInsert hyperlinkInsert email addressInsert codeInsert quoted textInsert listInsert Emoji
   
Message:

Forum code is on.
Html is off.

 
Check to subscribe to this topic.
   

T O P I C    R E V I E W
BenKeeping Posted - Mar 12 2012 : 2:20:12 PM
I often come across patterns like this in my company's code:

{
    bool result = true;
    ... some code which may set result to false.
    if ( result == ok)
    {
        ... remainder of function, now indented one level further
    }
    else
    {
       report an error
    }
    ...
    return result;
}

Perhaps a former developer came to C++ from Pascal where only one return point is possible.

This makes me wish there was a Visual Assist refactoring "Reverse logic of if statement" which would allow me to click on the 'if' and turn it automatically into

    if (!(result == ok))
    {
       report an error
    }
    else
    {
        ... remainder of function, now indented one level further
    }

I would then still want to manually edit this to

    if (result != ok)
    {
       report an error
       return false;  
    }
    ... remainder of function, now NOT indented one level further

But at least the refactoring tool could help me move things into the right order.
3   L A T E S T    R E P L I E S    (Newest First)
accord Posted - Mar 14 2012 : 9:47:05 PM
We are considering doing this at some point:

case=27848

quote:
Regarding the point about executing code - you'll notice I suggested just putting !(...) around the original expression

I have added a note about this to the case. Not everyone would be satisfied with this solution, though:

http://forum.wholetomato.com/forum/topic.asp?TOPIC_ID=8780

But implementing this simpler version would be easier.
BenKeeping Posted - Mar 14 2012 : 08:07:58 AM
Well, since "else if" is just a bracket-saving shorthand for

if ( result == ok )
{
    // action 1
}
else
{
     if ( result == error )
    {
        // action 2
    }
    else
    {
        // action 3
    }
}


I would expect it to produce


if (!( result == ok ))
{
    if ( result == error )
    {
        // action 2
    }
    else
    {
        // action 3
    }
}
else
{
    // action 1
}


Regarding the point about executing code - you'll notice I suggested just putting !(...) around the original expression, because I was aware that reversing the logic in a less general way (such as changing to 'result != ok' in my example) would be more complex.
Given that, I think it's a safe assumption that the expression will be evaluated in the same way.
feline Posted - Mar 13 2012 : 3:27:25 PM
We are unlikely to try and do this, since this sort of problem very quickly gets very hard. Consider a slightly more complex situation:

if ( result == ok )
{
    // action 1
}
else if ( result == error )
{
    // action 2
}
else
{
    // action 3
}

Now what does the concept of reversing the if statement mean?

This is before you consider more complex if statements that execute code as well as performing a check, where inverting the condition may have unexpected side effects.

© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000