Are “getters and setters evil” failing for the presentation layer?

Many people know this article: more about getters and setters . I think this is convincing work on portraying the evil side of getters / setters. I also tested it, trying to convert an existing project (incomplete) into code without getters / seters. It worked. The readability of the code has improved significantly, the code has decreased, and I even managed to get rid of getters / setters, where I initially thought that they were really necessary. Except for one place.

Getting models in the presentation part is where I think this approach is not suitable. In the article, the author uses the builder to export the model. The problem is that there is as much control over what is placed in the builder as what you would get with getters. Yes, it hides the implementation, the way it is represented in the model. But getters do not throw something very different from the model from what was there. If you create a "Money" object that passes "5" through the constructor, money.getAmount () will not return this conversion to any other currency or as an array with one "5" element in it.

What you installed, you get. And through the view, we set the values ​​and those values ​​that we expect when we ask them (to receive) from the object, which should hold what we set in the first place. The builder simply expects the same to export them.

This is a little long question. But I would like to be challenged in my opinion. Are getters and setters evil for transferring model data to the presentation layer?

There are many people who think that getters / setters are not evil at all. This is also not what I would like to hear, protected, as I think they are NOT Evil in other places than those that I spoke of.

+4
source share
2 answers

For very simple cases, the data object does not have any behavior for encapsulation, so arguments based on improving encapsulation of behavior do not really apply.

Most of the views I built were related to events. In an event-driven view, you register for a change event in the model and update the view when the model changes, instead of passing the "model" around and getting the value of each attribute, and then updating if its state has changed. Given that the event mechanism allows the model to push its state to the view, you do not need getters to represent it to pull out the state (and if the model is also a listener, you do not need assemblers). If you change only one attribute in a model with thousands of attributes, how well will the builder work and transfer the new model to the view?

If instead of thinking of the model as a data globe, but instead think of it as a cache implementation when sending state notification events from the linker / persistence level to listeners / views, then it's easy to see how it has behavior that can be encapsulated rather than being a purely state that can be interviewed.

+3
source

There is an alternative model used in Scala, but which can really be used anywhere. This is a constructor / extractor model. A constructor is just a class constructor. An extractor is a method that, when called, returns the parameters that are passed to the constructor, creating a clone of this object.

For dynamic languages, you simply return the list and execute it. For statically typed languages, you can go to the list of objects that must then be processed so that each type can be correctly assigned, or you must have a tuple type with a parameter so that you can return each parameter with the correct type.

In a specific case of Scala, its Extractors are similar to static Java methods, and they receive the object as input. It either returns parameters or returns None , which performs an analog analogue with Java null .

The idea is that you can decompose what you have created, which can pretty much have an idea.

Now, another concept that you might have is that “tell, don't ask” intends to support business rules with the data to which it relates. Now the business rules for the presentation are necessarily related to the presentation, so you have a problem with querying the data model ... if you do not invert the game. A model can display a view to display data by passing them the appropriate fields, rather than requesting them. Thus, the model tells the view to display data, and the view tells the model about its update.

+1
source

All Articles