Domanda

I am using PMD for checking java coding violation. I am writing automation test cases, and given method name like this:

public void testCaseMethod_4_2_16(){
   //some implementation 
}

where 4_2_16 is test case number 4.2.16

and when I check for PMD violation it is showing method name should not contain underscore, and this violation is shown as blocker according to PMD rules. So here are my question:

  • Why method name should not contain underscore is define as a blocker or PMD error?
  • What kind of PMD violation we should avoid and
  • What kind of violation we should try to fixed?

As for my understanding at least we should avoid first two level of violation shown by PMD. Any help on the PMD rule will be appreciated. thanks

È stato utile?

Soluzione

It is a standard that has been set so that people can easily read each other's code, therefore making code more maintainable.

The quote below is from Oracle's website on Java code conventions:

  • 80% of the lifetime cost of a piece of software goes to maintenance.

  • Hardly any software is maintained for its whole life by the original author.

  • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.

Altri suggerimenti

Java prefers camelCase notation. Java code conventions say that you should try to avoid underscores in method or variable name whenever it is possible.

In your case PMD's default prioritization fails. This rule should be on a "nice-to-have" level, but definitely not a blocker issue.

I would recommend you to use SonarQube, which did a re-prioritization of PMD's rules, which works better is most cases. The rule you found is just on Major level (below levels Blocker and Critical).

Naming Conventions rule in Sonar

If you are still not satisfied with the results, you can use the SourceMeter plugin for SonarQube, which has a different (possible better) prioritization of PMD rules as well.

Although there are lots of opinions out there, I like to separate quality-assurance for production and the one for test code.

If that is not possible for whatever reason, I'd rather deactivate (most of) the warnings for test-code. I think it is not worth the time making the test-code pretty (and underscores in method names is just styling to me), while there are serious warnings for the production code left open.

In java underscore is allowed while naming identifiers. I don't know why PMD is showing violation but you can just deactivate the warnings and go on. UNDERSCORE is allowed in java while naming identifiers.

I think it may help you...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top