Skip to content

Files

Latest commit

 

History

History
50 lines (40 loc) · 845 Bytes

prefer_typing_uninitialized_variables.md

File metadata and controls

50 lines (40 loc) · 845 Bytes

Pattern: Missing type annotation for uninitialized variable/field

Issue: -

Description

Forgoing type annotations for uninitialized variables is a bad practice because you may accidentally assign them to a type that you didn't originally intend to.

Example of incorrect code:

class BadClass {
 static var bar; // LINT
 var foo; // LINT

 void method() {
  var bar; // LINT
  bar = 5;
  print(bar);
 }
}

Example of incorrect code:

void aFunction() {
 var bar; // LINT
 bar = 5;
 ...
}

Example of correct code:

class GoodClass {
 static var bar = 7;
 var foo = 42;
 int baz; // OK

 void method() {
  int baz;
  var bar = 5;
  ...
 }
}

Further Reading