How to specify exceptions for PascalCase naming rule in SonarLint

Viewed 267

According to RSPEC-101, types should be named in PascalCase. I use the same convention but a lot of types in my codebase have acronyms in their name (e.g. ABCBase, ABCSomeName, ABSSomeOtherName, and so on). Is there any way to configure SonarLint in Visual Studio to treat "ABC" as an acronym and accept ABCSomeName as a valid type name?

3 Answers

usually it's a bad idea to prefix the name of a class (as is mentioned in clean code)

Don’t add gratuitous context

In an imaginary application called “Foo Solution” it is a bad idea to prefix every class with FS. Frankly, you are working against your tools. You type ‘F’ and press the completion key and are bombarded with a mile-long list of every class in the system.

https://medium.com/mindorks/meaningful-names-a-dimension-of-writing-clean-code-fdae1ae4f0b1

I found this issue on github: https://github.com/SonarSource/sonar-dotnet/issues/273

Snippets from the page (dated 2018, snipped in 2022):

Is there a way of adding exceptions to this rule? For example, I would like that GMTService not fail this rule as "GMT" is a keyword which I want to ignore.

And

There is no such feature of exclusion list but depending on your usage, you can either: if paired with SonarQube/SonarCloud mark the issue as False Positive or Won't Fix on the server side if you are only using the analyzer, exclude the issue using a pragma

It's common to downgrade acronyms and initialisms to only have the first character in uppercase when doing PascalCase symbols.

So NMEA in GetNMEAPosition would then be GetNmeaPosition.

This convention not only solves the problem with sonarlint S101 matching, but also makes PascalCase symbols easier to read - acronyms works well on their own, but not that well with PascalCase where the next word adds one more uppercase character.

This is Microsoft's view on spelling acronyms longer than 2 characters into camelcase: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-1.1/141e06ef(v=vs.71)?redirectedfrom=MSDN

  • When using acronyms, use Pascal case or camel case for acronyms more than two characters long. For example, use HtmlButton or htmlButton. However, you should capitalize acronyms that consist of only two characters, such as System.IO instead of System.Io.
  • Do not use abbreviations in identifiers or parameter names. If you must use abbreviations, use camel case for abbreviations that consist of more than two characters, even if this contradicts the standard abbreviation of the word.

And a Google symbol name style guide: https://google.github.io/styleguide/jsguide.html

§6.3 Camel case:

  1. Convert the phrase to plain ASCII and remove any apostrophes. For example, Müller's algorithm might become Muellers algorithm.
  2. Divide this result into words, splitting on spaces and any remaining punctuation (typically hyphens).
    • Recommended: if any word already has a conventional camel case appearance in common usage, split this into its constituent parts (e.g., AdWords becomes ad words). Note that a word such as iOS is not really in camel case per se; it defies any convention, so this recommendation does not apply.
  3. Now lowercase everything (including acronyms), then uppercase only the first character of:
    • … each word, to yield upper camel case, or
    • … each word except the first, to yield lower camel case
  4. Finally, join all the words into a single identifier.
Related