softshipper
softshipper

Reputation: 34081

Use getter and setter

Is is recommended to use in dart getter and setter to define a property like:

class Car {
  Engine engine;

  bool get isEngineRunning => engine.isRunning;

  void set isEngineRunning(bool isRunning) {
    engine.isRunning = isRunning;
  }
}

And what is advantage of it?

Upvotes: 0

Views: 1799

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657406

Only if it's required because you want to execute some additional code except forwarding to a field.

If the getters and setters are only used to wrap a field, the getters/setters are explicitly discouraged because they are just redundant.

In your case, because you are not forwarding to a field _isEngineRunning it's fine if you don't want to expose Engine engine.

However because engine is public it might be just too much noise and confusing because the same thing can be done in two different ways.

If engine would be private it would be better considering https://en.wikipedia.org/wiki/Law_of_Demeter

Upvotes: 3

Related Questions