简体   繁体   English

为什么java方法名称不应包含下划线?

[英]Why java method name should not contain underscore?

I am using PMD for checking java coding violation. 我正在使用PMD来检查java编码违规。 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 其中4_2_16是测试用例编号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. 当我检查PMD违规时,它显示的方法名称不应包含下划线 ,并且根据PMD规则,此违规将显示为阻止程序 So here are my question: 所以这是我的问题:

  • Why method name should not contain underscore is define as a blocker or PMD error ? 为什么方法名称不应包含下划线被定义为阻止程序PMD错误
  • What kind of PMD violation we should avoid and 我们应该避免什么样的PMD违规
  • 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. 至于我的理解,至少我们应该避免PMD显示的前两级违规。 Any help on the PMD rule will be appreciated. 有关PMD规则的任何帮助将不胜感激。 thanks 谢谢

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 : 以下引用来自Oracle的Java代码约定网站:

  • 80% of the lifetime cost of a piece of software goes to maintenance. 一个软件的80%的终身成本用于维护。

  • 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. 代码约定提高了软件的可读性,使工程师能够更快,更彻底地理解新代码。

Java prefers camelCase notation. Java更喜欢camelCase表示法。 Java code conventions say that you should try to avoid underscores in method or variable name whenever it is possible. Java代码约定表明,只要可能,您应该尽量避免使用方法或变量名中的下划线。

In your case PMD's default prioritization fails. 在您的情况下,PMD的默认优先级失败。 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. 我建议你使用SonarQube ,它重新确定了PMD规则的优先级,大多数情况下效果更好。 The rule you found is just on Major level (below levels Blocker and Critical). 您找到的规则仅在主要级别(低于Blocker和Critical级别)。

声纳中的命名约定规则

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. 如果您对结果仍然不满意,可以使用SonarQubeSourceMeter插件 ,它也有不同的(可能更好的)PMD规则优先级。

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. 在命名标识符时,允许使用java下划线。 I don't know why PMD is showing violation but you can just deactivate the warnings and go on. 我不知道为什么PMD显示违规,但您可以停用警告并继续。 UNDERSCORE is allowed in java while naming identifiers. 在命名标识符时允许在java中使用UNDERSCORE。

I think it may help you... 我想它可以帮到你......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:为什么为继承设计的类应包含受保护的克隆方法? - Java: why class designed for inheritance should contain protected clone method? Java中方法名称前的下划线是什么意思? - What is the meaning of underscore before a method name in Java? Java Lambda 表达式:为什么需要引用方法名称? - Java Lambda Expressions: Why should it be required to refer to the method name? 为什么Java持久性模型中的set和get方法应该具有相同的名称? - why does set and get method in java persistence model should have the same name? java包名为什么要小写? - Why should java package name be lowercase? 为什么Java Web应用程序包含一系列带有main()方法的类? - Why would a Java Web application contain a series of classes with a main() method? java无法连接到名称中带有下划线的数据库? - java can't connect to databases with an underscore in the name? 用于在java中检查带下划线的字符串名称的正则表达式 - Regex expression to check for string name with underscore in java 为什么java ClassLoader中没有unloadClass(String name)方法 - Why is there no unloadClass(String name) method in java ClassLoader Java - 方法声明中的类名,返回类型应该是 - Java - class name in method declaration where the return type should be
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM