Class TernaryPerExpressionCountCheck

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

    public class TernaryPerExpressionCountCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
    Restricts the number of ternary operators in expression to a specific limit.

    Rationale: This Check helps to improve code readability by pointing developer on
    expressions which contain more than user-defined count of ternary operators.

    It points to complicated ternary expressions. Reason:
    - Complicated ternary expressions are not easy to read.
    - Complicated ternary expressions could lead to ambiguous result if user
    does not know Java's operators priority well, e.g.:
     String str = null;
     String x = str != null ? "A" : "B" + str == null ? "C" : "D";
     System.out.println(x);
     

    Output for code above is "D", but more obvious would be "BC".

    Check has following properties:

    • maxTernaryPerExpressionCount - limit of ternary operators per expression
    • ignoreTernaryOperatorsInBraces - if true Check will ignore ternary operators
      in braces (braces explicitly set priority level)
    • ignoreIsolatedTernaryOnLine - if true Check will ignore one line ternary operators,
      if only it is places in line alone.
    Options ignoreTernaryOperatorsInBraces and ignoreIsolatedTernaryOnLine can
    make Check less strict, e.g.:
    Using ignoreTernaryOperatorsInBraces option (value = true)
    does not put violation on code below:
     callString = "{? = call " +
       (StringUtils.hasLength(catalogNameToUse)
       ? catalogNameToUse + "." : "") +
       (StringUtils.hasLength(schemaNameToUse)
       ? schemaNameToUse + "." : "") +
       procedureNameToUse + "(";
     

    When using ignoreIsolatedTernaryOnLine (value = true), even without
    ignoreTernaryOperatorsInBraces option Check won't warn on code below:

     int a = (d == 5) ? d : f
       +
       ((d == 6) ? g : k);
     
    Since:
    1.12.0
    Author:
    Aleksey Nesterenko
    • 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 setIgnoreIsolatedTernaryOnLine​(boolean ignoreIsolatedTernaryOnLine)
      Sets parameter to ignore expressions in case if ternary operator is isolated in line.
      void setIgnoreTernaryOperatorsInBraces​(boolean ignoreTernaryOperatorsInBraces)
      Sets parameter to ignore ternary operators in braces, default value = true.
      void setMaxTernaryPerExpressionCount​(int maxTernaryPerExpressionCount)
      Sets the maximum number of ternary operators, default value = 1.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST expressionNode)  
      • 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

      • TernaryPerExpressionCountCheck

        public TernaryPerExpressionCountCheck()
    • Method Detail

      • 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
      • setMaxTernaryPerExpressionCount

        public void setMaxTernaryPerExpressionCount​(int maxTernaryPerExpressionCount)
        Sets the maximum number of ternary operators, default value = 1.
        Parameters:
        maxTernaryPerExpressionCount - Number of ternary operators per expression
        Throws:
        IllegalArgumentException - when maxTernaryPerExpressionCount less zero
      • setIgnoreTernaryOperatorsInBraces

        public void setIgnoreTernaryOperatorsInBraces​(boolean ignoreTernaryOperatorsInBraces)
        Sets parameter to ignore ternary operators in braces, default value = true.
        Parameters:
        ignoreTernaryOperatorsInBraces - ignore ternary operators in braces
      • setIgnoreIsolatedTernaryOnLine

        public void setIgnoreIsolatedTernaryOnLine​(boolean ignoreIsolatedTernaryOnLine)
        Sets parameter to ignore expressions in case if ternary operator is isolated in line.
        Parameters:
        ignoreIsolatedTernaryOnLine - ignore expressions in case if ternary operator is isolated in line
      • visitToken

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