How Best to Learn a Programming Language

The main thing I am currently working on outside of my job is improving my C++ programming skills. I learnt C++ 98 when I was studying for my master’s degree but have been working in Unity or UE4 blueprints ever since graduating. Now, with the possibility of getting moved onto a C++ game development project looming in my future, I thought now was a good time to get up to date with the latest features.

I know from experience that learning a programming language is a daunting task, especially if you are going about it on your own. There is also the potential to make a lot of mistakes around learning to code. I made a lot myself. Therefore, I wanted to share how best to learn a programming language by sharing the system I am going to be using to learn C++.

Find Good Learning Material

The first thing I do when looking to learn something new is to research and find the best resources. From my experience, programming is best learnt through a book.

I have previously learnt from online resources such as YouTube videos, online courses and articles, but I have found that books tend to be better. They often supply you with exercises and questions to help you test your understanding and books often go into a lot more detail. The more you understand how a language works, the more tools you have to solve problems, and the easier it will be to debug issues.

For me trying to learn C++ I purchased C++ Primer 5th Edition, which is a beginner orientated programming book. The main reasons I went with this book is that it is highly rated and offers practice exercises throughout. This is a must when trying to learn to code. The main downside is that because of its age it only covers C++ 11 and misses all the new features introduced in C++ 14, 17, and 20. Considering C++ 11 is the basis for modern C++ I hope that getting familiar with new features won’t be as painful as transitioning from C++ 98.

Also, in case you are wondering, the reason I went for a beginner orientated book as opposed to a reference manual is that despite having written a lot of C++ code, I felt like it wasn't very good. At university, I was taught C++ 98 and wasn't given as much time as I would off liked to get to grips with the language. Also, there's that minor problem of having written very little C++ code in the last 5 years, so starting again from scratch felt like the right thing to do.

Understand and Remember

Once I have picked a resource, I want to learn from I set as much time aside as I can to learn it. For me I try to do a little everyday but obviously that isn't always an option for everyone.

I don't do anything fancy when it comes to trying to understand and remember the content I am learning about. I make notes about key points in my own words and then create questions I can use to test my understanding. I will repeatedly review these questions overtime until I feel I can confidently answer them all correctly without looking at my notes.

Again, everyone learns differently, so if writing notes doesn't help you, then I wouldn't bother and you can use other techniques like highlighting or flash cards.

Practice with Little Coding Experiments and Exercises

As I am making my way through some learning material like a book, I will often try to practice as much code as possible. I often choose books which contain exercises so I don't have to think them up on my own.

The end goal for my learning is to write programs in the language that I am learning, so it is better if I can complete coding exercises as I am reading a book. This is a much better approach than trying to read an entire book before ever writing a single line of code.

Exercises can help with simple things like reinforcing your ability to remember the syntax, such as creating a lambda or writing a for loop. They can also provide us with a way of testing if we understand the basic concept by asking us to solve a small problem like using a for loop to loop through an array of values.

If the material I am learning from doesn't have exercises or I am still struggling to understand something, then I often start experimenting with code to help me figure things out.

As a simple example, let’s say I have just read about how to write a for loop in C++. The first thing I can do is to simply try and write my own for loop, maybe doing something simple like printing out the variable being incremented in the loop like below.

for(int i = 0; i < 5; ++i)
{
   cout << i << endl;
}

If I feel like I understand the basics of a for loop I can then play around and trying to solve different problems. I could look at counting down instead of up. I could look at using a for loop to iterate through the values of an array. I could just start removing parts of the for loop like the condition i < 5 and see what happens. Once I feel confident, I can move onto the next thing.

💡
I would highly advise you to make sure you complete any programming exercises yourself and if you are stuck, look for help not answers.

Complete Projects

Besides working through exercises, I will often complete a project that tests my understanding of all the knowledge I have gained. I feel this is especially important when learning a programming language as it helps you turn solutions to problems into code.

I would describe a project as a program you create that is larger than a single exercise and has a real-world problem solving. For example, in the last section, we looked at learning how a for loop works by printing the loop count. In contrast, a small project could generate a number at random and then you could ask a user to guess the number by inputting a value into the console. A bigger project could be to read in text from a file and to perform queries on that text, such as seeing how frequently a word appears.

The fundamental difference is that when you are taking on a project, it is not immediately obvious what language features you need to use in order to solve the problem. You have to break down the project into tasks and then figure out for each task how to turn that into code.

I would highly recommend that you try and keep things simple and within your skill range when picking a project. It can be exciting to try and build something cool like a game, but if you aren't familiar with making games then trying to do that alongside learning a language might become overwhelming. For me, I am going to be making small games for my projects to help me learn C++ but that is because I am comfortable making games.