Skip to content

Files

Latest commit

 

History

History
113 lines (83 loc) · 1.18 KB

no_self_assignments.md

File metadata and controls

113 lines (83 loc) · 1.18 KB

Pattern: Use of self-assignment

Issue: -

Description

DON'T assign a variable to itself. Usually this is a mistake.

Example of incorrect code:

class C {
 int x;

 C(int x) {
  x = x;
 }
}

Example of correct code:

class C {
 int x;

 C(int x) : x = x;
}

Example of correct code:

class C {
 int x;

 C(int x) {
  this.x = x;
 }
}

Example of incorrect code:

class C {
 int _x = 5;

 int get x => _x;

 set x(int x) {
  _x = x;
  _customUpdateLogic();
 }

 void _customUpdateLogic() {
  print('updated');
 }

 void example() {
  x = x;
 }
}

Example of correct code:

class C {
 int _x = 5;

 int get x => _x;

 set x(int x) {
  _x = x;
  _customUpdateLogic();
 }

 void _customUpdateLogic() {
  print('updated');
 }

 void example() {
  _customUpdateLogic();
 }
}

Example of incorrect code:

class C {
 int x = 5;

 void update(C other) {
  this.x = this.x;
 }
}

Example of correct code:

class C {
 int x = 5;

 void update(C other) {
  this.x = other.x;
 }
}

Further Reading