Search code examples
javasonarqubenaming-conventions

Sonar Complains about variable declaration..Cant we use "_"?


private String producer_queueName = "cn";

Name 'consumer_queueName' must match pattern '^[a-z][a-zA-Z0-9]*$'.

Whats wrong with using _? Does Java not allow you to use _ for any variable?

I want to use _. How can I get rid of this error:

must match pattern '^[a-z][a-zA-Z0-9]*$'. 

Solution

  • This is just a warning from Sonar telling you that you are not conforming to java standards. In Java, it is standard to use camelCase for private variables. _ should only be used for static final variables.

    For example:

    //Conforming private variable with camelCase
    private String producerQueueName = "cn";
    
    //Conforming private static final variable with "_"
    private static final String PRODUCER_QUEUE_NAME = "cn";
    

    Sonar isn't telling you that you can't do what you're doing, just that you aren't complying with the standards. This might make it harder for someone else to read your code.

    See here for more details about java naming conventions.

    If you wish to disable the rule in Sonar, you will need to disable the rule in the Quality profile you are using. See here for the documentation on how to do that.