Skip to main content

Constness of an object


Defining a function you might want to make some of it's arguments to be constant to avoid unnecessary copying or have a bit more strict interface. There is another way to use constness - there we meet constant class members.


I believe you should know it's definition and how to define one. Anyway let's start from the beginning - defining a constant class-member function you obtain a function that forbids changing of an object of the class. Calling such function wouldn't change the inner state of the object. But we want to do stuff with our class and be able to change itself. Are those constant functions really useful at all? - you might ask yourself.

That's a good question. So let's see what we can done with setting few functions as constant. Let's say we've got a class that provides with images that accessible by some kind of an alias.
 
Here we see some interesting functions that allow us to add images, get them back and calculate theirs overall size. Simple but useful stuff. Let's describe abstraction that we have in this class.

GetImage allows us to get image and it's miniature by an alias. The alias variable passed by a reference so there is no copying which is good, but the alias isn't constant. That means that to be sure we have to pass a copy of the alias if we want to be sure that call won't corrupt our alias string (it will be corrupted for sure).

AddImage helps us with adding new images into the storage. An alias passed as a reference causes the same problems as in GetImage function. An image passed by value so here we have a copy from the start, which will take some time.

CalculateImagesDiskSpace provides calculated disk space used to store all of the images. Does not much except changing a model and consistency of the storage. Which is kinda hard to detect in a big project.

Let's say you obtained such a project that have a big code base - UI code, business logic, application setup, external libraries, some networking. A product owner want's you to fix a bug in a storage - something causes images to disappear and change from time to time. That harms product owner business and that's the reason he dropped on a developer that was improving the code earlier. A lot of stress for the beginning of the work with the new customer, don't you think so?

Let's see what we can do to track don't the data inconsistency. In software architecture there is such a thing as the data perspective. Mane reason of that perspective is to define life of elements in a project and be sure that the data model will be strong and consistent during work of an program.

We can track all the calls that changes data. Do not have all of them covered cause it's not that easy in a big project. Using this strategy we will waste a lot of time by doing same routine and trying to understand why we have the problem and where is the broken call.

Let's define changing code and separate it from the rest.

By setting GetImage and CalculateImagesDiskSpace we immediately get few interesting errors. Both of them will warn us about changes that are applied to some of the variables.
In GetImage it's name variable and in CalculateImagesDiskSpace it's erase of elements in storage. Does some of them looks as inconsistency for you? The second error screams that there is something weird goes in CalculateImagesDiskSpace. While looking into it you will see that erasing and after fixing it you will be a step closer to have the happy product owner.

Let's see what we could done to the ImageStorage class to make it a bit more bearable.

It isn't pretty by any mean, but using constness you will be sure what functions changes an object state and which of them are safe to use even in multithreaded environment.

Changes lead to greatness, steadiness helps to not waste it.

Popular posts from this blog

Move semantics

For a long time in C++ was no fast way to deal with return value. They were copied and there was no hope to change it. Some have used return parameters in argument list like: void GetObjectsInArea(Area exactArea, ObjectList& objList); To avoid unnecessary copies and speed up the execution developers were doing a lot of work. Eventually when optimized code were becoming unreadable the developers would abandon it and the code would transform into legacy code at some point.