MIT 600 part 2: variables are not boxes and healthy habits for programmers

Computer theory is a good way to relax. Computer theory containing sound thinking and healthy advice lets you relax at least two times – the first time as it clears your mind while you are studying. The second time when you apply these principles in your work, since it makes things simpler and more efficient. So I decided to continue the series of posts on the topics of the MIT course I am following through the Open Course Ware (see  MIT 600 part 1: Mapping World to Something Computational). The course is the 6.00 “Introduction to Computer Science and Programming.

This post contains some notes about the second lecture (get the materials here), and these notes can be summed up like this:

1) DO NOT THINK OF VARIABLES AS BOXES. THINK BINDING
2) STUDY DATA TYPES AND DON’T CHANGE YOUR STYLE ARBITRARILY
3) TEST ALL POSSIBLE BRANCHES (EACH SPECIFIC PATH) OF THE PROGRAM
4) EAT APPLES, WRITE COMMENTS
5) EAT BRUSSELS SPROUTS, AND CHOOSE YOUR VARIABLE NAMES WELL

So, we are at lecture two, which is less philosophical than the first one. We get to start learning how to make computers do stuff. To do that we’ll need commands and data (data represents the world, to manipulate the world we manipulate it through the data that represents it). And this is when the healthy, “eat apples and Brussels sprouts” advice comes in.

1) DO NOT THINK OF VARIABLES AS BOXES. THINK BINDING

Data is stored in variables. But Professor Jim Ericson warns that we should not think about variables as boxes to put things in. We should try to grasp the concept of binding. So, when we say x=2+2, we are saying to take the name x and bind it to the value that is the result of the expression on the right. This may look less intuitive to a total beginner than the box metaphor, but it is a healthier programming mind-frame in the long term. If we get used to the concept of binding right away, we want have any problems with things like mutation, and maybe even avoid or lessen the shock before the concept of pointers that used to afflict those to used to thinking about variables like boxes.

2) STUDY DATA TYPES AND DON’T CHANGE YOUR STYLE ARBITRARILY

Then there is the concept of typing, and the basic advice is to have type discipline and good coding/typing style. The discipline is about being careful about testing the way operators handle the types and reading the specs about it, etc. The coding/typing style is about avoiding to change type arbitrarily, even if the programming language is loosely typed.

3) TEST ALL POSSIBLE BRANCHES (EACH SPECIFIC PATH) OF THE PROGRAM

The lecture deals with decisions, branches and conditionals as well. A branching program is defined as “something that can change the order of instructions based on some test. And that test is usually a value of a variable”. This is pretty intuitive. “Even in real recipes, you have things like, if needed, add sugar.” Programming lets us define different versions of the flow, or of a recipe, by adding conditions. We could have two versions of a recipe depending on diets and allergies, or tastes or what’s in the fridge. If, then, else. Else if. What is important is to keep all the branches under check. That’s where the testing is fundamental. We are advised to always test (have a test case) every possible path the program can take. You, obviously, do unit test, don’t you? Sanity checks are called that for a reason. It is not only the code sanity being kept under check, but your own as well.

4) EAT APPLES, WRITE COMMENTS

I keep reminding myself to comment my code. To comment it well. MIT professors also keep reminding me that. “In general, good programming style says you put in comments that are going to be valuable in helping you as a reader understand what’s going on inside of the code. It could be, what’s the intuition behind this piece of code. It could be, preconditions I want to have on input. It could be, explanations of specific things you’re doing. But you need to have those comments there.”

“Now, this becomes a little bit of one of those motherhood and apple pie kinds of lectures. You know, your mother always told you to eat brussels sprouts because it was good for you. Well this is a brussels sprouts comment. everybody goes yeah, yeah, yeah, comments, of course. Of course we’re going to do comments. And they never do. So my challenge to you, and I know Professor Guttag can do this, my challenge to you is, a year from now, come back and look at code you wrote here. Can you still understand what it was you were trying to do? I don’t know, John, if you’d agree, right? If you can read the code a year later, even code you wrote yourself, it’s a good sign that you put good comments in, right?”

5) EAT BRUSSELS SPROUTS, AND CHOOSE YOUR VARIABLE NAMES WELL

Variable names are similar to comments. They help us understand what is going on. There is a long history of different naming styles (see the camel casing Wikipedia entry, for example). Either way, name well. Use variable names that make sense.

6.00 is an interesting course. It is for total beginners, with no programming experience. But the material taught is nevertheless useful for those who are not completely new to programming. For example, the most basic programming concepts, the once that people too often take for granted, are explained in a brilliant and non-trivial way by experts in the field. For active and trained minds the fundamental topics are not boring or to be dumbed down, but are seeds and foundations for more complex questions that will be faced later, so it is important to set the right tone and mindset from the start. MIT Open Course Ware helps a lot with cultivating that kind of attitude, as we will continue seeing in the future posts in this series.

Advertisements

About apprenticecoder

My blog is about me learning to program, and trying to narrate it in interesting ways. I love to learn and to learn through creativity. For example I like computers, but even more I like to see what computers can do for people. That's why I find web programming and scripting especially exciting. I was born in Split, Croatia, went to college in Bologna, Italy and now live in Milan. I like reading, especially non-fiction (lately). I'd like to read more poetry. I find architecture inspiring. Museums as well. Some more then others. Interfaces. Lifestyle magazines with interesting points of view. Semantic web. Strolls in nature. The sea.
This entry was posted in learn-to-learn, online courses and tagged , , , , . Bookmark the permalink.

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