How Learning C++ Changed My Programming Style
My journey and why software developers should learn it
People say learning C or C++ is every programming student’s worst nightmare. It can indeed be quite confusing to get started if you don’t have any prior low-level knowledge.
Despite what some people say, C++ is definitely not obsolete, as it’s still used in a wide variety of fields such as compilers, virtual machines, artificial intelligence, and game engines by around 25% of developers, according to the 2021 Stack Overflow survey.
However, most programmers tend to stay away from it, as it can be quite intimidating, especially because of its infamous error-prone memory management system. I suggest instead that every software developer should learn C++ as part of their toolbox. Let me show you why.
My background before C++
I got started with programming on an Arduino board, coding simple programs in C++. However, I was just doing beginner-level projects and didn't even know (or need to know) about core concepts like pointers or memory.
It wasn’t until I started coding in Python that I really began to understand the fundamentals — data structures and algorithms, design patterns, OOP — and appreciate programming. I also learnt some Java and Dart/Flutter for mobile development, but I never took on any complex project.
How I started with C++
During these first years of programming, I never got in touch with low-level languages and concepts until I got fascinated by artificial intelligence. At this point, I decided to do the dumbest thing in my programming history, develop a neural network training framework from scratch in C++ despite the fact that I had no idea how neural networks worked and had no experience in writing C++ code.
After some months of research and hard work, I eventually failed to complete my project. However, during the process, I learnt a lot about artificial intelligence and also about C++.
I know this head-first dive into these hard fields — C++ and neural networks — wasn’t certainly the smartest move, but it has nonetheless led me to perform much research on these topics and has allowed me to pick up new skills that I wouldn’t otherwise have developed, probably.
How C++ changed my programming style
Coming from a higher-level language background, I wasn’t quite used to writing efficient and optimized code. Through my C++ learning process, I started thinking more like a computer when writing code.
During my second head-first dive into a complex project with C++ — building a compiler and a virtual machine from scratch — I learnt even more about more advanced language features such as templating, constant expressions, move semantics… I also had to implement from scratch many common data structures such as linked lists, and trees and started working with raw binary data to handle the virtual machine’s memory and the binaries generated by the compiler.
Moreover, learning how to manage memory and how data is stored in RAM got me think twice when using resource-heavy, but common, data structures like strings and complex objects. In addition to that, I started using the appropriate numeric data types instead of throwing int
s everywhere. For instance, you wouldn’t store people’s age in an int
, which can represent numbers between -2,147,483,648 and 2,147,483,647. A 1-byte data type (unsigned char
, byte
, u8
) that can store values between 0 and 255 does the job just fine, other than being much more space-efficient.
Another important lesson is the benefits of an index-based lookup table or a hash table, rather than having to perform resource-heavy runtime calculations multiple times.
A new learning approach
For everything you learn about in the lower-level world — be it an error, a new data structure or concept, optimization technique — you are presented with many new paths that in turn lead deeper down the rabbit hole.
This learning approach is recursive, as every step requires you to (or leads to) do more research on the topic because you are missing some important pieces of the puzzle that require additional knowledge.
A clear example of the effects of this approach is my journey through compilers. First of all, I needed to understand the various stages of compilation: tokenization, syntax analysis, intermediate code generation, and executable output. The latter led me to learn about assembly languages and how they work, which, in turn, got me to do research on how a CPU works. This step was also crucial to building my virtual machine, which also required figuring out how memory works and, eventually, system interrupts.
As you can see, this is a never-ending journey down the rabbit hole of low-level. However, this approach doesn’t only apply to programming, but also to everything else in life: if you want to really learn something, you would be better off breaking it into bits and studying each one in-depth.
Conclusion
While many developers tend to stay away from learning C++, either because they are intimidated by its infamous difficulty or because they don’t think they will ever need it. However, I strongly suggest you give it a try, as it will help you become more aware of how the technologies you interact with on a daily basis work. Knowing your tools is the first step to mastering them, taking advantage of their strengths, and avoiding their weaknesses.
Other than that, knowing a language like C++ really helps you learn other languages, since they may share many similar concepts such as memory addresses and pointers, type generics, OOP, and more, not to mention that most modern languages are way easier to pick up.
Work hard upon starting, everything will seem easier then.
I hope you enjoyed this article. If you have anything to add, please share your thoughts in a comment. Thanks for reading!
If you are a Python developer interested in enhancing your programs’ performance, I suggest you check out this story below: