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

OpenGL: how to start studying?

I've been studying OpenGL for about 2 years, but I haven't got far with it and had no idea what is wrong. Is it me unable to understand OpenGL abstractions or is it designed not very well? I was looking for an answer all these years when suddenly I've started getting into math since my job required some math skills and suddenly OpenGL started to make sense for me. I ain't trying to say that now it's fun and games - honestly I find it challenging, but not as bad as before.

Templates and how to fold them

Variadic templates appeared in C++11 to cover such cases when you would have a template functions that could have a numerous members of different types. Doesn't it remind you of variadic functions that uses va_start, va_arg, va_end and so others? Cause it should be.


Tuples: are they useful?

In some languages there is such a thing as tuple which makes real to write such code (this time it's in python):


def f(x):
 # do stuff return (True, modified_string)

success, modified_string = f(something) 
C++ hadn't such feature until C++11. That was one of the reasons to pass variable as referenced argument, which ended up creating huge code bases that have unused arguments in it's functions. That happened in order to save revers compatibility and allow clients to use stable interfaces to access libraries without checking the version of dynamically attached library in order to use correct functions.