Class AvoidConstantAsFirstOperandInConditionCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class AvoidConstantAsFirstOperandInConditionCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
    Checks that constants do not appear in the first operand of any condition.

    If comparing values, C(C++) developers prefer to put the constant first in the equality check, to prevent situations of assignment rather than equality checking. It is easy to write "=" instead of "==", and no compile error will be produced but condition will work in a different way then intended. However, in Java it is impossible to use assignment inside the if condition, so that habit becomes unnecessary and does damage to the readability of the code.

    This check was extended to include all equality checks like ">", ">=", "<", "<=" for users who prefer constants always be on the right-hand side for any condition.

    Example: if (null == variable) rather than if (variable == null) because if you forget one (typo mistake) of the equal sign, you end up with if (variable = null) which assigns null to variable and IF always evaluate to true.

    Since:
    1.9.0
    Author:
    Sergey Burtsev
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      A key is pointing to the warning message text in "messages.properties" file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setTargetConstantTypes​(String... targets)
      Set target constant types.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST detailAST)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Field Detail

      • MSG_KEY

        public static final String MSG_KEY
        A key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
    • Constructor Detail

      • AvoidConstantAsFirstOperandInConditionCheck

        public AvoidConstantAsFirstOperandInConditionCheck()
    • Method Detail

      • setTargetConstantTypes

        public void setTargetConstantTypes​(String... targets)
        Set target constant types.
        Parameters:
        targets - target constant types
      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST detailAST)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck