Preserving Const

Make everything const by default (not necessarily constexpr). This includes everything from functions, to local variables (most important) to function parameters. const greatly helps with compiler optimisations and leads to a lot of code being resolved at compile time and reducing aliasing & duplication. Using const is also beneficial in...

3 minute read

Jumping Switches

One of the reasons to prefer a switch case declaration to a bunch of if-else’s is that the compiler can generate a jump table. This is not always the case. Take the example below. Code gen can be found here: https://godbolt.org/g/V7Qdci // Individual compares int switch_2_cases(int val) { switch(val) {...

2 minute read

Avoid Comments

Yes, you read that right. Allow me to explain. Code should be self explanatory. This means that the combination of your variable/class/function names, code structure and class relationships should automatically convey what’s going on without having to explain anything everything. In simple terms if someone else has to read a...

5 minute read

Evaluate/Access Once, Read multiple times

In certain cases, when accessing certain class / struct members, it can be costly if you’re reading the member multiple times. Especially in cases with branches the compiler may not store the object in a register i.e. it will not evaluate the access once. Take this case for example: void...

2 minute read

Named Parameters

In quite a few languages, you can annotate function arguments when calling one eg: requestHandler(url:"http://....", retry:false); For better or worse, C++ doesn’t have such a feature and instead this is what one ends up with: RequestHandler("http://...", false /*retry*/, nullptr /*custom_data*/); While this is good practice, you can often do without...

2 minute read

Comparing Numbers

Statistical Comparison & Analysis of Performance Metrics Introduction Before You Measure Choosing the Appropriate Metrics Sampling Data Viewing Raw Data Analysing the Distribution Rejecting a Hypothesis Comparing Numbers Summary References 1. Introduction Say you have a computer program / routine / function / test / loop, basically anything that runs...

26 minute read

C++ Tip of the Week : Series Intro

As you may have deduced from the title, I’ll be starting a C++ Tip of the Week inspired by the Google Abseil Tip of the Week, which I highly recommend reading. Very similar to Google, I’ll start off with some basic stuff and then move on to some advanced stuff...

2 minute read