This is a migrated thread and some comments may be shown as answers.

Wrong refactoring when combining if clauses

1 Answer 12 Views
Refactorings
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Wolfgang
Top achievements
Rank 1
Wolfgang asked on 04 Sep 2013, 11:19 AM
Let's take the following code. Assume that every variable is a bool value.

if (!a || b)
    if (c || d || (e && f))
             DoSomething();                   

using the JustCode refactoring, the following code pops up:

if (!a || b && c || d || (e && f))
     DoSomething();

Well.. do you see the problem?












Hint: 
bool a, b, c, d, e, f;
 
a = b = c = d = e = f = false;
 
if(!a||b)
    if(c ||d||(e&&f))
        Console.WriteLine(  "This will not be printed.");
 // just click on "Join nested if statement"
if (!a || b && c || d || (e && f))
    Console.WriteLine("Oh no, this should not be printed, what happened?");


With the first code the first if a! is true, but the second (c ||d||(e&&f) is false.
With the second code DoSomething will be executed, as !a is true, and the other parts does not need to be checked...

The refactoring just makes correct code wrong.

If a join is done in such a case a few more paranthesis must be added...

if ((!a || b) && (c || d || (e && f)))

I doubt a bit that this makes the code easier to read... but after all it would not change the result of the code 


1 Answer, 1 is accepted

Sort by
0
Zdravko
Telerik team
answered on 09 Sep 2013, 07:13 AM
Hello Wolfgang,

 Thanks for bringing this issue to our attention and for the detailed description.
I logged it for fixing in our product backlog.
Thank you and stay tuned.

Regards,
Zdravko
Telerik
Share what you think about JustCode with us, so we can help you even better! You can use the built-in feedback tool inside JustCode, our forum, or our JustCode feedback portal.
Tags
Refactorings
Asked by
Wolfgang
Top achievements
Rank 1
Answers by
Zdravko
Telerik team
Share this question
or