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

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.

Maturing, relocation, getting to the point

So it's been a long time I've wrote something for the blog.
Since the last post few things have changed. For example - I've moved in a foreign country. I went into Ukraine which is not a big deal you would say, but for me it's a pretty significant change in my life.

I've worked a two years in solo on a video related projects, one of which was related to trans-coding video on fly using a gstreamer and the other one was about storing and providing access to a video content by special tags, this one was a nightmare.

The gstreamer is a neat open source solution in video processing libraries. The people designed it are crazy ones - such a over-complicated implementation to achieve flexibility and effectiveness in processing video on various platforms and hardware. The most important about this library for me was it's non-existing support. Or should I say community driven support? I don't know even for now. At the time I've had a couple questions, so basical…

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.