Rule proposal: `return condition` instead of using if/else or ternary with explicit true/false
See original GitHub issueNot to be confused with https://eslint.org/docs/rules/no-else-return Complementary to https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-ternary.md
Occasionally I see Boolean functions that include multiple returns where a plain return condition
would suffice.
Fail
function isLoggedIn() {
if (state === 'logged-in') {
return true;
}
return false;
}
function isTruthy(value) {
if (value) {
return true;
}
return false;
}
function isNumber(value) {
return typeof value === 'number' ? true : false;
}
Pass
function isLoggedIn() {
return state === 'logged-in';
}
function isTruthy(value) {
return Boolean(value);
}
function isNumber(value) {
return typeof value === 'number';
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:9
Top Results From Across the Web
Can `prefer-ternary` be merged into eslint core rule `no-else ...
Yes, I understand your point, but I still think the two rules are similar, if we can merge then the built-in one could...
Read more >How to use Java's conditional operator ?: | TheServerSide
The Java ternary operator provides an abbreviated syntax to evaluate a true or false condition, and return a value based on the Boolean...
Read more >Ternary conditional operator - Wikipedia
In computer programming, the ternary conditional operator is a ternary operator that is part of the syntax for basic conditional expressions in several ......
Read more >Does Python have a ternary conditional operator?
Yes, it was added in version 2.5. The expression syntax is: a if condition else b. First condition is evaluated, then exactly one...
Read more >Conditional (ternary) operator - JavaScript - MDN Web Docs
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?)
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Good point,
prefer-ternary
would changeto
and
no-unneeded-ternary
changes that toI’ll find time to fix #1116.