Getters and setters are evil. No need to argue about this, it's settled. You disagree? Let's discuss that later. For now, let's say, we want to get rid of getters. The key question is how is it possible at all? We do need to get the data out of an object, right? Nope. Wrong.
- Posted by: Yegor Bugayenko
- Posted on: April 04 2016 23:56 EDT
I think it's ... understandable that he wants to see objects as self-contained bags of consciousness, but the mindset here is not only impractical in an ecosystem where mutators and accessors are the norm, but it's tragically flawed when you do actually want control. Imagine creating a media object for every case where you're composing data for output - a point of sale, for example, might need a PriceMedia, TitleMedia, AuthorMedia reference -- or to parse out a JSON stream, or a DTO (with accessors, or with public final fields. Of these, the DTO is easily the "lightest," or you could use what is going to be fairly well mandated by the ecosystem to be present anyway.
I like the *idea*, it's fine - but it's also presented as clickbait, and that means whatever point the author has is being drowned in reactions to his over-the-top claims that programming even *can* be "evil."
Just look at what this author writes and do the opposite.
His other proposals are about as stupid as this one.
I think the point is valid from a pure OO perspective, and it is also the "correct" was to interpret things, however I think it has a pragmatic reason, why this getter/setter style is universal. On one side it has a historic reason, and is embedded so deeply in almost any programming language and the programming culture, that it would be a hell of a change. On the other hand, it can become a maintenance nightmare once your decent sized domain model changes, and does this often.