There’s a common myth that writing software in C++ is very very hard. Take these two comments on my previous video (below). This first one is impressed that I managed to build an MVP web app in two weeks, working part time. While the other one says that I could have done it in one week if I hadn’t used C++.
Both assume that C++ is hard, which simply isn’t true. I wrote a minimal sales tracker web app in C++ in two weeks, part time. And, I’m not one of those mythical 10x coders (although I am good at my job). So it is possible. I just did it.
Writing code in C++ can be hard, but not for the reasons you think.
C++ Is No Harder Than Other Programming Languages
Have a look at this code:
Both programs compute the sum of squares and print it to screen. The one on the left is Javascript, while the other is C++.
function sumOfSquares(n) {
let total = 0;
for (let i = 1; i <= n; i++) {
total = total + (i * i);
}
return total;
}
let n = 5;
let result = sumOfSquares(n);
console.log("n = " + n);
console.log("Sum of squares = " + result);
if (result > 50) {
console.log("Result is greater than 50");
} else {
console.log("Result is 50 or less");
}
#include <iostream>
int sumOfSquares(int n) {
int total = 0;
for (int i = 1; i <= n; i++) {
total = total + (i * i);
}
return total;
}
int main() {
int n = 5;
int result = sumOfSquares(n);
std::cout << "n = " << n << std::endl;
std::cout << "Sum of squares = " << result << std::endl;
if (result > 50) {
std::cout << "Result is greater than 50" << std::endl;
} else {
std::cout << "Result is 50 or less" << std::endl;
}
return 0;
}
Notice how similar they are. The C++ one is a bit more verbose, but you can see the same for loop, arithmetic operations and print commands side-by-side. Even the squiggly brackets are the same.
Core code structures like if/then, loops, arithmetic and function calls are similar in all programming languages. Writing them in one language is no easier or harder than another, when writing the same algorithms.
C++ Challenges
C++ does have its challenges, though. For starters both C and C++ won’t protect you from your own stupidity. For example, it’ll happily let you trash memory beyond both ends of your arrays, which can cause really weird behaviour down-the-line (see below). And you can spend the next week searching for the bug in the wrong place.
void foo() {
int numbers[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
numbers[-1] = 10; // Oops! Just trashed memory before the array
for(unsigned i = 0; i <= 10; i++) {
numbers[i] = 0; // Oops! Did it again, after the end of the array
}
}
Languages like Javascript, Python and others won’t let you do that, because they do bounds checking. To be fair, you can do that in C++ too, by using std::array’s at() function to do bounds-checked accesses (and accept the performance penalty), or use iterators so you don’t go beyond the end in the first place.
#include <array>
#include <iostream>
void bar() {
std::array<int, 10> numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
try {
numbers.at(-1) = 10; // Does bounds checking
} catch (const std::out_of_range& e) {
std::cout << "Out of bounds: " << e.what() << std::endl;
}
// Iterator based loop does not allow out of bounds access
for(auto& number : numbers) {
number = 0;
}
}
The next major issue: too much choice. More modern programming languages usually have one build tool and one package manager. C++ has many build tools and package managers. GNU Make, CMake, Meson; I did an entire video series on C++ build systems, and probably haven’t even tried half of them.
Then there’s C++’s absolutely gigantic standard library, which has grown and grown over the decades. Absolute backward compatibility must be maintained! Every cool feature that other languages have, eventually end up in C++. Lambda functions? We’ve got them too! Co-routines? We’ll take those thanks. Functional programming? Okay, maybe just a little.
The C++ libraries are so big now that there are whole sections of the C++ STL libraries that I have never used, and likely never will. That’s a giant clue on how you can reign in the overwhelm, by the way.
But, none of this really matters. After all, I wrote a minimal web app in C++, in two weeks, part-time. How did I do it, despite all of these issues?
What Actually Makes Programming Easier…
It comes down to code libraries, and frameworks.
If I asked you to write a web app from scratch in plain Javascript, that would be hard. And I mean without using node.js’s extensive libraries. So you have to write your own TCP socket code, your own HTTP parser. Everything. That’s a lot of work, and rather hard.
Use node.js’ runtime librarii, and that web app gets a lot easier. That’s because you’re standing on the shoulders of giants. There’s a huge ready-written code-base at your disposal.
Bolt next.js on top of node.js, and you’ve got even more functionality at your finger tips, provided that you know how to use all that power
Start with something like shipfa.st, and you get an even bigger head-start.
The same principle applies to C++ too. A good library or framework can give you a huge head-start. A bad library is the stuff that nightmares are made of, but good ones are great.
So in the case of Order Hand (my sales tracker), I built it on top of my own partially done web-app framework. When it’s done, it’ll be a bit like shipfa.st, but for C++. That, in turn is built on top of the drogon and lib-sodium libraries. Drogon itself is built on top of multiple libraries. The only one I’ll mention, is sqlite, because I decided to use sqlite databases for ease of deployment.
This is the software stack that turned what could have been months of work into two-weeks part-time. I could write the MVP fast, because it’s built on years worth of software development. The same is true for web apps built in other languages. It isn’t the language, but the libraries and frameworks at your disposal.
Making C++ Easier
So, here’s how you can be effective with C++:
- Cut down on complexity. Choose one build tool. I recommend CMake. Not because I wrote a great tutorial for it (which I did), but because it’s the de-facto standard. It’s so common that you’re going to use it sooner or later. Don’t like CMake, try Meson. Forget the rest
- Learn the fundamentals of programming in C++. Get good at the basics. Do learn how to use smart pointers, vectors, lists, etc.
- Do NOT try to learn every C++ language feature, and every corner of the C++ standard libraries. It’s overwhelming, and you don’t need them. You don’t even have to use Object-Orientated Programming (OOP) if it doesn’t suite your projects. Let me repeat that so you grasp its significance: you do NOT have to use OOP just because you’re using C++
- Look for good frameworks/libraries to use in your projects.
- Practise. Write lots of code. Build stuff, learn along the way. Keep going, and you’ll get good at it. That’s what I did
Or, you can use a different language and platform altogether. Pick whatever works for you. I’ve decided to go with C++.