Why Getters & Setters Aren't OOP, Use This Technic Instead

How Yegor Bugayenko's concept of printers finally achieve proper OOP encapsulation

Arnold Abraham
CodeX
Published in
4 min readDec 1, 2021

--

Image made by the author with Canva.com

Getters and Setters are common practice but fundamentally against OOP.

Allen Holub describes how getters and setters make your code terrible for maintainability. He also states that this overall pattern of using getters and setters is questionable. OOP is all about encapsulation and not revealing inner logic to outer spaces. Guess what? Use getters, and you do exactly spread internals out.

With revealing instead of encapsulating, you create highly dependent parts.

The OOP concept indeed says not to reveal internals, but how is it even possible to work internal data?

1. Getters & Setters Talk Too Much…

An easy start with a class of a CD with two fields of _name and _band. These internals are sealed and can't be accessed from the outside.

Imagine converting this class into a different format (XML, HTML, Binary, etc.). Then you need to make some preparations:

You need to reveal the internals by setting up getters for Name and Band. Only then are you allowed to retrieve their values and pass them to the new element. Alternatively, you make public fields, which leads to the same exposure.

Whatever format you want to implement, the adjustments to get there would be like in preparations.cs.

1 question is rising: Who is responsible for the values of Name and Band?

You can be sure that you will build or already have built algorithms depending on these internal values. Even worse: You make changes to…

--

--

Arnold Abraham
CodeX
Writer for

JavaScript, TypeScript and C#/.NET Tutorials/News/Best Practices by a German Software Engineer - Fun helps you to learn on the fly --> arnoldcode.com