8 Things I don't like about C++

Venkateswaran Sivasailem
Bootcamp
Published in
5 min readSep 2, 2022

--

Photo by Marc Rafanell López on Unsplash

If you ever used C++ on a side project or full-time job, you might have felt that writing bug-free code in C++ is not an easy task. In fact, in recent years, languages like Golang, Rust, Zig and a few others are developed just to overcome the difficulty of C++.

In this article, I have listed some common pitfalls that I faced while learning and working with C++

Disclaimer :) This article is based on my personal experience and by no means to conclude that C++ is good or bad.

It’s huge

credits pixabay

The language has 97 reserved keywords and keeps getting added 😟 If you wanna compare it with other languages, Golang has 25 and python reserves 33. It’s not uncommon to see books that are focusing on a single feature of the language, for example, C++ Move Semantics — The Complete Guide by Nicolai M. Josuttis (by the way it’s a great book to read if you are really into C++).

So next time, if you come across a YouTube video with the thumbnail “Learn C++ in 1 hour” don’t click it 😆

A lot of bad online tutorials

In today’s internet era, you can learn anything just by googling but at the same time, it opens the door for many substandard teachers 😕

For example, you might have seen these types of simple code to teach how to use header files in C++.

HelloWorld.h
HelloWorld.cpp

If you compile and run this code g++ HelloWorld.cpp && ./a.out will print Hello World

Can you find the landmine here? 😃

It’s using namespace std; in HelloWorld.h file. Most of the time the reason is to reduce typing work (you can type cout, endl & string instead of std::cout, std::endl & std::string). This will suck your whole evening if you did this in a reasonable-size C++ project which uses third-party C++ libraries that define the same names that are defined under the std namespace.

More details: Why is “using namespace std;” considered bad practice?

of course, there is another minor issue #pragma once which is not portable so you need to use a header guard for portable C++ projects.

It’s not a swiss knife, it’s a swiss knife factory

credits pixabay

You have so many tools under your belt and you are eventually overwhelmed to decide which is the right one to pick. You can initialize a variable in 7 different ways 😈 and you can assign 5 different value types (lvalue, rvalue, xvalue, prvalue, glvalue)

C++ Initialization, C++ Value categories enjoy!

Same but different, but still the same

Because of the language diversity, everyone builds their little language inside. Some project refuses to use templates and other prohibit raw pointers and overuse casting. Along with this, there are different code formatting which makes things even worse.

Sometimes, switching between C++ projects will give a feeling of relearning a new language even though both are written in standard C++.

Comparison of C++ coding style — Choose your poison 😆

Even if you are escaped from all these traps, you have some framework fanboys on your team who want to write everything in Qt, Boost, Poco or whatever to bring the elephant into the glasshouse.

Undefined Behaviors

Description of undefined behaviour in cppreference.com

Renders the entire program meaningless if certain rules of the language are violated.

This is not something specific to C++ and every language has some kind of undefined behaviour. Even the most praised scientific language, Haskell has few. But in C++ because of its divergence, if you don’t stick with a well-defined subset of the language, you have a higher possibility of facing UB in the code.

UndefinedBehavior.cpp

This may not be the undefined behaviour or some C++ guru might say that it’s not crashing because of this pointer is not used in the code. No matter what, this will surprise anyone trying to learn this language.

Implicit Surprise

credits pixabay

C++ has a strong type system, but it also has a landmine called Implicit conversion. It’s a quite big topic and you can read it here if you are interested. But just to give a glimpse of it, in C++ you can write if condition check with literally anything, for example if(7){ } if(true){ } if(enum){ } if(someObjectWhyNot){ } all are valid because any non-zero value gets converted to true and it creates some beautiful bugs in large-size projects.

And of course, there are several ways to do an explicit cast from one type to the other. const_cast, static_cast, dynamic_cast, reintrepet_cast using these casting tools, you can even change your PC ram from Kingston to Samsung 😆

Still not enough

It already takes years to gain a reasonable amount of expertise in C++ because of its complexity, but the C++ committee comes with a ton of features for every release. More features More Undefined behaviours.

Build System

credits pixabay

CMake is considered a de facto build script generator for C++, but it has a very steep learning curve and in some projects, both Make and CMake coexist to make things even harder.

Final Thoughts

C++ is 40 years old language and it’s carrying all the backwards compatibility burdens. Thousands of projects are written in C++ (TensorFlow, OpenCV, WebKit, V8 engine etc.,) and still they are maintained by a huge community of developers. There is no way to completely replace this guy with any new language in the near future.

Best Book for Beginners — ” C++ Primer 5th edition by Stanley B. Lippman”

Book for all levels — https://stackoverflow.com/a/388282/10334333

--

--