Programming is a head fake (Deconstructing Logic)
Have you watched Randy Pausch's Last Lecture? You should if you haven't. One thing that I remembered from that lecture is the advice he left for his son: football is a head-fake. It's not about throwing a ball around with a bunch of people and the cheerleaders. It's about teamwork. You're building up a work ethic and you're figuring out how to work closely with other people.
Programming is the same way. When you initially read code, you may see something that looks like Greek or Cantonese. I remember feeling that way when I went into my first math course in school. But programming is a head fake. It's actually just logic in writing. In order to be good at writing out logic, you need to be able to effectively deconstruct large problems into smaller logical steps.
I asked all of my students to complete an exercise with me that looked like this:
Create an array of the LA-based students in Bloc and count the number of times the letter "a" appears in all the names.
This is logically a lot to think about for a beginner. Some people will freeze up and they have no idea where to start. It helps when I tell them to think of this in terms of little logical steps.
When you put on your socks in the morning, you don't just "put on your socks"--that's a high level description of what you do, but your body is making hundreds of decisions in the process.
First, you have to find your socks. Are they in your dresser? Are they in your closet? What kind of socks are you wearing? Do you need to put them on before or after you put on your pants? So your brain uses everything its learned to stand up and balance yourself, and then you put a left foot in front of the right one, and you repeat this movement and balancing act until you reach your dresser. Your brain tells your right (or left) hand to move outward, and you bend over and grab two socks.
Your breathing adjusts as you move, your heart rate increases, and your pupils dilate accordingly based on the amount of light you're seeing--all of these are involuntary things happening as part of putting on your socks. You then sit down, you lift on leg up slightly and reach over and pull on the sock. Each of these steps could be broken further down into many more steps.
Any process you go through has a series of more complex steps behind them. This example is one where you need to break the problem up into many smaller pieces. The people who are able to think logically about a problem and deconstruct the logic are able to move through the programming exercises the best.
So in this example, you start out with an array. You have to iterate over each element in the array. But doing this forces you to think about each element individually. So I usually tell students to open up irb and type something like this:
"Amara"
It simply outputs the same string. Now how do we count the number of "a" letters in this string? Most of us don't know, so we Google it or we ready through the String Class and find out that we can use a count method.
"Amara".count("a")
And this unexpectedly outputs "2". That's because the first "A" is capitalized. So now we need to remove the capitalization, and we again do more searching. We find that we can use the downcase method.
"Amara".downcase.count("a")
Finally, we get the expected result. Now we realize that we have to go back and make this work in our iterator.
students.each do |student|
puts student.downcase.count("a")
end
The problem is it's just outputting a sequence of numbers. We need to keep track of them. So let's use a variable, since we know variables can store things.
students.each do |student|
count = student.downcase.count("a")
end
But if we output count at the end, we just see that it's "1". To show some context, our array was created with this statement:
students = %w{Amara Tom Ryan Khoa}
This is a point where most beginners struggle. Most of my students think that count is being created as a new variable during each iteration of the loop. I explain to them that variables are scoped to the method level, and the variable is actually being overwritten. I'll notice that people will still struggle here to deconstruct the problem further.
If you break this up logically, you notice that you need to prevent this overwriting from happening. You can do that by simply incrementing the count variable:
count = count + student.downcase.count("a")
Which is the same as:
count += student.downcase.count("a")
And finally, the student will realize that they're returning the occurrences of the letter "a" across all of the elements in the array. This is how applications work. They're like hundreds of these tiny little puzzles progressively building up towards one larger abstraction or logical idea. This usually finds itself being labeled as an "elevator pitch" or something similar, where you get across in a few sentences what logic your application performs.
Programming is all about logic. You have to train yourself how to think logically in steps, since that's what a computer is built for. Every complex problem can be deconstructed into simpler, logical steps.
Bloc runs intense, 12-week online web development bootcamps. Learn more...
Liked this post? Subscribe to our newsletter.
