openrewrite / openrewrite/rewrite-static-analysis

Recipe to extract the common code if both if and else start with the same statement

Open
#681 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

recipe
Dominant language
Java
Stars
62
Forks
112
Avg merge
1d 19h
Merged PRs (30d)
40

Description

What problem are you trying to solve?

Code readability.

What precondition(s) should be checked before applying this recipe?

  • Only applicable if the if condition doesn't have any side-effects. Or this needs special handling

Describe the situation before applying the recipe

        if (score >= 50) {
            System.out.println("Checking eligibility...");
            System.out.println("You passed!");
        } else {
            System.out.println("Checking eligibility...");
            System.out.println("You failed.");
        }

Describe the situation after applying the recipe

        System.out.println("Checking eligibility...");
        if (score >= 50) {
            System.out.println("You passed!");
        } else {
            System.out.println("You failed.");
        }

Context

  • Similarly, the same could be done for trailing statements.
  • As a very special case, if both branches have the same code, this would eventually get rid of the if.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Java before-and-after examples in the issue and inspect existing recipe unit tests to find the appropriate recipe structure and test location. Clarify how conditions with side effects and trailing statements should be handled; done means the common leading statement is extracted while the shown conditional behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.