Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I avoid wrapping fields in getters and setters?

Tags:

flutter

dart

I am building a Flutter app. I have a class that looks like this:

class ToDo {
    String _title;
    bool _done;

    String get title => _title;
    void set title(String newTitle) { _title = newTitle; }

    bool get _done => _done
    void set done(bool done) { _done = done; }
}

But the Dart linter is complaining that I should "Avoid wrapping fields in getters and setters just to be safe". However, this doesn't make much sense to me. Right now the getters are useless, but what if in the future I need to do some kind of processing before accessing a variable from outside? All I'll have to do is update the getters. However, if the properties were public and being accessed directly, I would have to update the whole codebase if some business rule changed.

So what is the point of this warning? Or, in other words, why creating "useless getters" would be a bad practice?

like image 380
Allan Juan Avatar asked May 10 '20 23:05

Allan Juan


People also ask

How do you avoid getters and setters?

The simplest way to avoid setters is to hand the values to the constructor method when you new up the object. This is also the usual pattern when you want to make an object immutable.

Should getters and setters be tested?

Properties (getters/setters in Java) are good examples of code that usually doesn't contain any logic, and doesn't require testing.

What's the advantage of using getters and setters that only get and set instead of simply using public fields for those variables?

The getter and setter method gives you centralized control of how a certain field is initialized and provided to the client, which makes it much easier to verify and debug. To see which thread is accessing and what values are going out, you can easily place breakpoints or a print statement.

What will happen if setters and getters are made private?

The private getter/setter methods provide a place for adding extra behavior or error checking code. They can provide a place for logging state changes or access to the fields.


1 Answers

However, if the properties were public and being accessed directly, I would have to update the whole codebase if some business rule changed.

Ask yourself this: what exactly would you need to change in your Dart codebase if you had to change a public member to use an explicit getter and setter instead?

In most languages, getters and setters look like method calls to consumers. If you wanted to replace a public data member with a public getter and setter, that would be a breaking API change, requiring changes to everything that uses it.

Dart is not like that. Getters and setters do not look like method calls to consumers; they are indistinguishable from direct member access. (Having a public data member implicitly declares a corresponding getter and setter as part of the class's interface.) Changing a public data member to a public getter and setter would not require any changes to callers, so providing trivial getters and setters around private member variables provides no benefit.

(This is also explained by the documentation for the unnecessary_getters_setters lint that you encountered.)

Incidentally, the unnecessary_getters_setters lint should occur only if you provide both a getter and a setter (which is not what your example code does). If you provide only one, then it would no longer be equivalent to a public data member.

like image 171
jamesdlin Avatar answered Sep 20 '22 16:09

jamesdlin