Refactoring with C++17 std::optional

There are many situations where you need to express that something is “optional” - an object that might contain a value or not. You have several options to implement such case, but with C++17 there’s probably the most helpful way: std::optional. For today I’ve prepared one refactoring case where you can learn how to apply this new C++17 feature.

READ MORE...

Factory With Self-Registering Types

Writing a factory method might be simple: unique_ptr<IType> create(name) { if (name == "Abc") return make_unique<AbcType>(); if (name == "Xyz") return make_unique<XyzType>(); if (...) return ... return nullptr; } Just one switch/if and then after a match you return a proper type. But what if we don’t know all the types and names upfront?

READ MORE...

The Pimpl Pattern - what you should know

Have you ever used the pimpl idiom in your code? No matter what’s your answer read on :) In this article I’d like to gather all the essential information regarding this dependency breaking technique. We’ll discuss the implementation (const issue, back pointer, fast impl), pros and cons, alternatives and also show examples where is it used.

READ MORE...

How not_null can improve your code?

One of the key points of modern C++, as I observe, is to be expressive and use proper types. For example, regarding null pointers, rather than just writing a comment: void Foo(int* pInt); // pInt cannot be null I should actually use not_null<int *> pInt. The code looks great now, isn’t it?

READ MORE...

Learning from bugs and PVS-Studio Team

Real life: Fixed 1 out of 99 bugs in a project. 117 to go… Have you experienced something similar? Although it’s impossible to write bug-free code, there are tools and practices to lower the rate of mistakes. Today, I’d like to run through a gigantic list of freely available resources from the PVS-Studio Team who works with bugs analysis on a daily basis.

READ MORE...

Modernize: Sink Functions

One of the guidelines from Modern C++ is to avoid using raw new and delete. Instead, you should use a smart pointer, a container or other RAII object. Today I’d like to focus on so-called ‘sink functions’ that takes ownership of input parameters. How can we modernize code around such calls?

READ MORE...

Variadic Templates and a Factory Function

Variadic Templates from C++11 is probably not a feature that you use on a daily basis. But recently, I’ve come across one refactoring example where I’ve decided to give a try and apply variadics. Intro When I was doing some work in some old UI code I’ve noticed several similar lines of code that looked like that:

READ MORE...