ParameterAssignment

Since Checkstyle 3.2

Description

Disallows assignment of parameters.

Rationale: Parameter assignment is often considered poor programming practice. Forcing developers to declare parameters as final is often onerous. Having a check ensure that parameters are never assigned would give the best of both worlds.

Examples

To configure the check:

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

Example:

class Example1 {
  int methodOne(int parameter) {
    if (parameter <= 0 ) {
      throw new IllegalArgumentException("A positive value is expected");
    }
    parameter -= 2;  // violation
    return parameter;
  }

  int methodTwo(int parameter) {
    if (parameter <= 0 ) {
      throw new IllegalArgumentException("A positive value is expected");
    }
    int local = parameter;
    local -= 2;  // OK
    return local;
  }

  IntPredicate obj = a -> ++a == 12; // violation
  IntBinaryOperator obj2 = (int a, int b) -> {
    a++;     // violation
    b += 12; // violation
    return a + b;
  };
  IntPredicate obj3 = a -> {
    int b = a; // ok
    return ++b == 12;
  };
}
        

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.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Parent Module

TreeWalker