Writing Generic Code (Advanced Bronze)
Generic code is an important concept in informatics. Of course, as all concepts go, you may dodge generic code and continue to write in a hyper-specific style. As such, begin by questioning purpose.
PURPOSE: Why write generic code?
Generic code is adaptable, meaning that it can be put to use immediately in many ways without major changes. It can be reused, extended, and even versioned powerfully to save time. Time is of essence in informatics, where I refer to both algorithmic time complexity and coding time.
Even if you are writing a very specific data structure or algorithm, to truly grasp it, a good contemplation is "Can I generalize what I have learned to a broader class of problems?" Answer this by then attempting to generalize.
That said, before you proceed, I issue the below warning:
Generic code can easily assist black-boxing, where you write a snippet of code such that you either forget its meaning or have no idea what it means in the first place. This is a common pitfall, and you should avoid this as much as possible if you truly want to grasp a concept in informatics.
Modern C++, a groundbreaking language in the current informatics scene, indeed has several builtin features to support and streamline generic code. Here, we will cover the basic important ones you will definitely want to add to your coding arsenal.
Classes & Structs
Classes are by far the most important utility in extensible code. If you ever want to write a data structure that has several member functions to process stored data, classes are for you. Of course, classes can have public and private sections. For instance, consider a class human
, which maintains several relevant member functions.
But how do we actually use (and reuse) this class data structure we have created? We do this by creating instances of this class, concretely known as objects.
Below is an example instance of the human
class, a human
object named Sal
.
Of course, we will want to initialize any object of this human
class with its fundamental attributes (body_temperature
and temper
from the above), but the problem here is that we are unable to access them directly; they are private and remain uninitialized.
To partially alleviate this, we can initialize variables in the class declaration itself:
This gives us something to work with, and we can now create Sal
in main()
as well as call his member functions (instantiated directly from the base human
class).
To completely solve this, we might be forced to make these variables public, but instead we can be more clever and write a constructor function for this class, essentially a function that is automatically called whenever an instance of the class is created. Constructors are useful when we want to prevent modification to variables we create but be able to initialize them for use, making them naturally the most viable option for proprietary software companies.
We can create a constructor for the human class and require initialization of the variables body_temperature
and temper
, which gives us some control over Sal
's intrinsic properties as they are initialized. The overall code becomes:
This is immediately very extensible if we wish to create multiple instances of human
, all with their own initial properties. In fact, we can be even more general by creating an external function condition that easily states feelings and emotions for us without having to be rewritten.
As one very specific but useful note on constructors, when we wish to merely initialize properties, we can adopt an alternative declaration that executes significantly faster than the first; using the argument name as the variable itself also becomes permissible and is guaranteed defined behavior:
Structs are useful when we care less about keeping private properties and more about having just a general reusable data structure. This means everything is public in a struct by default, and a human
struct, along with the reformatted constructor above, would look like:
Immediately, our struct is easier to manage given its open nature. It is more integrated with the surrounding code, and we can do manipulations like:
Finally, we can choose to discard the constructor altogether and opt for an initializer list based on the order of the declaration of intrinsic variables. In the human
case, the variables in order are body_temperature
, temper
, and name
, so we can remove the constructor and opt for an initializer list like:
Needless to say, all of this enables very clean initialization and manipulation of classes and structs, integral to generic code.
Templates
The human
class example, though well-defined, was mostly intended to serve as an example of the versatility of classes and structs. We now switch to something simpler. Imagine a three-dimensional point in space as the following struct, where we create some sample instances P1
and P2
But what if we wanted to make a P3
point with coordinates as doubles? We would then be forced to create a secondary point struct pt1
:
This might not seem too bad immediately, but imagine having to create structs like this over and over again just to accommodate various type changes. We need something better.
Lo and behold, the template to the rescue. We can use the format template<...>
to specify the specific templating conditions and then simply define the struct normally. In particular, if we use a class T
, we achieve:
It would be narrow-minded to think that templates are in any way limited to classes and structs. They can be used with functions and much more. One interesting use case is that of the size of various containers. The size member function of a container usually returns a type incompatible with integer, but we can easily write a templated function to fix this, handling all class types T
of container at once:
You may apply this as sz<vector<int>>(v)
on some vector<int> v
, but since C++11, functions (but not classes or structs until C++14 and C++17) can actually infer template arguments, meaning that we can simply use sz(v)
!
What if we wanted to put multiple arguments in a template to handle multiple classes? Consider the below secondary pair comparator struct as an example:
And in this design, since the template arguments both apply to only a function, they can be easily inferred! For instance, declaring a set of pair<double, int>
in C++11 is as easy as set<pair<double, int>, CPS>
.
What constitutes what other kinds of types we can put in templates? As a general rule of thumb, until C++17, the types valid in template arguments are only classes and fundamental types, of which only classes can be directly inferred in many cases by functions.
In fact, we can even have templates that take a variable number of arguments, known as variadic templates, or templates within templates, known as nested templates, both of which are beyond the scope of this basic exposition but can be found here and here.
As a fun fact, just as templates give us so much control over the generality of the language, a lot of the C++ standard in itself is written generically with templates under the hood.
Macros
Macros are infamous among many C++ users as terrible due to almost inevitable confusion when developers collaborate with each other. Nevertheless, macros are important to many in the informatics community to simplify notations in the standard.
Among various types of macros you will see are keywords such as typedef
, using
, and #define
. Of these, typedef
is now rather outdated (though still used by some) because it is more or less just an annoying version of using
with frustrating semantics, so we will not cover it here.
We start with using
. This is a fascinating keyword, frequently used to simplify namespace prefixing when applicable. For instance, statements like
actually allow us to use an entire namespace. Of course, since using namespace std
is frequently limited to the competitive programming community and looked down upon otherwise, we can use using
to invoke better simplifications.
Suppose that a lot of my code usesstd::cout
, which I find frustrating to type. I can write
and then just live with cout
. But what if I was using strings and wanted to type neither std::string
nor string
? I could use using
twice to fix this:
Or golfed into a single statement:
We can make even more aliases, with even aliases within aliases (see ll
), for environments where speed is key, such as competitive programming:
Finally, we can take using
to the next step and invoke templates! For instance, if we want to be able to write ar<int, 6>
instead of std::array<int, 6>
, we write:
As another example, if we want to use ai
to mean integer array, we can make constructions like ai<6>
work as well:
In general, it is important that using
declarations have strong scope guarantees, meaning that they will not work outside of their defined scope. To use declarations everywhere in the program, they must be invoked in global scope. But, if we want to be clever and create a reusable struct and just specify template arguments internally, we always have the option of:
In case we want to be able to access the specific type T
's alias meaning outside of pt
, this too becomes very easy:
We end this section off with #define
. In principle, good code should not need this (because using
exists), but #define
is essentially a crude find-and-replace that happens before compile time (in the preprocessor stage). In this sense, it is easy to use, where #define NAME VALUE
would ideally find all instances of NAME
in the code and replace them with VALUE
.
This example defines MOD
as 1e9+7
by find and replace. But without resorting to something as crude as #define
, we could just as well do:
The takeaway is to use using
and templates and avoid #define
. If you absolutely insist on using #define
for some reason, though, use it with caution and remember to #undef
for good scope control.
Preprocessing Logic
As far as preprocessor magic goes, we are in no way limited to #define
butchery. Ever wanted to write a program that compiles in different ways depending on some initial conditions? We can use preprocessor directives like #if
and #else
, or #ifdef
and #ifndef
to allow for this.
For instance, I may want my struct pt
to be two-dimensional in some cases, and I can control this simply:
Similarly, if we are not opposed to #define
, we could use #ifdef
and #ifndef
to see whether or not a macro is defined via #define
.
There are many clever applications of this, including versioning. Importantly, if we want code to run differently for different versions of C++, we can write:
Namespaces & Inline Namespaces
Finally, we can write our own namespaces to separate various functions. Within a namespace, we can have functions, variables, classes, and even more namespaces. Then, we can invoke using
declarations to use the whole namespace.
These are fairly self-explanatory, but a more rare feature of C++11 is the inline namespace
. Inline namespaces are not technically real namespaces but allow us to chunk up code and avoid having to use namespaces just to gain access.
So why even have inline namespaces? We can use their features interestingly. For instance, suppose we had a feature in an old version (v1) of a program but now removed it in the new version (v2).
Now, not using a namespace will automatically use v2. But if we want access to v1 of the buggy_feature
, we can simply write v1::buggy_feature
. There you have it, simple and effective version control.
Last updated