Class AvoidInlineConditionalsCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class AvoidInlineConditionalsCheck
    extends AbstractCheck

    Detects inline conditionals. Here is one example of an inline conditional:

     String a = getParameter("a");
     String b = (a==null || a.length()<1) ? null : a.substring(1);
     

    Rationale: Some developers find inline conditionals hard to read, so their employer's coding standards forbid them.

    To configure the check:

     <module name="AvoidInlineConditionals"/>
     

    Example:

     int x = 5;
     boolean foobar = (x == 5); // OK
    
     String text;
     text = (text == null) ? "" : text; // violation
    
     String b;
     if (a != null && a.length() >= 1) { // OK
       b = a.substring(1);
     } else {
       b = null;
     }
    
     b = (a != null && a.length() >= 1) ? a.substring(1) : null; // violation
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation Message Keys:

    • inline.conditional.avoid
    Since:
    3.1