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

New Inspection: Inform about Boolean Parameters #644

Merged
merged 4 commits into from
May 2, 2022

Conversation

saeltz
Copy link
Collaborator

@saeltz saeltz commented Apr 26, 2022

Boolean parameters can easily lead to bugs. Especially if there are more than one.
This inspection informs about them, so developers can take a wise decision.

This has already been said by famous Robert C. Martin in Clean Code.

The formatting is part of #643 but this PR is based on the other to have working formatting.

@saeltz saeltz marked this pull request as ready for review April 26, 2022 15:15
@saeltz saeltz requested a review from mccartney April 26, 2022 17:10
@sksamuel
Copy link
Collaborator

Lgtm. @mccartney you ok to merge ?

@mccartney mccartney self-assigned this Apr 30, 2022
@mccartney
Copy link
Collaborator

Do we have support for inspections being disabled by default?
While I can see this might be useful in some cases, but my hunch is that most folks would see it weird to discourage Boolean parameters to methods. The suggestion to use two methods instead (i.e. something similar to overloading) seems like an overkill. What if there were 2 or 3 Boolean parameters - would you suggest to have 4 or 8 methods instead?

Anyway - I don't mind adding this inspection. I'd only seek for it not being on by default (which I am not sure if we have support for).

@saeltz
Copy link
Collaborator Author

saeltz commented May 2, 2022

Do we have support for inspections being disabled by default?

Not really but the default list of disabled inspections could be changed to include this inspection. But that's not the nicest (but easy) solution.

What if there were 2 or 3 Boolean parameters - would you suggest to have 4 or 8 methods instead?

Robert Martin argues that if the method takes a boolean parameter than it most probably does two things based on an if. Then it should better be two methods.
If you have multiple booleans, the code becomes even more troublesome because it's easy to mix booleans at call-site, e.g.

methodName(x, true, false)
vs.
methodName(x, false, true)

This becomes easily unnoticeable without enforcing named parameters. (I haven't found a way to add that as an inspection.)
If the multiple booleans belong to a model that is handled in the method, one could argue to create a case class for the model and pass that instead, e.g.

final case class Model(x: X, isActive: Boolean, hasPermission: Boolean)
def methodName(model: Model) = ???

I'd only seek for it not being on by default

I specifically added this inspection on INFO level only, so it doesn't stop compilation. Same as with object name recommendations.

@mccartney
Copy link
Collaborator

Makes sense. Thanks for the context and the explanation. I think we are good to go.

Also filing #646 to simplify things.

All in all - I am happy to see your contributions. I am pretty sure Sam is happy too.

@mccartney mccartney merged commit 7f5fad5 into scapegoat-scala:master May 2, 2022
@saeltz saeltz deleted the booleanParameters branch May 2, 2022 20:17
@sksamuel
Copy link
Collaborator

sksamuel commented May 2, 2022 via email

@saeltz
Copy link
Collaborator Author

saeltz commented May 3, 2022

Could we maybe release a new minor version of scapegoat including this new inspection?

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

Successfully merging this pull request may close these issues.

3 participants