Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1685

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T10:27:07+00:00 2024-11-25T10:27:07+00:00

“Split or condition in if” refactoring

  • 62k

We’d like to document a refactoring that we usually teach to teams we work with: the Split or condition in if refactoring.

Split or condition in if.

Motivation.

Sometimes we have nested conditionals that are branching depending on the same information. When an outer if condition contains an or which combines boolean expressions that check that information, splitting the or in the outer if may lead to opportunities to remove the rechecks in inner branching. This happens because having that or in the outer if forces us to check again the same information in an inner if condition.

Let’s see an example:

export class Rover {   // some code    execute(command) {     if (command === 'l' || command === 'r') {       if (this.direction === "N") {         if (command === 'r') {           this.direction = "E";         } else {           this.direction = "W";         }       } else if (this.direction === "S") {         if (command === 'r') {           this.direction = "W";         } else {           this.direction = "E";         }       } else if (this.direction === "W") {         if (command === 'r') {           this.direction = "N";         } else {           this.direction = "S";         }       } else {         if (command === 'r') {           this.direction = "S";         } else {           this.direction = "N";         }       }     } else {       // some more code     }   } } 
Enter fullscreen mode Exit fullscreen mode

Have a look at the execute method code of the Rover class (we’ve omitted some code for brevity). Notice how this code is branching twice depending on the value of the command variable. Using an or in the condition of the outer if, makes it necessary to recheck the command variable again in inner if conditions. This adds unnecessary nesting and duplication that can be easily removed if we first split the or in the outer if.

Mechanics.

Some IDEs have automated this refactoring, for instance, the JetBrains family of IDEs.
If you’re using one of JetBrains IDEs, you only need to place your cursor on the or condition and press ALT+ENTER to ask the IDE for possible context sensitive actions and select the Split ‘||’ condition in ‘if’ from the pop up menu.

Image Automated refactoring in WebStorm

Automated refactoring in WebStorm.

If your IDE does not have this refactoring automated, you can use the following mechanics to safely split the or condition in the if:

  1. Duplicate the if. This step might be different depending on if there was initially an else or not.
    • If there was an else, you’d need to add the duplicated code in an else if block.
    • If there was no else, you can just duplicate the whole if besides the original one.

    Note that this step may change the behaviour. The tests may not pass until you do the next step.

  2. Remove one clause of the or condition from one of the copies and the other clause of the or condition from the other copy. At this point, run the tests to be sure that you haven’t accidentally modified the behaviour and the refactoring went ok.

Image description

Refactoring mechanics when there’s no else.

Image description

Refactoring mechanics when there’s an else.

Related code smells.

Mainly Duplicated Code and Complicated Boolean Expression.

Splitting or condition in if results in a code where is easier to remove both Duplicated Code and Complicated Boolean Expression smells, which in turn may unmask other smells.

In the following video, we show how after applying this refactoring we can easily remove duplicated if conditions because they become obsolete (even though the refactoring is automated in WebStorm, we used the mechanics described above for demonstration purposes).

Related refactorings.

The inverse of this refactoring pattern is the Combine ifs refactoring pattern described by Christian Clausen in his book Five Lines of Code, How and when to refactor

Conclusion.

We have described the Split or condition in if refactoring, and provided safe mechanics to perform it when it's not automated by your IDE. We hope this refactoring technique might be useful to you.

Acknowledgements.

I’d like to thank Fran Reyes and Rubén Díaz for giving me feedback on the final draft of this post.

beginnersprogrammingrefactoringwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.