A popular thought experiment that I often give my students who are early in their programming careers is the following:
There is a basket in front of you with 10 different papers inside. Each paper has a random number on it. Your job is to add those numbers up and give me a final sum at the end. Write out the steps. Before you continue reading, take a second to think about it. Just a second.
Simple enough, everyone thinks. The most popular answer that I usually get is "You add them all up one by one..". Which technically, yes, you can only add them up one at a time. But there is no equivalent to that in code I'm afraid, aside from:
And this is the way that most people think, so do not feel bad if your answer was "I add them all up". In order to survive as humans in our day to day lives, much of complexity of everyday living has to be taken care of for us by our subconscious mind and our autonomic mechanisms inherent in our physiology. Let's take driving a car for example. Without complexity, we can simply state that the gas pedal makes the car move forward, and the brake stops the car. The steering wheel turns the car left or right and the shift knob changes gears. It's a pretty accurate and high level representation of what driving a vehicle is like. And that's all we have to know in order to make it work.
If you were to write those steps down on paper however, you would not have a functioning car. But you would have an idea as to what a car is. For the most part, we live on the periphery of complex logic.
If you are a programmer however, you must be involved with what is inside that periphery. As a programmer, your job is to take the requirements, such as "Drive a car" and to break it down into as many steps as humanly possible in order to create that thing or that event or that experience. And then to take those events which you think you found, and to break them down even further. Over and over, until you have actionable items that you can accomplish.
Let's take driving a car for example. Suppose you were building a video game with car driving mechanics. Making the car have forward movement, could technically involve pushing the pedal in (in this case the controller analog stick perhaps) and then measuring the displacement of the pedal from its rest position to it's new position. Once we have that, we can calculate how much more fuel to spend, which is turn would effect the overall RPM's and speed. The overall new calculated speed, would probably then have to be reduced based on the amount of "friction" that we calculate to have, again based on various other factors, and we would get a new actual speed. We would then use this speed to essentially render our environment faster or slower. Our car wouldn't change at all. But the environment would either be coming at it faster or slower.
I'll say this, this is not a natural of way thinking for most. Some are naturally gifted with this ability from a young age. I was not. But through years of learning and practice and building it has become almost second nature to deconstruct actions and events into their smaller components.
Going back to our first example, I'll break down what a potential answer could be for summing all of the numbers in a basket. The way I like to teach it, is to just treat it as a real-world scenario. What would you actually do, if this were a physical and tangible request. You could start by grabbing a piece of paper in order to keep track of the total. You would find a pencil and start with the first number. You would write said number down as it is the first one. Then you would grab the second number, and write that down. You could add it to the current total and then keep moving towards number #3. And you would repeat this process until you reached a solution.
That is a more programmable answer. It has variables, conditions, logic and a looping mechanism, which is why I enjoy asking the question. It ties in many programmatic principles into a very simple real world example that most can follow.
Programming is a much more complex skill that many realize going into it. It changes the way that you perceive the world and vastly improves your overall problem solving ability both in and out of the digital world. But as I mentioned, it takes time, patience and dedication to get to where you need to get to.
Enjoy this post?