AbbreviationAsWordInName

Since Checkstyle 5.8

Description

Validates abbreviations (consecutive capital letters) length in identifier name, it also allows to enforce camel case naming. Please read more at Google Style Guide to get to know how to avoid long abbreviations in names.

'_' is considered as word separator in identifier name.

allowedAbbreviationLength specifies how many consecutive capital letters are allowed in the identifier. A value of 3 indicates that up to 4 consecutive capital letters are allowed, one after the other, before a violation is printed. The identifier 'MyTEST' would be allowed, but 'MyTESTS' would not be. A value of 0 indicates that only 1 consecutive capital letter is allowed. This is what should be used to enforce strict camel casing. The identifier 'MyTest' would be allowed, but 'MyTEst' would not be.

ignoreFinal, ignoreStatic, and ignoreStaticFinal control whether variables with the respective modifiers are to be ignored. Note that a variable that is both static and final will always be considered under ignoreStaticFinal only, regardless of the values of ignoreFinal and ignoreStatic. So for example if ignoreStatic is true but ignoreStaticFinal is false, then static final variables will not be ignored.

Properties

name description type default value since
allowedAbbreviationLength Indicate the number of consecutive capital letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ... ). int 3 5.8
allowedAbbreviations Specify abbreviations that must be skipped for checking. String[] {} 5.8
ignoreFinal Allow to skip variables with final modifier. boolean true 5.8
ignoreOverriddenMethods Allow to ignore methods tagged with @Override annotation (that usually mean inherited name). boolean true 5.8
ignoreStatic Allow to skip variables with static modifier. boolean true 5.8
ignoreStaticFinal Allow to skip variables with both static and final modifiers. boolean true 8.32
tokens tokens to check subset of tokens CLASS_DEF , INTERFACE_DEF , ENUM_DEF , ANNOTATION_DEF , ANNOTATION_FIELD_DEF , PARAMETER_DEF , VARIABLE_DEF , METHOD_DEF , ENUM_CONSTANT_DEF , PATTERN_VARIABLE_DEF , RECORD_DEF , RECORD_COMPONENT_DEF . CLASS_DEF , INTERFACE_DEF , ENUM_DEF , ANNOTATION_DEF , ANNOTATION_FIELD_DEF , PARAMETER_DEF , VARIABLE_DEF , METHOD_DEF , PATTERN_VARIABLE_DEF , RECORD_DEF , RECORD_COMPONENT_DEF . 5.8

Examples

To configure the check:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AbbreviationAsWordInName"/>
  </module>
</module>

Example:


class Example1 extends SuperClass { // ok, camel case
  int CURRENT_COUNTER;  // violation 'no more than '4' consecutive capital letters'

  static int GLOBAL_COUNTER;        // ok, static is ignored

  final Set<String> stringsFOUND = new HashSet<>(); // ok, final is ignored
  int firstNum;
  int secondNUM;
  static int thirdNum;
  static int fourthNUm;
  String firstXML;
  String firstURL;
  final int TOTAL = 5;              // ok, final is ignored
  static final int LIMIT = 10;      // ok, static final is ignored
  int nextXYZ = 1;
  final int countID = 2;

  static int nextID = 3;
  static final int MAX_ALLOWED = 4; // ok, static final is ignored

  void newOAuth2Client() {}
  void OAuth2() {}
  void OAUth2() {}

  @Override
  public void printCOUNTER() {}     // ok, overridden method is ignored
  // violation below 'no more than '4' consecutive capital letters'
  void incrementCOUNTER() {}
  // violation below 'no more than '4' consecutive capital letters'
  static void incrementGLOBAL() {}
}

To configure to include static variables and methods tagged with @Override annotation.

Configuration:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AbbreviationAsWordInName">
      <property name="ignoreStatic" value="false"/>
      <property name="ignoreOverriddenMethods" value="false"/>
    </module>
  </module>
</module>

Example:


class Example2 extends SuperClass { // ok, camel case
  int CURRENT_COUNTER;  // violation 'no more than '4' consecutive capital letters'
  // violation below 'no more than '4' consecutive capital letters'
  static int GLOBAL_COUNTER;

  final Set<String> stringsFOUND = new HashSet<>(); // ok, final is ignored
  int firstNum;
  int secondNUM;
  static int thirdNum;
  static int fourthNUm;
  String firstXML;
  String firstURL;
  final int TOTAL = 5;              // ok, final is ignored
  static final int LIMIT = 10;      // ok, static final is ignored
  int nextXYZ = 1;
  final int countID = 2;

  static int nextID = 3;
  static final int MAX_ALLOWED = 4; // ok, static final is ignored

  void newOAuth2Client() {}
  void OAuth2() {}
  void OAUth2() {}

  @Override // violation below 'no more than '4' consecutive capital letters'
  public void printCOUNTER() {}
  // violation below 'no more than '4' consecutive capital letters'
  void incrementCOUNTER() {}
  // violation below 'no more than '4' consecutive capital letters'
  static void incrementGLOBAL() {}
}

To configure to check all variables and identifiers (including ones with the static modifier) and enforce no abbreviations (essentially camel case) except for words like 'XML' and 'URL'.

Configuration:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AbbreviationAsWordInName">
      <property name="allowedAbbreviationLength" value="0"/>
      <property name="allowedAbbreviations" value="XML,URL,O"/>
      <property name="ignoreStatic" value="false"/>
      <property name="tokens" value="VARIABLE_DEF,CLASS_DEF"/>
    </module>
  </module>
</module>

Example:


