Skip to content

Files

Latest commit

 

History

History
65 lines (54 loc) · 1.15 KB

prefer_initializing_formals.md

File metadata and controls

65 lines (54 loc) · 1.15 KB

Pattern: Missing use of initializing formal

Issue: -

Description

Using initializing formals when possible makes your code more terse.

Example of incorrect code:

class Point {
 num x, y;
 Point(num x, num y) {
  this.x = x;
  this.y = y;
 }
}

Example of correct code:

class Point {
 num x, y;
 Point(this.x, this.y);
}

Example of incorrect code:

class Point {
 num x, y;
 Point({num x, num y}) {
  this.x = x;
  this.y = y;
 }
}

Example of correct code:

class Point {
 num x, y;
 Point({this.x, this.y});
}

NOTE This rule will not generate a lint for named parameters unless the parameter name and the field name are the same. The reason for this is that resolving such a lint would require either renaming the field or renaming the parameter, and both of those actions would potentially be a breaking change. For example, the following will not generate a lint:

class Point {
 bool isEnabled;
 Point({bool enabled}) {
  this.isEnabled = enable; // OK
 }
}

Further Reading