DEV Community

Cover image for Don’t force your objects to construct what they need
le0nidas
le0nidas

Posted on • Originally published at le0nidas.gr on

Don’t force your objects to construct what they need

Let’s say we have an object that handles instances of Person. For example PeopleScreen:

PeopleScreen renders instances of Person so this should be the only format we provide to it. Let me explain.

Forced construction

There is a new flow that ends in opening PeopleScreen but all the information for the list of people are in a Map<String, String>. There is no reason to alter PeopleScreen in order to support this new format:

the new format is passed through an overloaded constructor but the same goes if we use a setter method

Why we shouldn’t do it

We could argue that by doing so we tie the object with each special format making the code hard to maintain and scale but the real reason is that we violate the SRP principle since PeopleScreen will have more than one reasons to change. One if something changes in the way we render and two if something changes in Person‘s construction.

What we should do

We should keep PeopleScreen only consuming Person and move all transformations to their own objects allowing a coordinator to transform and pass data around.

Discussion (1)

Collapse
detoix profile image
detoix

It has something to do with open/closed as well - in this example you can easily add new feature by making this transformation outside of PeopleScreen and therefore extend your application by only adding code - without making any change in existing codebase.