diff --git a/docs/pages/pmd/rules/java.md b/docs/pages/pmd/rules/java.md index 2ead22586f..d85589ac67 100644 --- a/docs/pages/pmd/rules/java.md +++ b/docs/pages/pmd/rules/java.md @@ -86,6 +86,7 @@ folder: pmd/rules * [IdenticalCatchBranches](pmd_rules_java_codestyle.html#identicalcatchbranches): Identical 'catch' branches use up vertical space and increase the complexity of code without ... * [IfElseStmtsMustUseBraces](pmd_rules_java_codestyle.html#ifelsestmtsmustusebraces): Deprecated Avoid using if..else statements without using surrounding braces. If the code formatting or inden... * [IfStmtsMustUseBraces](pmd_rules_java_codestyle.html#ifstmtsmustusebraces): Deprecated Avoid using if statements without using braces to surround the code block. If the code formatting... +* [LinguisticNaming](pmd_rules_java_codestyle.html#linguisticnaming): This rule finds Linguistic Naming Antipatterns. It checks for fields, that are named, as if they ... * [LocalHomeNamingConvention](pmd_rules_java_codestyle.html#localhomenamingconvention): The Local Home interface of a Session EJB should be suffixed by 'LocalHome'. * [LocalInterfaceSessionNamingConvention](pmd_rules_java_codestyle.html#localinterfacesessionnamingconvention): The Local Interface of a Session EJB should be suffixed by 'Local'. * [LocalVariableCouldBeFinal](pmd_rules_java_codestyle.html#localvariablecouldbefinal): A local variable assigned only once can be declared final. diff --git a/docs/pages/pmd/rules/java/codestyle.md b/docs/pages/pmd/rules/java/codestyle.md index 2950fe7cf0..ce77b11bf7 100644 --- a/docs/pages/pmd/rules/java/codestyle.md +++ b/docs/pages/pmd/rules/java/codestyle.md @@ -5,7 +5,7 @@ permalink: pmd_rules_java_codestyle.html folder: pmd/rules/java sidebaractiveurl: /pmd_rules_java.html editmepath: ../pmd-java/src/main/resources/category/java/codestyle.xml -keywords: Code Style, AbstractNaming, AtLeastOneConstructor, AvoidDollarSigns, AvoidFinalLocalVariable, AvoidPrefixingMethodParameters, AvoidProtectedFieldInFinalClass, AvoidProtectedMethodInFinalClassNotExtending, AvoidUsingNativeCode, BooleanGetMethodName, CallSuperInConstructor, ClassNamingConventions, CommentDefaultAccessModifier, ConfusingTernary, ControlStatementBraces, DefaultPackage, DontImportJavaLang, DuplicateImports, EmptyMethodInAbstractClassShouldBeAbstract, ExtendsObject, FieldDeclarationsShouldBeAtStartOfClass, ForLoopShouldBeWhileLoop, ForLoopsMustUseBraces, FormalParameterNamingConventions, GenericsNaming, IdenticalCatchBranches, IfElseStmtsMustUseBraces, IfStmtsMustUseBraces, LocalHomeNamingConvention, LocalInterfaceSessionNamingConvention, LocalVariableCouldBeFinal, LocalVariableNamingConventions, LongVariable, MDBAndSessionBeanNamingConvention, MethodArgumentCouldBeFinal, MethodNamingConventions, MIsLeadingVariableName, NoPackage, OnlyOneReturn, PackageCase, PrematureDeclaration, RemoteInterfaceNamingConvention, RemoteSessionInterfaceNamingConvention, ShortClassName, ShortMethodName, ShortVariable, SuspiciousConstantFieldName, TooManyStaticImports, UnnecessaryAnnotationValueElement, UnnecessaryConstructor, UnnecessaryFullyQualifiedName, UnnecessaryLocalBeforeReturn, UnnecessaryModifier, UnnecessaryReturn, UselessParentheses, UselessQualifiedThis, VariableNamingConventions, WhileLoopsMustUseBraces +keywords: Code Style, AbstractNaming, AtLeastOneConstructor, AvoidDollarSigns, AvoidFinalLocalVariable, AvoidPrefixingMethodParameters, AvoidProtectedFieldInFinalClass, AvoidProtectedMethodInFinalClassNotExtending, AvoidUsingNativeCode, BooleanGetMethodName, CallSuperInConstructor, ClassNamingConventions, CommentDefaultAccessModifier, ConfusingTernary, ControlStatementBraces, DefaultPackage, DontImportJavaLang, DuplicateImports, EmptyMethodInAbstractClassShouldBeAbstract, ExtendsObject, FieldDeclarationsShouldBeAtStartOfClass, ForLoopShouldBeWhileLoop, ForLoopsMustUseBraces, FormalParameterNamingConventions, GenericsNaming, IdenticalCatchBranches, IfElseStmtsMustUseBraces, IfStmtsMustUseBraces, LinguisticNaming, LocalHomeNamingConvention, LocalInterfaceSessionNamingConvention, LocalVariableCouldBeFinal, LocalVariableNamingConventions, LongVariable, MDBAndSessionBeanNamingConvention, MethodArgumentCouldBeFinal, MethodNamingConventions, MIsLeadingVariableName, NoPackage, OnlyOneReturn, PackageCase, PrematureDeclaration, RemoteInterfaceNamingConvention, RemoteSessionInterfaceNamingConvention, ShortClassName, ShortMethodName, ShortVariable, SuspiciousConstantFieldName, TooManyStaticImports, UnnecessaryAnnotationValueElement, UnnecessaryConstructor, UnnecessaryFullyQualifiedName, UnnecessaryLocalBeforeReturn, UnnecessaryModifier, UnnecessaryReturn, UselessParentheses, UselessQualifiedThis, VariableNamingConventions, WhileLoopsMustUseBraces language: Java --- ## AbstractNaming @@ -1020,6 +1020,87 @@ if (foo) { // preferred approach ``` +## LinguisticNaming + +**Since:** PMD 6.7.0 + +**Priority:** Medium (3) + +This rule finds Linguistic Naming Antipatterns. It checks for fields, that are named, as if they should +be boolean but have a different type. It also checks for methods, that according to their name, should +return a boolean, but don't. Further, it checks, that getters return something and setters won't. +Finally, it checks that methods, that start with "to" - so called transform methods - actually return +something, since according to their name, they should convert or transform one object into another. +There is additionally an option, to check for methods that contain "To" in their name - which are +also transform methods. However, this is disabled by default, since this detection is prone to +false positives. + +For more information, see [Linguistic Antipatterns - What They Are and How +Developers Perceive Them](https://doi.org/10.1007/s10664-014-9350-8). + +**This rule is defined by the following Java class:** [net.sourceforge.pmd.lang.java.rule.codestyle.LinguisticNamingRule](https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/LinguisticNamingRule.java) + +**Example(s):** + +``` java +public class LinguisticNaming { + int isValid; // the field name indicates a boolean, but it is an int. + boolean isTrue; // correct type of the field + + void myMethod() { + int hasMoneyLocal; // the local variable name indicates a boolean, but it is an int. + boolean hasSalaryLocal; // correct naming and type + } + + // the name of the method indicates, it is a boolean, but the method returns an int. + int isValid() { + return 1; + } + // correct naming and return type + boolean isSmall() { + return true; + } + + // the name indicates, this is a setter, but it returns something + int setName() { + return 1; + } + + // the name indicates, this is a getter, but it doesn't return anything + void getName() { + // nothing to return? + } + + // the name indicates, it transforms an object and should return the result + void toDataType() { + // nothing to return? + } + // the name indicates, it transforms an object and should return the result + void grapeToWine() { + // nothing to return? + } +} +``` + +**This rule has the following properties:** + +|Name|Default Value|Description|Multivalued| +|----|-------------|-----------|-----------| +|booleanFieldPrefixes|is \| has \| can \| have \| will \| should|the prefixes of fields and variables that indicate boolean|yes. Delimiter is '\|'.| +|checkVariables|true|Check local variable names and types for inconsistent naming|no| +|checkFields|true|Check field names and types for inconsistent naming|no| +|booleanMethodPrefixes|is \| has \| can \| have \| will \| should|the prefixes of methods that return boolean|yes. Delimiter is '\|'.| +|checkPrefixedTransformMethods|true|Check return type of methods whose names start with 'to'|no| +|checkTransformMethods|false|Check return type of methods which contain 'To' in their name|no| +|checkSetters|true|Check return type of setters|no| +|checkGetters|true|Check return type of getters|no| +|checkBooleanMethod|true|Check method names and types for inconsistent naming|no| + +**Use this rule by referencing it:** +``` xml + +``` + ## LocalHomeNamingConvention **Since:** PMD 4.0