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

Rule request: abstruse conditionals #1436

Open
Drenmi opened this issue Dec 3, 2018 · 2 comments
Open

Rule request: abstruse conditionals #1436

Drenmi opened this issue Dec 3, 2018 · 2 comments
Labels

Comments

@Drenmi
Copy link
Contributor

Drenmi commented Dec 3, 2018

Hello! 🙂

Lately I've been working on cleaning up a lot of old code in RuboCop, and one of the most common patterns that has appeared is overly abstruse conditionals, which contain all the low level details and often mix several conditionals and concepts into a single expression.

Example:

if foo.bar.baz? && foo.qux.include?(:quux) || bar.qux? && bar.foo - bar.baz > 1
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnamed concept 1    
                                                          ^^^^^^^^^^^^^^^^^^^^^ Unnamed concept 2
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unnamed concept 3

These conditionals are usually completely unintelligible, even if you can wrap your head around the order of operations and identify the implicit grouping.

Refactoring these is simple, but not easy. Simple because the solution is just to extract one or more explaining variables, methods, or classes. Hard because reverse engineering the conditional requires substantial work.

It would be nice to have automatic linting for these kinds of conditions.

I'm open to collaborating on defining the rules governing such a linter, but it might not be as straightforward as setting a complexity threshold, as relatively simple conditions can often also benefit from having their concepts named.

@troessner
Copy link
Owner

I like the idea. I'm not sure though, how you would codify "abstruse" 😆
Maybe counting the conditionals? Everything bigger than 2 reeks?

@troessner troessner added the smell label Dec 3, 2018
@Drenmi
Copy link
Contributor Author

Drenmi commented May 20, 2020

I like the idea. I'm not sure though, how you would codify "abstruse" 😆
Maybe counting the conditionals? Everything bigger than 2 reeks?

Perhaps a max_expressions configuration option that defaults to 2?

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