The Nitty-Gritty of Thinking Like a Programmer

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.

I was getting pretty good at manipulating my brain that way, but then I moved onto other projects and therefore other languages.  When I started JavaScript, I had to do tasks similar to the one above, but some things had changed (not too many…JavaScript is pretty similar to Python).  I learned that you couldn’t just tell it to “look at every character,” in the same way you could in Python. You said to it: count every character (give it a position), then tell me what you see if you look at the entire string of characters at that particular position.  Not a huge change, but as the first time I had to get used to a language switch, it was initially pretty taxing.

Then I moved on to making a custom function in Google spreadsheets.  In that case, it was more than finding a new way to think: it was overriding old methods of thinking. I was already fairly familiar with spreadsheet functions, but now I had to find some way to deploy them using JavaScript (using google apps script, which is lacking in tutorials.  People on the web are very helpful, though).  Something as simple as selecting the cells you wanted became incredibly difficult.

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

Advertisements

8 thoughts on “The Nitty-Gritty of Thinking Like a Programmer

  1. codeinfig says:

    not to miss your point, but you could replace all instances of “\n” with “,\n” using stringvariabletomodify.split(“\n”).join(“,\n”);

    to get closer to the idea of your post, i did a pretty “good” (ridiculous) job avoiding functions for years, until i learned python. python without functions doesnt work very well, imo. so i started writing more functions, which turned out to be really great. i use them in javascript also.

    i dont love every change in a new language, but sometimes it is exactly what we need.

    1. seasaltandvinegar says:

      Yeah, built in functions are great. Like I mentioned, once you start exploring them, they can be much better! I was mainly addressing the initial process where you have no idea that any helpful tools exist.

  2. thewishingwell says:

    When I was a kid I didn’t understand fractions. My teacher kept drawing apple pie on the board and going, This piece is for Johnny, this piece is for Suzy, how many pieces out of the whole do we have left? I went home yelling to my mom that I didn’t understand what pie had to do with math. I don’t remember when the lightbulb about fractions actually clicked, so I’m not sure this is a helpful story. I guess the point is, what works for some (or most) doesn’t work for everyone. I admire you looking at the problem of coding from all angles.

    1. seasaltandvinegar says:

      I love the story! It’s so relevant for so many people, especially me when I was little (and still now). After we worked through word problems and came up with the equation 5 + 2 = 7 or something I would always ask “Well why didn’t they just tell us to do 5 + 2 in the first place??”

    2. codeinfig says:

      its funny how these things get left out. its fine to teach the concrete and the abstract, but sometimes youre going to have to explain the connection between them. one of the things ive seen happen is people mistaking examples for rules: thinking a for loop is the basic structure of every program (if you accidentally teach that, you havent used enough examples.)

      very true about what works for whom. i bet you at least one person wouldve gotten the pie thing if there was a candy bar (bar graph vs pie graph) example also. better yet: a second example that wasnt about food.

      1. thewishingwell says:

        Hmmm interesting I hadn’t thought of it that way: concrete vs. abstract. Pie definitely abstract! I love the idea of a candy “bar” graph! And yes I think I finally understood fractions due to my mom explaining it without apple pie involved. Haha.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s