More than anything else, learning how to code is a lesson in thinking. I know this sounds kind of trite, but I’ll try to make it more specific.
Coding is all about giving the computer directions (duh), only it doesn’t understand the directions in the same way that humans do. You have to find creative ways to make it “see” what you see automatically.
For example, let’s say that you have a poem whose poet has mistakenly forsaken the comma at the end of each line, and you wish to add it back in. You could just do it yourself line by line, but let’s say it’s a really long poem and you want to write a Python function that can do it for you. By normal human thinking, you would tell the computer to “add a comma at the end of each line.” But then you realize that you have to tell it what the “end” of a line actually means. Fortunately, there’s a character for that. We see a break in the line, the computer sees “\n.” So we tell it to look at every character until it finds the “\n,” but it doesn’t understand if you tell it to put a comma there (fine, there are some functions that can do this, but I’m assuming that we’re beginners here). So then you realize that you have to tell it to find the position of the “\n,” and finally, finally, you can add your comma there. Yep, it’s definitely a new way of thinking.
Let’s say you wanted to select the first 5 empty cells in a row and put the numbers 1 – 5 in them. Well first you have to find where those empty cells are…but that’s not enough. Then you have to find the “range” of those cells. Only then can you add your numbers into that range.
One of the languages I’m working on now is much weirder than anything I’ve come across before–it’s a language of filters. Basically you use it if you have a huge amount of text and you want to make it more readable, or you want to pick out only certain parts of the text. The thinking gets even freakier. Trust me. Sometimes you have to write things that look like an alien language (look up regular expressions if you want to know what I mean–they’re actually pretty simple and cool).
I’m sure something that challenges my thinking even more will come next, but I guess that’s the life of a programmer.
Have you ever noticed the need to change your thinking for problem solving? What are some tricks that you use to make it easier?
Image from dilbert.com