Add exclude name regex to "Functions should not have too many parameters"

We are using Angular for our project and it uses constructor based dependency injection. This can lead to the constructor having a lot of parameters as it injects the necessary tokens for a given component / service.

The issue is that the “Functions should not have too many parameters” rule in JavaScript / TypeScript is great in every other context then this. We don’t want to raise the limit as normal functions & methods should not have too many parameters.

The feature request is some kind of regex based function/method exclusion to be included in the rule. This would allow us to exclude “constructor” from these checks, which will still affect normal class constructors but is far more ideal then having to raise the limit for this rule.

Hello,

Would you be able to share a sample project that illustrates the problem? That would help to make the good choice for the solution to implement.

Thanks
Alex

1 Like

Here is a GitHub repo & StackBlitz project that will cause the rule to be flagged

I can provide any additional information as needed. Sorry for the delayed response.

1 Like

Hi Sean,

Thank you for bringing this up and for that repo! This kind of contribution is very helpful.

We’re considering improving the rule to account for this Angular use case :slight_smile:

Created an issue on Github to track this.

Digging in a bit more:

  • Do you think these components with many dependencies are outliers or pretty much always like that?
  • Could you share the rough amount of false positives you are seeing?
  • What are the min and max injected dependencies you’re using?

Cheers,
Gabriel

Hi Gabriel,

Here are the statistics I pulled for this issue within our organization (answering for Sean).

  • 400+ logged code smells with this issue in the constructor
  • 20+ separate repos impacted
  • minimum of 0 services and a maximum of 30+ services being injected into constructors in the Angular repos (most constructors typically have around 1 to 5 as a norm)
  • Outlier of around 30+ injected services in one of the constructors (this of course likely needs to be refactored)
1 Like

Hi Ryan,

Thank you for taking the time to put together the stats.
We also gathered some internal stats that confirm your findings.

This will be part of our next batch of improvements early next week.
It will be released on SonarCloud and on the upcoming SonarQube 9.6.

Thanks again for your great help :+1:

See you around,
Gabriel

1 Like