R.A.I.I. Resource Acquistion is Initialization
Created: 12th May 2026
Updated: 12th May 2026
So today we got started on RAII. An idiom that help represent the idea of
effective resource managerment. It follows guidelines similar to the S.O.L.I.D.
principle that allow a programmer to follow certain ideas that lead to more
effective and more maintable code. The main idea is that an object is only
destroyed if it has fully constructed, referring to the local variables of
said object. And that the destruction of it is the reverse order of construction.
This makes sure that an object is only every destroyed if it exists already, as
destorying something without knowing it is "alive" may lead to undefined behaviour.
This also ensures that any variables or other objects that rely on others are
destoryed first before their prerequisites are destroyed. I should also probably
mention that this requires the knowledge and use of classes to be implemented
correctly, though this does not mean that the ideas cannot be applied to other
programming languages or structures.
RAII is used for resource managerment since variables are initialized alongside
the memory used for it. This ensures that when something is constructed, its values
are also set, hence the name of the idiom. This ensures that there is no point
where an object may have a half-active or half-usable state. The object should be
fully capable of executing its functions without coming upon any undefined behaviours
or "missing" values. The object should also be fully destroyed without issues since
the object can only fully construct when no errors occur during construction. So
instead of checking whether the object is an empty or pointing to some unknown memory,
we can be sure that the object "handles itself". This saves the common issue where
heap allocated objects are not manually destroyed which leads to points of memory leaks.
RAII allows the programmer to set the destruction or deallocation of said memory in
the destructor of the object, saving any potential headaches when it comes to managing
memory. It is important to note that the class should have a singular responsiblity.
This means that it is highly recommend that when handling complex structures that deal
with multiple raw pointers, that this class be composed of other minor or sub classes
to deal with each type of memory. However, there are other solutions as always depending
on the programmer and structure of the program.
One of the solutions when handling complex multi-step initialization are using
delegate constructors. Delegate constructors are constructors that are within
other constructors. An example would be a class with 2 integers and 1 string.
The first constructor would take in 2 integers, whereas the second constructor will take
in 2 integers and 1 string. So instead of duplicating the code to assign the integers,
the second constructor can use the first constructor to assign the integers first,
and then assign the string given in the arguments. This is useful for
managing multiple possible sources of failure as you can check for exceptions during
construction of different arguments.
For now this is may main takeaway from today's topics. If I figure out more about it
I'll add more to this post. [12th May 2026]