Skip to content

Files

Latest commit

 

History

History
37 lines (27 loc) · 707 Bytes

avoid_setters_without_getters.md

File metadata and controls

37 lines (27 loc) · 707 Bytes

Pattern: Use of setter without getter

Issue: -

Description

Defining a setter without defining a corresponding getter can lead to logical inconsistencies. Doing this could allow you to set a property to some value, but then upon observing the property's value, it could easily be different.

Example of incorrect code:

class Bad {
 int l, r;

 set length(int newLength) {
  r = l + newLength;
 }
}

Example of correct code:

class Good {
 int l, r;

 int get length => r - l;

 set length(int newLength) {
  r = l + newLength;
 }
}

Further Reading