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

Do not use else if and else clauses where return statements make them redundant #12

Open
cbeams opened this issue Jun 6, 2018 · 0 comments

Comments

@cbeams
Copy link
Contributor

cbeams commented Jun 6, 2018

For example, in the following code, the else if and else clauses are redundant:

if (someCondition) {
    return X;
} else if (otherCondition) {
    return Y;
} else {
    return Z;
}

This code can and should be simplified to read as follows:

if (someCondition)
    return X;

if (otherCondition)
    return Y;

return Z;

Whether or not braces are used for single-line conditionals is a matter for a different style rule, but in any case, the second code block is easier to read, uses fewer conditional constructs, and makes it all the more obvious that returning "Z" is the fallback / default case if no preceding conditionals are matched.

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

No branches or pull requests

1 participant