Wrong refactoring when combining if clauses

2 posts, 0 answers
  1. Wolfgang
    Wolfgang avatar
    98 posts
    Member since:
    Oct 2012

    Posted 04 Sep 2013 Link to this post

    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 


  2. Zdravko
    Admin
    Zdravko avatar
    657 posts

    Posted 09 Sep 2013 Link to this post

    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.
  3. DevCraft banner
Back to Top