DEV Community

Cover image for About constructors and class safety
Sameh Muhammed
Sameh Muhammed

Posted on • Updated on

About constructors and class safety

Introduction

Moving from Structure programming paradigm to OOP paradigm came from simulation of human life, which all data is part from Object, you can't find just a number like 175 alone, it have to be part of something and measured in some way like 175 CM height of Person, So starting from Struct datatype which is just a group of variables/properties alone without even getters and setters, then a step forward by classes with properties and behaviors to be more close to real life as each object is perform actions or change its' state by time.

Constructors

So starting from class definition that is a group of properties and behaviors(logic), when you need to make an/a object/template of your class and put there some data and perform actions, you should first allocate space for this object and instantiate it, in OOP language you have to use "new" keyword followed by what called constructor to tell complier to allocate memory for this data.

But, Why contractors? and why we can have multiple variation of constructor, isn't one enough?

Constructors not just for allocating memory, they also for instantiate your object in a safe state, most of developers coding there programs for end user service, but when it came to write a library for other developers use, this will make you think a lot about your class safe state, let's take an example

if we have below class

Image description

This class is performing a division operation, if we let it with this constructor you are putting your library under developers mercy if someone put the divisor with 0 and fire divide method the program will crash.

Another example

Image description

Let's say this Product class is part of library will going to be use by another team or developers, and without any documentation, one developer came and used default constructor to get an object from Product class and then fire getTotalPrice() method the code will crash also because its' state is wrong and it should be in a certain state before anyone can use it.

To solve first example problem you have to add validation upon divisor variable to not accept 0 as a value, and in second example you have to remove default constructor and add one with 2 variables to start objects' life with safe state, in the 2 examples the issue is from the developer who writes the classes at first because he have to defend his logic against failure and not trust anyone.

another benefit of constructor that it can enhance code readability, if you have a lot of properties need to be set to use your object why not to encapsulate these set statements into the constructor and not scatter them everywhere.

Image description

Replace constructors with creation Methods

When you have multiple constructors and each one is responsible for creating an object with different state and purpose, for more readability and less confusion purpose it's better to make your constructors private and provide static methods with meaningful name describes what kind of object this method create.

Image description

Constructor is not enough

We know now, importance of using constructors in good manner to provide class safety, but what about Setters of class, you may start your object with safe state and someone use your setter methods to change properties values with wrong one.

Immutability

If you have problem with setters you can just eliminate them don't use setters at all, provide just getters and accept values from constructors, by this you have only one entry point for your class and put their any guards you want and now your class is in safe state and this state can not be changed.

Image description

But, what if you need to give your class some flexibility to be changed, in this case you can provide methods that change state/value by providing a totally new object with the new state and off course as well you can put your guards there, by doing so your class now is instantiating in safe state and can be changed by providing a new object with safe state also, you are now working with what called immutable object like String and BigDecimal classes in Java or what called Value Object in Domain Driven Design (DDD).

Value object or immutable object providing you with safe state in all object life, but it complex also development process and affects your memory with multiple footprint objects when changing state multiple times like the case of String class, so you can use this approach with objects that need special handling and moved from module to another or dealing with multiple threads that will provide safety there.

References

IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.

Buy Me A Coffee

Top comments (0)