Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-nested-ternaries can cause difficult to understand code #1811

Closed
chrislloyd opened this issue May 16, 2018 · 1 comment
Closed

no-nested-ternaries can cause difficult to understand code #1811

chrislloyd opened this issue May 16, 2018 · 1 comment
Labels

Comments

@chrislloyd
Copy link

The rule states that "Nesting ternary expressions can make code more difficult to understand." However, by enabling the rule engineers often have to create and nae intermediate variables which can also make the code more difficult to understand.

A real world example:

screen_shot_2018-05-16_at_12 31 18_pm_1024

I'd argue that in this case both the variable names and also having to move conditional logic further away from where it is executed make the code more inscrutable.

My recommendation would be disabling the no-nested-ternary rule in the short term and enabling the rule to be a bit more lenient in the long term. Happy to put in a PR if that's the consensus of the maintainers too.

@chrislloyd chrislloyd changed the title no-nested-ternaries often causes unnecessary intermediate variables no-nested-ternaries can cause difficult to understand code May 16, 2018
@ljharb
Copy link
Collaborator

ljharb commented May 16, 2018

In general, I'd say that intermediate variables (in the same region of the code) almost always make code easier to understand. I acknowledge that the vagaries of jsx and JS combined which require locating these things in different places may disrupt that, at times.

However, it seems like this might be a better alternative:

{!showPinSaveOverflow && !promotedIsRemovable && saveButton}
{showPinSaveOverflow && !isOwnPin && saveButtonPromotedRemoved}

In general tho, this is complex logic that doesn't really seem to have a clear way to express it - perhaps moving some of this logic into instance methods would help, or, putting this into its own SFC?

Either way I don't think a version with a nested ternary is going to be more readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants