# Replace Assignment with Compound Operator
# 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.
# Tags
# 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
🛠️ Auto-refactor Available
You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:
Need help? Check out our installation guide.
# Properties
Property | Value |
---|---|
Rule ID | ArithmethicAssignment |
First seen in jSparrow version | 1.0.0 |
Minimum Java version | 1.4 |
Remediation cost | 2 min |