Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
 All Forums
 Visual Assist
 Feature Requests
 Inverting conditions and if/else

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
lallous Posted - Jun 03 2009 : 06:28:59 AM
Hello

It would be nice if the Refactor could also add invert condition support when positioned over a statement.

Another thing is to swap the if/else body.

if ( a )
{
  // do 1
}
else
{
  // do 2
}

then


if ( ! a )
{
  // do 2
}
else
{
  // do 1
}
9   L A T E S T    R E P L I E S    (Newest First)
accord Posted - Nov 04 2011 : 8:41:07 PM
You may find wolfram alpha useful which can help figure out minimal forms of a negating:
www.wolframalpha.com

A simple example to see what I mean. You type

not ((a and b) or (c and d))

and it comes up with alternate minimal forms like:

not (a and b) and not (c and d)

which makes sense. Personally I rarely want to negate complex conditions and think a powerful tool like the above is more safe to use for a non-trivial problem like this (It is based on Wolfram Mathematica, an extremely robust program). I tried a refactoring tool that had this feature for C++ and it wasn't reliably for me (i.e. it could change the meaning of the code, and if you don't catch such a modification immediately, it can cause problems for you later).

This is my opinion. I don't suggest that it cannot be done, I just say it is a non-trivial problem and C++ refactoring tools should be used very carefully based on my experiences. More is not always better
adurling Posted - Nov 02 2011 : 07:24:25 AM
I think the real benefit of a refactoring tool would be in correctly reversing the expressions. I could ! the expression myself, but that makes the code less readable IMHO. In actual fact, some of your competitors offer not only Reverse Conditional but also Simplify Conditional... which seems like a really great refactoring also.
accord Posted - Nov 01 2011 : 8:09:41 PM
It is still on our list of things to consider. Out of interest: which part is the most important for you? Swapping the if and else branches and putting a ! before the condition would be sufficient for you? Or you think that correctly reverse complex expressions is crucial?
adurling Posted - Nov 01 2011 : 12:36:19 PM
Hello,

I realise I'm jumping on a very old thread, but I wondered what happened to this idea?

Maybe the codebase I am working with is a little unusual, but for me this is a killer feature. I'm impressed with VAX in general, but this is an obvious omission.
drac Posted - Jun 05 2009 : 07:17:54 AM
No.(edit, see below)

!(a && b || c && d)
is
(!(a && b) && !(c && d))
which translates to
((a || b) && (c || d))


If you don't believe me write a test program

Cheers

Edit: I didn't believe myself, so here is the test program:

#include <iostream>

void test(bool a, bool b, bool c, bool d)
{
  std::cout  << "a: " << std::boolalpha << a << ", "
             << "b: " << std::boolalpha << b << ", "
             << "c: " << std::boolalpha << c << ", "
             << "d: " << std::boolalpha << d << std::endl;
  
  if (a && b || c && d)
  {
    std:: cout << "1. code 1" << std::endl;
  }
  else
  {
    std:: cout << "1. code 2" << std::endl;
  }

  if ((a || b) && (c || d))
  {
    std:: cout << "2. code 2" << std::endl;
  }
  else
  {
    std:: cout << "2. code 1" << std::endl;
  }

  if (!(a && b || c && d))
  {
    std:: cout << "3. code 2" << std::endl;
  }
  else
  {
    std:: cout << "3. code 1" << std::endl;
  }
  
  if ((!a || !b) && (!c || !d))
  {
    std:: cout << "4. code 2" << std::endl;
  }
  else
  {
    std:: cout << "4. code 1" << std::endl;
   
  }
}

int main()
{
  for (int i = 0; i < 16; ++i)
  {
    test(i & 8, i & 4, i & 2, i & 1);
  }
  
  return 0;
}

And here are the results:

a: false, b: false, c: false, d: false
1. code 2
2. code 1
3. code 2
4. code 2
a: false, b: false, c: false, d: true
1. code 2
2. code 1
3. code 2
4. code 2
a: false, b: false, c: true, d: false
1. code 2
2. code 1
3. code 2
4. code 2
a: false, b: false, c: true, d: true
1. code 1
2. code 1
3. code 1
4. code 1
a: false, b: true, c: false, d: false
1. code 2
2. code 1
3. code 2
4. code 2
a: false, b: true, c: false, d: true
1. code 2
2. code 2
3. code 2
4. code 2
a: false, b: true, c: true, d: false
1. code 2
2. code 2
3. code 2
4. code 2
a: false, b: true, c: true, d: true
1. code 1
2. code 2
3. code 1
4. code 1
a: true, b: false, c: false, d: false
1. code 2
2. code 1
3. code 2
4. code 2
a: true, b: false, c: false, d: true
1. code 2
2. code 2
3. code 2
4. code 2
a: true, b: false, c: true, d: false
1. code 2
2. code 2
3. code 2
4. code 2
a: true, b: false, c: true, d: true
1. code 1
2. code 2
3. code 1
4. code 1
a: true, b: true, c: false, d: false
1. code 1
2. code 1
3. code 1
4. code 1
a: true, b: true, c: false, d: true
1. code 1
2. code 2
3. code 1
4. code 1
a: true, b: true, c: true, d: false
1. code 1
2. code 2
3. code 1
4. code 1
a: true, b: true, c: true, d: true
1. code 1
2. code 2
3. code 1
4. code 1

Conclusion. You were correct!
khb Posted - Jun 05 2009 : 03:29:54 AM
Ehm, drac, shouldn't
!(a && b || c && d)
then be equal to
((!a || !b) && (!c || !d))
rather than
((a || b) && (c || d))
? But in this case there would be more negations. But I don't know if you are heading for performance or readability...

Regards
Marcus
feline Posted - Jun 04 2009 : 1:31:13 PM
Interesting, I had not heard of those rules, I have put a note onto the case about them, thank you.

This is a good example of why I personally am wary of this. Sometimes I end up with very complex if conditions in my code. I try to avoid them, but checking 6 or 8 different "pairs" of things in the check is not unknown.

So I would be reluctant to ask any tool to restructure those if statements, partly because I split the condition across several lines to visually group it, to make it easier to understand.

Then again, if we do offer this command, it should work well in most cases, and should be used with caution in very nasty cases
drac Posted - Jun 04 2009 : 08:22:05 AM
It would be nice if VA would not just negate the whole boolean expression but apply the De Morgan's Laws (http://en.wikipedia.org/wiki/De_Morgan%27s_laws) to that boolean expression:

if (a && b || c && d)
{
  // code 1
}
else
{
  // code 2
}

to

if ((a || b) && (c || d))
{
  // code 2
}
else
{
  // code 1
}

and not just

if (!(a && b || c && d))
{
   // code 2
}
else
{
   // code 1
}
feline Posted - Jun 03 2009 : 10:15:06 PM
This is an interesting idea, and I do understand the appeal. I have put in a feature request to see what our developers think of this:

case=27848

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