Skip to content

Recipe to replace trivial if statements with ternary operator #683

@greg-at-moderne

Description

@greg-at-moderne

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";

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions