Skip to content

Files

Latest commit

 

History

History
73 lines (58 loc) · 1.53 KB

prefer_final_fields.md

File metadata and controls

73 lines (58 loc) · 1.53 KB

Pattern: Missing use of final field

Issue: -

Description

Declaring fields as final when possible is a good practice because it helps avoid accidental reassignments and allows the compiler to do optimizations.

Example of incorrect code:

class BadImmutable {
 var _label = 'hola mundo! BadImmutable'; // LINT
 var label = 'hola mundo! BadImmutable'; // OK
}

Example of incorrect code:

class MultipleMutable {
 var _label = 'hola mundo! GoodMutable', _offender = 'mumble mumble!'; // LINT
 var _someOther; // LINT

 MultipleMutable() : _someOther = 5;

 MultipleMutable(this._someOther);

 void changeLabel() {
  _label= 'hello world! GoodMutable';
 }
}

Example of correct code:

class GoodImmutable {
 final label = 'hola mundo! BadImmutable', bla = 5; // OK
 final _label = 'hola mundo! BadImmutable', _bla = 5; // OK
}

Example of correct code:

class GoodMutable {
 var _label = 'hola mundo! GoodMutable';

 void changeLabel() {
  _label = 'hello world! GoodMutable';
 }
}

Example of incorrect code:

class AssignedInAllConstructors {
 var _label; // LINT
 AssignedInAllConstructors(this._label);
 AssignedInAllConstructors.withDefault() : _label = 'Hello';
}

Example of correct code:

class NotAssignedInAllConstructors {
 var _label; // OK
 NotAssignedInAllConstructors();
 NotAssignedInAllConstructors.withDefault() : _label = 'Hello';
}

Further Reading