dart - Use getter and setter -
is recommended use in dart getter , setter define property like:
class car { engine engine; bool isenginerunning => engine.isrunning; void set isenginerunning(bool isrunning) { engine.isrunning = isrunning; } }
and advantage of it?
only if it's required because want execute additional code except forwarding field.
if getters , setters used wrap field, getters/setters explicitly discouraged because redundant.
in case, because not forwarding field _isenginerunning
it's fine if don't want expose engine engine
.
however because engine
public might noise , confusing because same thing can done in 2 different ways.
if engine
private better considering https://en.wikipedia.org/wiki/law_of_demeter
Comments
Post a Comment