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");
    }
    // violation below 'Assignment of parameter 'parameter' is not allowed.'
    parameter -= 2;
    return parameter;
  }

  int methodTwo(int parameter) {
    if (parameter <= 0 ) {
      throw new IllegalArgumentException("A positive value is expected");
    }
    int local = parameter;
    local -= 2;
    return local;
  }
  // violation below 'Assignment of parameter 'a' is not allowed.'
  IntPredicate obj = a -> ++a == 12;
  IntBinaryOperator obj2 = (int a, int b) -> {
    a++;     // violation 'Assignment of parameter 'a' is not allowed.'
    b += 12; // violation 'Assignment of parameter 'b' is not allowed.'
    return a + b;
  };
  IntPredicate obj3 = a -> {
    int b = a;
    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