Two Weeks of C++
On October 31st, I made the impulsive decision to learn C++.
Two weeks have passed, and I’ve spent nearly every day programming in the language, totaling over 25 hours of writing functioning (yet terrible) C++ code. It’s been a wonderful time for me, and I haven’t enjoyed coding this much in a while.
However, for all I know, these gleeful moments may stem from the fact that I’m in a honeymoon phase with the language. Give me a few weeks, and my attitude towards and appreciation for C++ may change.
Hopefully, I will continue to love the language in a few weeks’ time, but there’s no way to know. Since I cannot predict the future, I want to focus this post on why I’m learning C++, my intentions with it, and a reflection on how comfortable I am with it.
Why I want to learn C++
Despite the spontaneity of my October 31st decision, there were underlying factors that motivated it.
I’m in the midst of an obsessive math phase, where I constantly crave learning a new linear algebra or calculus concept. Even though I graduated from college with a math degree, I forgot most of what I studied. So, the deeper I delve into the magnificent world of mathematics, the more I struggle to comprehend advanced topics.
There is an undescribable beauty and satisfaction I feel when I solve difficult math problems. While I’m not a genius, nor am I the smartest person everywhere I go, I recognize the potential I have to use math in the future to solve problems and make an impact.
What problems exactly? Well, I don’t know. All I know now is that I want to immerse myself in an applied mathematical field one day in the future.
Most (if not all) math-related careers require programming skills. We no longer have to spend entire days handwriting equations and solving problems on paper. Computers work for us, and we can manipulate them to perform all the computations we deem necessary. And they’ll do it much faster than a human.
For most math-related fields, Python is sufficient. A thorough understanding of libraries like NumPy, SciPy, and Matplotlib will take you a long way. Throw in data libraries like Pandas or Spark, and machine learning libraries like TensorFlow, PyTorch, or scikit-learn, and your options expand further.
Regardless of your chosen libraries, what matters is that combining strong backgrounds in mathematics and Python opens many doors for you.
But just because Python is a versatile language doesn’t mean it’s always the best choice. Sometimes, you need to write programs and algorithms that require fast execution and low latency.
That’s where a language like C++ enters the room.
While not as popular for math as Python due to its relative complexity and difficulty to learn, C++ is used across many math and quantitative fields.
Again, I don’t know what I plan to do with math and programming in the future. So, for all I know, Python may be all I need. Unless I go into a highly quantitative field dependent on high-performance computing, C++ may be a “nice-to-have” skill.
Thus, my math obsession is only half the equation for why I’m learning C++.
The other reason I chose the language is due to its proximity to operating systems.
As a systems programming language, C++ teaches you how software interacts with operating systems and computer hardware. It forces you to be conscious of memory management and resource allocation to write efficient programs, whereas Python abstracts these away from its users.
I’ve always wanted to learn a lower-level programming language, so I could better understand how technology works, specifically at the intersection of hardware and software.
I could have chosen another low-level language like C or Rust, as those seem to be more popular systems programming languages. But since C++ has roots in mathematical fields and is a low-level language, it’s an ideal choice.
Where I’m starting from
Fortunately, C++ is not my first programming language. So, I can skip the tedious phase of learning fundamentals such as variables, loops, conditional statements, input/output, file manipulation, data structures, abstraction, and so on.
I may not be exceptional at all of the things I listed above, but I have a strong understanding of what they do and how they work. So, it’ll help me on my C++ journey.
Even though I’m not approaching C++ as a complete beginner, I’m also no expert. I’ve done my fair share of Python projects over the last few years. It includes things like a finance tool that helped me track my personal financial transactions, a learning tool that scraped posts from my old blog and sent me automated emails to review them, a CLI tool that automatically backed up and saved files on my laptops, and a data backfill project that extracted data from PDFs and used an LLM to identify specific attributes before loading into a database.
I’ve done enough projects to consider myself a hobbyist. But I’ve never built or contributed to a production-level program with active users. So, while I’m competent and confident enough to begin new programming projects, I have a long way to go before I can dare to call myself a professional.
My lack of production-level contributions doesn’t hold me back from having a strong foundation in software development, however.
I work closely with a software development team at my company, where I build data models and SQL application views for their programs.
So, I get to sit in on many meetings and technical discussions with the team. Hearing them analyze the software development problems they encounter and the solutions they implement helps me construct a conception of what real-world engineering looks like.
Even though I lack hands-on software development experience, my exposure to the field gives me a good starting point for learning a new language and (hopefully) building valuable applications of my own.
What I’m working on in C++
Studying math over the last few months helped me identify the three ways I learn the subject best.
The first, and most obvious, method is doing practice problems by hand. Despite most of the applied math you encounter being done through code, solving problems on paper has its purpose. Unlike other subjects, where you can simply memorize definitions and facts, you learn math differently. Yes, you can memorize formulas and theorems, but to truly understand how calculations work, you must solve a lot of problems and write out each step along the way.
The second method I discovered is writing. As with everything I learn, writing blog posts helps me refine concepts and understand them intimately. The writing process forces me to clarify my thinking and take a first-principles approach to communicate what I learn.
The third method, and one of the main reasons I’m learning C++, is translating math topics to code. By writing algorithms and scripts that automate mathematical computations, I force myself to understand concepts at their lowest granularity. If I don’t know their ins and outs, there’s no way I can write a program to perform the desired calculations. So, translating math to code is a great way for me to break down math concepts and digest how/why they work.
Before deciding to learn C++, I wrote a Python program that performed common vector and matrix operations studied in linear algebra.
I figured that converting my Python scripts to C++ was an engaging way to start learning the language.
This made me want to create my own C++ linear algebra library with implementations similar to those in Python’s NumPy and SciPy libraries. My version is called Claire (A Custom Linear Algebra Implementation and Representation Engine).
I’m not doing this to build a competitor to the well-established, well-optimized Python libraries. Contributors to NumPy and SciPy have years of programming experience over me, and I’m sure most of them have stronger math backgrounds as well. So, it’s nearly impossible for me to build a better math engine.
Instead, I’m doing this project to simultaneously improve my math and C++ skills.
My thoughts so far
Since most of my prior programming experience is with Python, I compare C++ features to their Python counterparts, if they exist.
Going into this journey, I knew that Python was designed to provide programmers with an easy-to-understand, friendly syntax. So, I feared the more intricate C++ syntax would take time to grasp, but it didn’t. I quickly picked up ideas such as the nonessential indentation and the use of curly braces around function bodies and control flow statements. After writing a few basic scripts, I had no issues with C++ syntax.
One of the biggest differences between the two languages is dynamic versus static typing.
Part of what makes Python easy to use and loved by many is its dynamic typing. You can define variables, parameters, or return values without limiting their data types. This means it’s easy to reassign a value to a new data type without encountering execution errors.
But C++ prohibits this. Its static typing requires you to specify one data type for a variable, parameter, or function return value. Once you select a type, you cannot change it during your code execution.
I find the static typing not only intuitive but preferred. Yes, I have to write more code, but it makes it easier to read scripts and understand what different objects or code blocks do.
Another feature new to me is compiling code. Since Python is an interpreted language, you can easily execute a script from the terminal after saving it. That’s because on execution, the Python interpreter parses your code, compiles it, and then executes it.
On the other hand, C++ forces you to compile your code before executing it. This precompilation translates C++ syntax into machine code that an operating system can read. So, when it’s time to execute code, since it’s already compiled, the scripts tend to be faster and more efficient.
Learning how the compiler functions under the hood is beyond me at the moment. I'll have to research further if I want to understand how compilers work. But, for now, I’m able to manually compile and run C++ projects from the terminal, without relying on an IDE.
Most of the time, my code successfully compiles. But I experience frequent compilation errors. The error messages are convoluted, so it takes a while to read and debug them. I’m slowly improving my ability to read compiler error messages. Since errors will continue to occur as I write more complex scripts, I assume this skill will improve over time.
There are more C++ intricacies that I understand at a high level but do not have a strong grasp of yet. These include header files, operator overloading, memory management, pointers, class constructors, and more. I’ve figured out how to implement these at a basic level within my linear algebra library, but I need to study them more.
As I continue learning C++, I expect to feel more comfortable with the intricacies and learn how to properly implement them in my projects.
Tools that help me learn
For the first 7-10 days of my C++ adventures, I relied heavily on ChatGPT to teach me various topics and debug my code.
When I asked ChatGPT to debug and rewrite code, I didn’t always understand the code it generated. I sacrificed quality for speed.
ChatGPT’s code may have corrected errors and made my code work, but my lack of C++ experience meant I had no clue whether it was efficient, safe, or good practice. LLMs learn to write C++ code through whatever resources or content are freely available on the internet, the majority of which are likely trash. So, if an LLM has garbage input, it’s sure to have garbage output. This made me wary of what to trust.
When I asked ChatGPT to explain C++ topics, it did a good job filtering out bad explanations and communicated core concepts concisely. This pleased me.
I originally tried using trusted documentation and reference pages, but the unfamiliar C++ terminology made what I read perplexing. I struggled to learn, so I took the shortcut of relying on ChatGPT to fix my code.
LLMs are great for learning and debugging when you’re already experienced with a language, but if the goal is to master a language, relying too much on LLMs inhibits the learning process.
Now that I’m more familiar with C++ terminology and have a broad understanding of all features, I can more easily read documentation and reference pages. So, I’m trying to rely on those more than LLMs for learning. The process may take longer, as I have to manually parse online resources to find the good ones, but figuring out solutions on my own improves my grip on the language.
I will still use LLMs moving forward. However, I will limit my dependence on them and learn C++ the hard way, as it’ll pay off more in the future.
What’s next with C++
Two weeks is a short time to spend with a new programming language. I’m far from classifying myself as a competent C++ programmer, and I expect it will take many months or years until I can make that claim.
While I enjoy the language now, I recognize I may not stick with it forever.
As mentioned, I’m mainly learning C++ to improve my math skills and learn how to combine math and programming to solve real-world problems.
Once I determine where I want to apply my math skills, I may discover that C++ is excessive. Perhaps Python would be sufficient.
If I do require C++ in the future, then great. I’d thank my current self for starting my C++ journey now. But if C++ isn't needed and Python alone suffices, I’d still thank myself for this journey. Learning the language now and exploring ways to apply math to programming will improve my math skills and teach me more about critical software development skills I may need in the future.
Although it’s possible for my time with C++ to be short-lived, I hope that doesn’t happen. I’m enjoying the language so far and would love to leverage it in the future to solve problems.