Replace Assignment with Compound Operator

Properties

Property Value
First seen in jSparrow version 1.0.0
Minimum Java version 1.4
Remediation cost 2 min

Description

Assignments involving an arithmetic assignment can be simplified by using a compound operator such as +=, -=, /= or *=. For example, this rule will transform a=a+1 into a+=1.
The rule only applies if both operands are primitive types.

Benefits

Applying this rule leads to more simplicity in the code base and thus improves readability.

Requirement & Tags

Requirements

Java 1.4

Code Changes

Operator +=

Pre

i = i + 3;
i = 3 + i;
i = i + (3 + 4);

Post

i += 3;
i += 3;
i += (3 + 4);

Operator -=

Pre

i = i - 3;
i = -3 + i;
i = i - (3 + 4);

Post

i -= 3;
i += -3;
i -= (3 + 4);

Operator *=

Pre

i = i * 3;
i = i * (3 + 4);

Post

i *= 3;
i *= (3 + 4);

Operator /=

Pre

i = i / 3;
i = i / (3 + 4);

Post

i /= 3;
i /= (3 + 4);

Limitations

Rule can be applied only on the following types:

  • int
  • double
  • float
  • long
  • short