Want to master programming? Try this process.

Author David Lower   .   07 August 2017

Having lived in a foreign country for many years I mastered the art of 'learning' a new language. Applying this mindset has also helped me learn the language of programming. Here are my tips.

My exploits into Javascript unearthed a very clear revelation recently.

If I want to understand what it is I am really trying to build — I must first ‘think’ in the language with which I build.

Stumbling about from one problem to another, trying to understand how arguments work, functions execute and all the above. I concluded quite simply, I did not know the basics well enough.

It is all very well and good copying code and watching tutorials (nothing wrong with that at all). But actually thinking a problem through from beginning to end is a totally different beast.

Last week I spent 5 hours trying to reverse a string. As a newbie, I had no idea how to do it. I refused to even look for a hint, let alone the answer. In the end, I managed to achieve 90% of the task.

That experience broke me in some ways. I felt such strong emotions ranging from failure and frustration to hopelessness and stupidity. You can’t help but shake that overwhelming sense of ‘what is the point’?

So what was the revelation that I had?

Think in your head what the code is actually saying to the computer.

7 years ago I moved country and with that, I had to learn a foreign language. There comes a point when you go from being able to speak short sentences to make yourself understood — to being able to have a fairly normal conversation. Even though your vocabulary doesn’t increase much, the divide between broken sentences and flowing conversation is huge.

If you want to ask a question or make a statement, you first think that question/statement aloud in your head using your native language. Then you translate it into the new language.

In time, however, you begin to think in the foreign language and it is at that point your communication goes from stagnant sentences to flowing conversation.

This same cross over also occurs in programming. When we learn to actually ‘think’ like the computer does, then we will really enter into the realms of mastering that language. So how do we achieve this?

1 Speak out load the different elements of the code.
2 Remember computers read from right to left.
3 Use the proper language when speaking out load.

This is a problem taken from FCC (freecodecamp).

reverseString("Greetings from Earth");
function reverseString(str) {
    var arr = [];
    var array = str.split("");
    for (i = str.length - 1; i >= 0; i--) {
        arr.push(array[i]);
    }
console.log(arr.join(""));
}

So if I was to read this out load and also bare in mind going from right to left, what would it sound like?

var array = str.split("");

Take my string, split each character within the string (that is what the "" does) and make a new string with each character. Put all the new strings together inside a new array (.split method does this). This is what it would look like.

["G","r","e","e","t","i","n","g"...]
arr.push(array[i]);

For each value of i repeating through the loop (above), take that value as the position in the array and push out that element into the new array I created earlier.

Although I have done a horrendous job of trying to actually explain what I mean. Hopefully, you get the idea. Break each component down, understand what it does, speak it out and logically follow that order through. Loosely go from right to left.

If unsure, test it. Test each section with a console.log() to see if it gives the result you were expecting. Try and figure it out before searching for the answer.

Figuring out these tiny details, although extremely painful in the moment, does produce a wealth of understanding that doesn’t become apparent until after the problem.

Do this process again and again and again. Over time it will become natural and you will start to think like the programming language itself.

All the best on your coding journey.