openrewrite / openrewrite/rewrite-static-analysis

Recipe to replace trivial if statements with ternary operator

Open
#683 2 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. Funnily, this is opposite of #50. Have a recipe to convert if-else statements to a ternary (Elvis) operator when applicable.

In specific cases the ternary operator is more readable than a verbose if-else. I think we might want to apply some heuristics on the condition being simple and/or the values assigned/returned being simple.

Describe the situation before applying the recipe

        if (age >= 18) {
            category = "Adult";
        } else {
            category = "Minor";
        }

Describe the situation after applying the recipe

        category = (age >= 18) ? "Adult" : "Minor";

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 by reviewing the existing static-analysis recipes in this repository to find the recipe and test conventions for Java transformations. Define the applicable condition and expression simplicity cases from the issue before implementing the conversion, then add coverage demonstrating the requested before-and-after behavior and run the relevant recipe tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.