class Example3 extends SuperClass {
  int CURRENT_COUNTER;  // violation 'no more than '1' consecutive capital letters'
  // violation below 'no more than '1' consecutive capital letters'
  static int GLOBAL_COUNTER;

  final Set<String> stringsFOUND = new HashSet<>(); // ok, final is ignored
  int firstNum;
  int secondNUM;        // violation 'no more than '1' consecutive capital letters'
  static int thirdNum;  // ok, the static modifier would be checked
  static int fourthNUm; // violation 'no more than '1' consecutive capital letters'
  String firstXML;      // ok, XML abbreviation is allowed
  String firstURL;      // ok, URL abbreviation is allowed
  final int TOTAL = 5;              // ok, final is ignored
  static final int LIMIT = 10;      // ok, static final is ignored
  int nextXYZ = 1;      // violation 'no more than '1' consecutive capital letters'
  final int countID = 2;            // ok, final is ignored
  // violation below 'no more than '1' consecutive capital letters'
  static int nextID = 3;
  static final int MAX_ALLOWED = 4; // ok, static final is ignored

  void newOAuth2Client() {} // ok, O abbreviation is allowed
  void OAuth2() {}          // ok, O abbreviation is allowed
  void OAUth2() {}

  @Override
  public void printCOUNTER() {}

  void incrementCOUNTER() {}

  static void incrementGLOBAL() {}
}

To configure to check variables, excluding fields with the static modifier, and allow abbreviations up to 2 consecutive capital letters ignoring the longer word 'CSV'.

Configuration:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AbbreviationAsWordInName">
      <property name="allowedAbbreviationLength" value="1"/>
      <property name="allowedAbbreviations" value="CSV"/>
      <property name="ignoreStatic" value="true"/>
      <property name="tokens" value="VARIABLE_DEF"/>
    </module>
  </module>
</module>

Example:


class Example4 { // ok, ignore checking the class name
  int firstNum; // ok, abbreviation "N" is of allowed length 1
  int secondNUm;
  int secondMYNum; // violation 'no more than '2' consecutive capital letters'
  int thirdNUM; // violation 'no more than '2' consecutive capital letters'
  static int fourthNUM; // ok, variables with static modifier would be ignored
  String firstCSV; // ok, CSV abbreviation is allowed
  String firstXML; // violation 'no more than '2' consecutive capital letters'
  final int TOTAL = 5; // ok, final is ignored
  static final int LIMIT = 10; // ok, static final is ignored
}

To configure to check variables, enforcing no abbreviations except for variables that are both static and final.

Configuration:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AbbreviationAsWordInName">
      <property name="allowedAbbreviationLength" value="0"/>
      <property name="ignoreFinal" value="false"/>
      <property name="ignoreStatic" value="false"/>
      <property name="ignoreStaticFinal" value="true"/>
      <property name="tokens" value="VARIABLE_DEF"/>
    </module>
  </module>
</module>

Example:


class Example5 extends SuperClass {
  int CURRENT_COUNTER;  // violation 'no more than '1' consecutive capital letters'
  // violation below 'no more than '1' consecutive capital letters'
  static int GLOBAL_COUNTER;
  // violation below 'no more than '1' consecutive capital letters'
  final Set<String> stringsFOUND = new HashSet<>();
  int firstNum;
  int secondNUM;        // violation 'no more than '1' consecutive capital letters'
  static int thirdNum;
  static int fourthNUm; // violation 'no more than '1' consecutive capital letters'
  String firstXML;      // violation 'no more than '1' consecutive capital letters'
  String firstURL;      // violation 'no more than '1' consecutive capital letters'
  final int TOTAL = 5;  // violation 'no more than '1' consecutive capital letters'
  static final int LIMIT = 10;
  int nextXYZ = 1;       // violation 'no more than '1' consecutive capital letters'
  final int countID = 2; // violation 'no more than '1' consecutive capital letters'

  static int nextID = 3; // violation 'no more than '1' consecutive capital letters'
  static final int MAX_ALLOWED = 4;

  void newOAuth2Client() {} // ok, O abbreviation is allowed
  void OAuth2() {}          // ok, O abbreviation is allowed
  void OAUth2() {}

  @Override
  public void printCOUNTER() {}

  void incrementCOUNTER() {}

  static void incrementGLOBAL() {}
}

To configure to check variables, enforcing no abbreviations and ignoring static (but non-final) variables only.

Configuration:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AbbreviationAsWordInName">
      <property name="allowedAbbreviationLength" value="0"/>
      <property name="ignoreFinal" value="false"/>
      <property name="ignoreStatic" value="true"/>
      <property name="ignoreStaticFinal" value="false"/>
      <property name="tokens" value="VARIABLE_DEF"/>
    </module>
  </module>
</module>

Example:


class Example6 {
  int counterXYZ = 1; // violation 'no more than '1' consecutive capital letters'
  // violation below 'no more than '1' consecutive capital letters'
  final int customerID = 2;
  static int nextID = 3; // ok, ignored
  // violation below 'no more than '1' consecutive capital letters'
  static final int MAX_ALLOWED = 4;
}

To configure to check variables, enforce no abbreviations (essentially camel case) except for words like 'ALLOWED'.

Configuration:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AbbreviationAsWordInName">
      <property name="allowedAbbreviations" value="ALLOWED"/>
      <property name="ignoreStaticFinal" value="false"/>
    </module>
  </module>
</module>

Example:


class Example7 {
  int counterXYZ = 1;
  final int customerID = 2;
  static int nextID = 3;
  static final int MAX_ALLOWED = 4; // ok, abbreviation is allowed
}

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.naming.AbbreviationAsWordInNameCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker