question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: `return condition` instead of using if/else or ternary with explicit true/false

See original GitHub issue

Not 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:closed
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
fregantecommented, Apr 27, 2021

Good point, prefer-ternary would change

function isLoggedIn() {
	if (state === 'logged-in') {
		return true;
	}

	return false;
}

to

function isLoggedIn() {
	return state === 'logged-in' ? true : false;
}

and no-unneeded-ternary changes that to

function isLoggedIn() {
	return state === 'logged-in';
}
0reactions
fiskercommented, Apr 27, 2021

I’ll find time to fix #1116.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found