loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

eslint: forbid boolean function arguments

Open bajtos opened this issue 4 years ago • 6 comments

Function arguments of type boolean are considered as a bad practice because they make the code difficult to read and reason about.

Consider the following code:

ctx.getBinding('repositories.todo', true);

What does the true flag mean? There is no way to tell when reading the code using the function, we have to look up the API definition and/or implementation to understand.

Compare with the current implementation which is following the best practices:

ctx.getBinding('repositories.todo', {optional: true});

Now it's more clear that we want to treat the binding as optional and don't trigger an error when it's not found.

Let's improve our eslint configuration to automatically detect and reject boolean arguments. Eslint rule to use: no-inferrable-types

Further reading:

Acceptance criteria

  • [ ] A commit modifying our eslint configuration to enable the new check. This is a breaking change, see Describe incompatibilites for release notes
  • [ ] One or more commits fixing any violations of this new rule in our existing code. For functions (methods) that are part our public API, decide whether to disable this rule via a code comment, change the method signature in a breaking (semver-major) change or implement both variants (with an options arg, with a boolean arg for backwards compatibility) to avoid a breaking change.

Breaking changes must be committed in such way that they don't trigger semver-major release of packages that are not affected.

bajtos avatar Feb 18 '20 13:02 bajtos

Is no-inferrable-types the correct eslint rule? AFAIK it's to prevent unnecessary verbosity when declaring a variable or parameter type.

Though it's still a good rule to enable nonetheless.

achrinza avatar Feb 20 '20 10:02 achrinza

@achrinza no-inferrable-types is already enforced to avoid something like const x : string = 'abc';.

raymondfeng avatar Feb 20 '20 22:02 raymondfeng

Is no-inferrable-types the correct eslint rule? AFAIK it's to prevent unnecessary verbosity when declaring a variable or parameter type.

Hmm, that's a good question. Now that I am re-reading the documentation for no-inferrable-types, I am not sure why I thought this is the rule to use 🙈

bajtos avatar Mar 06 '20 14:03 bajtos

I reviewed eslint rules and did not find any that we could use to enforce this coding style. I am closing this issue as it is not actionable now.

bajtos avatar Sep 29 '20 09:09 bajtos

Hi 👋 I was looking for a similar rule lint rule for another project and came across this issue. Thought you might like to know that I just implemented a rule in @foxglove/eslint-plugin called @foxglove/no-boolean-parameters. More info here: https://github.com/foxglove/eslint-plugin/tree/main/rules#rules

jtbandes avatar Aug 28 '21 04:08 jtbandes

Thanks, @jtbandes! Will see if we can incorporate it into our monorepo.

achrinza avatar Aug 28 '21 11:08 achrinza