# 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
Use a Java Refactoring Tool
No license required
You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.
System-wide Refactoring
Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.
# Properties
Property | Value |
---|---|
Rule ID | ArithmethicAssignment |
First seen in jSparrow version | 1.0.0 |
Minimum Java version | 1.4 |
Remediation cost | 2 min |