Set-up and tear-down

Recently as part of program run-time performance optimization effort, I was scanning through the code for occurrences of keyword new. The goal was to find unnecessary memory allocations. I was surprised to find most of news in the unit test module. Not that there was any particular need for memory allocation: it is just that the framework that was chosen (a fairly popular one) enforces on the programmers bad practices of manually controlling the life-time of their objects. Continue reading

Posted in programming | Tagged , , | 4 Comments

Handling short codes — part II

Today, we will continue with the implementation of a type capable of storing short codes. For the previous post on the subject see here. This time, we will focus on type safety. Continue reading

Posted in programming | Tagged , , | 5 Comments

Handling short codes — part I

In my work, we often deal with codes: country codes, airport codes, airline codes, aircraft codes, and more. The thing they have in common is that they are really short. Sometimes 2-letter long, sometimes 3-letter long, sometimes a bit longer. Most of the time their value is used to compare against other codes, e.g., when searching through a collection. We hardly ever need to see their textual representation.

The initial choice was to represent them in programs by type std::string, a general-purpose type for storing sequences of characters. But, as with any general-purpose tools, they fail to take into account the particular usage cases which often offer room for improvements. Continue reading

Posted in programming | Tagged , , , | 32 Comments

A clever comment style

Comments are one of the most useful language features; in practically any programming language. Yet, they can become really a pain. Just consider the following example that I have seen quite often in different variations:

// now, return the difference between the two results
return result1 + result2;


// at this point x must not be negative
return a - b;

You could surely give me even better examples of counterproductive comments. In fact, this thread contains a long list of funny comments in the code.

Because such things really happen, some people come up with guidelines like, “never use comments in your code — make the code self explanatory.” While the direction looks sound, I would not go so far as to making it an ultimate rule.

I guess, we need to accept the shortcomings of comments; instead, let me show you a technique I was taught by my colleague that helps mitigate one comment-related problem. Continue reading

Posted in programming | Tagged | 17 Comments

String’s interface

What I have to say today is fairly obvious, and you probably know about it. But because I have observed the following pattern in my project’s code fairly frequently, I feel it needs to be recorded. Consider this code:

bool fun (const string& code)
  assert (code.length() >= 2);

  if (code.substr(0, 2) == string("XX")) {
    // ...

  // ...

Can you see what is wrong about it? Please, do not focus on the usage of assert. I only put in order for you to take for granted that the string has two or more characters. Continue reading

Posted in programming | Tagged , , | 22 Comments

Bug of the week

Today we are going to see a case study illustrating a bug. Not a very spectacular one: a typical bug one would encounter in everyday work. We will start with the symptoms, identify the root cause, and suggest measures to prevent similar things from happening in the future. Continue reading

Posted in programming | Tagged , , | 11 Comments

A conditional copy constructor

In this post we will try to define a ‘wrapper’ class template that does or does not have a copy constructor depending on whether the wrapped class has it. This will be a good opportunity to explore in depth a couple of advanced C++ features. Note that this is a rather advanced topic and, unless you are writing or maintaining a generic library, you will probably never need this knowledge.

The problem

By a wrapper class I mean something like boost::optional<T>. It wraps a value of type T: it is an ‘almost type T’ with some special features. Additionally, as in the case of optional, our wrapper does not inherit from T, nor does it contain a data member of type T.

Our goal is that for any type T the following holds:

std::is_copy_constructible<T>::value ==

Continue reading

Posted in programming | Tagged , , , , , | 3 Comments