“Computers are incredibly fast, accurate and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination”
Everything that happens on a computer happens for a reason.
Our job as programmers is to understand the reasoning so we can make the computer do what we want it do. We don’t quite understanding the reasoning yet - but we can build up our understanding by working through sample problems.
Learning to reason about code is kind of like learning how to drive. You might not understand all of the intracies of how the engine actually works - but you build an understanding of how it works by working with the inputs and observing what happens. As you use the car and use the different controls your intution about how the car works builds up.
If we want to understand a piece of code, we’re going to have to reason through it by following some basic steps. Every programmer follows these basic steps in some form. Over time, you’ll find your own process but for now let’s work through these steps.
One of the first things you want to do when you are presented with code is to figure out what it even does. You want use info from the code to help you understand what the code is doing. This list is not comprehensive and you might not need to take all of the steps.
Computers need specific instructions, Humans need simple instructions.
With a basic understanding of the code, we can start form a hypothesis about what the code actually does. We are converting the code into English so we can better understand it.
English: - This function determines whether or not a number is even. - This code computes a specific fibbonacci number.
Not Quite English: - This function takes the variable x and returns true if x modulo (%) 2 is true. - This piece of code uses iteration to sum up x and the previous value of x...
While the “Not Quite English” descriptions are accurate (and in english), they still rely on the language of programming and that makes it hard to work with. It’s much easier for us to reason in a spoken language - especially since we already use English to communicate.
Now that we’ve formed our hypothesis, we need to verify it. There are a few ways we can approach it
How you use the above steps depends on the type of code and your personal preferences. Sometimes it makes sense to attempt to mentally go through the steps above. Other times, it can help to work backwards - that is first attempt to test valid inputs and then form a hypothesis. This is often used when there is a lot of code because it can be faster than trying to come up with a hypothesis from just looking at the code.
Now that we have an understanding of what the code does, lets start using that by turning english statements into code.
3 minutes.
What does the function doMath do? (in english, not in code) (and no math is not an acceptable answer)
What does this do? Try to use the rules from above to form a hypothesis. 5 minutes.
Side note: This is a also problem to practice your enviroment diagraming skills. You should work this out later. Why are enviroment diagrams useful for this class and others!
What about this function that is called “doSomethingElse”. What does it do in english?