DEV Community

loading...

Discussion on: How you can reduce usage of getter methods in your code

aleksikauppila profile image
Aleksi Kauppila Author

I'd prefer calling this design. I design what interactions are appropriate for objects.

Employee has to know $company because when we save it to database (or anywhere) this information is important. To be fair i didn't communicate this that clearly.

Accessing the company of an Employee must come from a use case. Until we have use case we avoid exposing this information. When we have use cases these things may be able to catch a certain more natural meaning. So instead of just doing data processing based on employee attributes we could have named reports or something.

This discussion is very difficult when the model is as crude as my code example here.

Thread Thread
bgadrian profile image
Adrian B.G.

Maybe you even forgot to mention that you control your entire app (monolith), and is easy to refactor the classes AND maybe it is even a small app (and you can refactor ALL the usages of a class in a matter of minutes.

my problems with this article will happen when you do not have access to the source code of the classes and you are just an User. This includes libraries, APIs,RPCs, frameworks. Also this could happen when the code has millions of LOC.

Probably some of the readers will build these kind of classes too, and you cannot and should not think of all use cases when you are building an API/expose your classes. If you try to do that it will bloat your classes, give the users flexibility, let them decide how to use it, of course in limited reason.

Thread Thread
aleksikauppila profile image
Aleksi Kauppila Author

As implied in code examples, this is business layer code. Yes, i expect people working with applications to be able to change their code

When you're working with libraries, you usually don't have to worry about them containing entities such as Company or Employee. But defensive practices work well when designing a library too. If there's only one way to use a class, it's virtually impossible to break the code.

Forem Open with the Forem app