[Java] Field names should comply with a naming convention - S116

Greetings,

My apologies if this topic is not in the proper section, feel free to move it where it belongs

In my project, I was able to change the Regular expression for this rule but it seems to me that this rule applies to private, protected and public fields, is that correct ?
I would like to have a Regular expression that is different for each type of fields (a specific one for private fields, a different one for public fields…etc), is that possible ?

Thanks in advance
Frederic

Right now, as I understand it, that isn’t possible.

Can you provide some examples of why you want to do this? Specifically, examples of how you would configure this differently for different fields and why.

Right now, as I understand it, that isn’t possible.

Bummer.

Can you provide some examples of why you want to do this? Specifically, examples of how you would configure this differently for different fields and why.

Sure.
For example, our project’s code style is to have all private fields start with "_" while all public/protected filed NOT start with "_"

Thanks

Hey @FJBDev,

I’m just coming back to you about your request from a rule-writing perspective.

Yes, it is correct, the rule implementation completely ignores modifiers of the field (except static), and so you won’t be able to do what you want only using regular expressions.

Currently, the rule is not designed to differentiate fields by visibility and act differently. After discussing the topic internally, we also believe that this is not a practice we want to enforce in general for the Java language. Introducing the concept in the rule would go against the conventions of the language, could be counterintuitive for users, and would introduce complexity in the rule where there is none.

Note that on SonarCloud, you are unfortunately stuck with this, and you probably don’t have another choice than deactivating the rule. Using SonarQube, you might have been able to write your own custom rule in Java, and target both cases. You would then replace S116 with yours in your Quality Profile. This approach also means entering the world of static analysis with us. :slight_smile:

Cheers,
Michael

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.