Add no-nested-ternary rule #37
kristijanbambir
started this conversation in
Ideas
Replies: 2 comments 1 reply
-
My vote: add rule |
Beta Was this translation helpful? Give feedback.
0 replies
-
My vote: add a rule! Reason: Hard to read. E.g., if we use nested ternary for return statements it is easier to read a few lines rather than one long. function foo() {
return conditionA ? 1 : conditionB ? 2 : 3;
// vs
function bar() {
if (conditionA) {
return 1;
}
if (conditionB) {
return 2;
}
return 3;
}
// i wouldn't mind something like this either
function bar() {
if (conditionA) return 1
if (conditionB) return 2;
return 3;
} Same goes for React components: function Component() {
const { loading, data, error } = fetchData();
return (
<div>
{loading ? 'loading' : error ? 'error' : 'data'}
</div>
); VS function Component() {
const { loading, data, error } = fetchData();
if (error) return 'error';
if (loading) return 'loading';
return <div>data</div>; or function Component() {
const { loading, data, error } = fetchData();
return (
<div>
{loading ? 'loading' : null}
{error ? 'error' : null}
{data ? 'data' : null}
</div>
); #noTernaryTrain |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We often complain that a ternary expression within another ternary expression is hard to read and understand. I propose to add the
no-nested-ternary
as an error to the base JS config. Thoughts?Beta Was this translation helpful? Give feedback.
All reactions