Sign inorRegister Site is for SALE!

Wake up it is the XXI century


I would like to begin this article from the fact that now it is 2012. I am saying this, because I often read the code in C++ at my work and for hobby, which was written about 10-20 years ago (and it is actual now), or the code written recently by the people who learned to program in C++ 20 years ago. And after that I got feeling that there was not any progress over the years, as well nothing was changed and developed, and the mammoths still roam on the Earth.


The programming specific was very different 20 years ago. The memory and resources of CPU were measured by the bytes and the cycles, many things had not been invented yet, and we had to deal with that situation. But this is not an excuse today to write a code based on these prerequisites. The world is changing now. I can feel it in the water. I can feel it in the ground. We need to keep up with the progress.

Everything that I am going to write further only applies to the programming in C++ and the mainstream-compilers (gcc, Intel, and Microsoft), unfortunately, I have worked less with other programming languages and compilers, so I will not talk much about them. Also, I will only talk about the application programming for desktop OS (trends may differ in the clusters, microprocessors and system programming).


There is awesome thing as TR1. This can be a revelation, but almost all modern compilers have built-in smart pointers, good random number generators, a lot of special mathematical functions, support for regular expressions, and other interesting things. They work quite well. Enjoy them.


Another great thing is C++0x. This is the name of the most recent iteration of the C++ programming language, approved by ISO as of 12 August 2011. It includes several additions to the core language and extends the C++ standard library, incorporating most of the C++ Technical Report 1 (TR1) libraries. Rejoice and use it, brothers!

There are already available:

• Lambda expressions
• Rvalue links
• Generalized constant expressions
• External templates
• Initialization lists
• For-loop through a collection
• Object constructors enhancement
• nullptr
• Local and unnamed types as template arguments
• Explicit conversion operators
• Symbols and strings in Unicode
• Raw string literals
• Static diagnostics
• Template typedefs
• The keyword auto

and lots of other useful things.
Well, let us take a look at the following examples:

• Instead of
vector<int>::const_iterator itr = myvec.begin(); now we can write
auto itr = myvec.begin(); - And it will work! Moreover, even the strong typing is not going away (auto - this is not a pointer and Variant, this is just syntactic sugar)
• Now we can use collections by analog loop for_each

int my_array[5] = {1, 2, 3, 4, 5};
for(int &x : my_array)
x *= 2;

Well, it is beauty. Let me remind, this is supported in the main, stable (no alpha \ beta) branches of all major compilers, and it works. Why are you not using it yet?

Passing everything and everywhere through pointers (reference)

Capability to pass the entities into the functions and the methods as through a reference or through value is a very powerful tool. I often see when everything always is passed through the pointer. Here are the people’s arguments:

• The pointer passes faster than a data structure - an increase of speed.
• The pointer does not require an additional copy when passing – it saves memory.

Both arguments are irrelevant. It often saves a couple of bytes and cycles, as well it has a bunch of flaws:

• The receiver function has to check all the arguments at least to NULL. The pointer is not NULL and that fact still does not guarantee anything.
• The receiver function could do anything with the passing entity. Edit, delete, and so on. The argument about the keywords “const” is no argument. In C++ is a lot of hacks that make it possible to change data through constant pointer or reference.
• The call function has to trust it in part caused by changes in the data, or validate them after each call.
• Many objects, which passing is tried to be optimized by the use of pointers are themselves almost pointers. This applies to the class strings, which work is optimized everywhere.

Let me give an analogy: you have a party at your home, and there are a dozen good friends and a pair of random individuals (as always). Suddenly, one of such individuals notices on your computer informative movie and asks you to borrow that. You unplug the computer from the socket, instead of recording the movie on flash drive \ DVD and give it to him saying: "Here, take and watch it". Well, it sounds like nonsense, right? So why would you give to some unknown function your code data to be profaned?

Calculation of the constant

Here is a piece of code:

#define PI 3.1415926535897932384626433832795
#define PI_DIV_BY_2 1.5707963267948966192313216916398
#define PI_DIV_BY_4 0.78539816339744830961566084581988
< more 180 similar definitions >

Maybe I reveal a great mystery for someone, but today constants are calculated by the compiler when compiling (not at runtime). So “PI/2" will be read more easily, take less space and run as fast as 180 definitions in the example above. Do not underestimate the compiler.

Do not invent own bikes

Sometimes I see in the code something like:

class MySuperVector
{// my very fast implementation of vector

At this point, the trembling strikes through me. There are STL, Boost (and many other libraries) where the best minds the world have been improving many excellent algorithms and data structures. You should write something new only in 3 cases:

• You study (laboratory work, yearly project)
• You write a serious research on this theme
• You know by heart the codes STL, Boost, dozens of similar libraries, 3 volumes of Donald Knuth and clearly believe that there is solution for your project.

In reality, the following happens:

• People have no idea about the availability of libraries
• People do not read clever books
• People have an inflated self-esteem and consider themselves smarter than everyone else

Therefore, we have funny bugs and so on.

Unnecessary optimizations

int a = 10;
a <<= 1;

What is written here? I believe almost everyone knows that this is the bitwise shift. Also, many people know that these operations for integers are equivalent to multiplication and division by 2. So it was fashionable to write in this way before, because the bitwise shift operation is performed faster than multiplication and division operations. Here are the facts for today:

1. All compilers are smart enough to substitute the multiplication and division for shift in such cases.
2. Not all people are smart enough to understand the code.

As a result, you get poorly readable code without the advantages of working speed and periodic questions (depending on the number and skills of colleagues): What is this crap? Why do you need this?

Unnecessary memory saving

I will give a couple of links to cases, where people tried to save 1-2 bytes of memory and what came of it.

Resource IPv4

Today, we already have an average of 2 to 4 GB of RAM. In a couple of years everything will be 64-bit and the memory will be even greater. Think in advance. Save megabytes, rather than individual bits. If we are talking about the number of people, objects, transactions, temperature, date, distance, file size, etc., - use the types: long and longlong or something specialized. Forget about the byte, short and int. It's only a few bytes, but overflows in the future could be very expensive. Do not be lazy to create separate variables for the different entities, and do not use one temporary with the thought "anyway they will not be used at the same time".


Do not program the rock painting. Progeny will not appreciate it :)!
Skull 26 february 2012, 13:31
Vote for this post
Bring it to the Main Page


0 Ashwinee December 8, 2020, 11:41
I found best gpl plugin and themes at very low price with latest update. You must download it from

Leave a Reply

Avaible tags
  • <b>...</b>highlighting important text on the page in bold
  • <i>..</i>highlighting important text on the page in italic
  • <u>...</u>allocated with tag <u> text shownas underlined
  • <s>...</s>allocated with tag <s> text shown as strikethrough
  • <sup>...</sup>, <sub>...</sub>text in the tag <sup> appears as a superscript, <sub> - subscript
  • <blockquote>...</blockquote>For  highlight citation, use the tag <blockquote>
  • <code lang="lang">...</code>highlighting the program code (supported by bash, cpp, cs, css, xml, html, java, javascript, lisp, lua, php, perl, python, ruby, sql, scala, text)
  • <a href="http://...">...</a>link, specify the desired Internet address in the href attribute
  • <img src="http://..." alt="text" />specify the full path of image in the src attribute