Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Feature Requests
 Reverse logic of if statement
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

BenKeeping
New Member

2 Posts

Posted - Mar 12 2012 :  2:20:12 PM  Show Profile  Reply with Quote
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.

feline
Whole Tomato Software

United Kingdom
18951 Posts

Posted - Mar 13 2012 :  3:27:25 PM  Show Profile  Reply with Quote
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.

zen is the art of being at one with the two'ness
Go to Top of Page

BenKeeping
New Member

2 Posts

Posted - Mar 14 2012 :  08:07:58 AM  Show Profile  Reply with Quote
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.
Go to Top of Page

accord
Whole Tomato Software

United Kingdom
3287 Posts

Posted - Mar 14 2012 :  9:47:05 PM  Show Profile  Reply with Quote
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.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000