Skip to content

Files

Latest commit

 

History

History
38 lines (30 loc) · 751 Bytes

prefer_final_locals.md

File metadata and controls

38 lines (30 loc) · 751 Bytes

Pattern: Missing use of final for variable

Issue: -

Description

Declaring variables 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:

void badMethod() {
 var label = 'hola mundo! badMethod'; // LINT
 print(label);
}

Example of correct code:

void goodMethod() {
 final label = 'hola mundo! goodMethod';
 print(label);
}

Example of correct code:

void mutableCase() {
 var label = 'hola mundo! mutableCase';
 print(label);
 label = 'hello world';
 print(label);
}

Further Reading