openrewrite / openrewrite/rewrite-static-analysis

Recipe to simplify code when a similar expression is used in both values of a ternary operator

Open
#695 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?

Increase code readability.

Describe the situation before applying the recipe

        return s2.length() < s1.length() ? s2.toString() : s1.toString();

Describe the situation after applying the recipe

        return (s2.length() < s1.length() ? s2 : s1).toString();

Context

The change is not always safe to be made. This might result in compilation errors. Sketch of a situation when the change shouldn't be applied as it's illegal:

interface Hotel { int roomNumber(); }
interface Apartment { int roomNumber(); }

class A {
  int count(Object venue) {
     return venue instanceof Hotel ? ((Hotel) venue).roomNumber() : ((Apartment) venue).roomNumber();
  }
}

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

No file, test, or entry point is named. Start by locating the Java static-analysis recipe code and compare the issue’s before-and-after examples, including the Hotel/Apartment case. Done means the recipe simplifies safe ternary expressions without producing invalid Java or compilation errors.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.