without an e

When you read code, imagine typing it by hand. [01/06/2007 19:11:49]

Whenever you are confronted with unfamiliar code that you are having trouble understanding, imagine yourself re-typing it by hand.

For example, I'm trying to learn a little erlang for some WebDAV work I'm doing with YAWS. The following code is from the erlang course:

	-module(mathStuff).
-export([factorial/1, area/1]).

factorial(0) -> 1;
factorial(N) -> N * factorial(N-1).

area({square, Side}) ->
Side * Side;
area({circle, Radius}) ->
% almost :-)
3 * Radius * Radius;
area({triangle, A, B, C}) ->
S = (A + B + C)/2,
math:sqrt(S*(S-A)*(S-B)*(S-C));
area(Other) ->
{invalid_object, Other}.

Stop for a minute and imagine typing the above code on a keyboard. It doesn't matter if you know erlang or not. In fact, this technique works even better when you're learning a completely new language.

Come on, just do it. :)

Now. Did you notice that sometimes functions in erlang end with a period, and sometimes they end with a semicolon? Did you see why? Do you remember which character introduced the comment? Did you notice that some things are capitalized and some things aren't?

When I first looked at the example, I didn't notice the semicolon-vs-period distinction. My eyes went right over it. But when I imagined typing it, I slowed down enough to notice the details. The semicolons jumped out at me immediately, and I had to stop and go back to the previous section where it introduced the syntax for functions to make sure I understood.

This is just a neat mental trick you can use to make sure you're really understand the code you're looking at, and not just glancing over it. And of course, you don't have to actually type it - in fact, I started doing this a long time ago for reading programming books away from my computer precisely because I didn't have a keyboard around.

I was put off by the syntax of Erlang for a while too, coming from Python. I've come to really like the language and (most of) the libraries built around it. It's a pretty damn powerful tool in my server-side arsenal given its concurrency features and general performance. Just don't give up too soon! :)
by Bob Ippolito [01/06/2007 22:16:02]
Actually, when learning a new language/tool, I tend to actually type the example code, even If I have a copy & paste version available. Like you say, you tend to pick up on the fine detail you would miss otherwise.
by Sean O'Donnell [01/07/2007 00:59:16]
Wow.. this is a nice trick. I used to actually type in all the examples in the book when I learn a new language. I think I can now learn new languages even when I am far from my computer.
by Muthu Kannan [01/07/2007 01:02:32]
Hmm, I haven't thought of it like this. :) When I code, I do try to put myself in the situation of someone not well introduced in our code and comment according to that. I can also edit out others comments where clearly superflous in our code and add in other more explanatory ones for parts they may be completely missing. It's not because I think of myself better than others, but because you usually have a much easier time commenting others code right than the person writing the code in the first place, at least if you know you understand it.
by Jonas [01/07/2007 16:43:24]
Your math contains a bug when calculating circle's area. "3 * Radius * Radius;" should be something like: "3.14 * Radius * Radius;" (don't know how to input float in erlang) PI != 3, PI == 3.14159265 Yes, it's commented that "almost" but you should always be exact when it's possible, hell define PI or something :)
by Hosting Guy [01/07/2007 16:48:35]
Awesome article and a beautiful way to think out the code =)!
by Guru Panguji [01/07/2007 17:06:53]
To: Hosting Guy. That's probably why michal put the '% almost :-)' comment up there. There is another way that will get you closer if all you have to work with is integers: Radius * Radius * 22 / 7;
by marlin [01/07/2007 17:24:36]
There's a book called "Code Reading" that follows similar concepts to fully understand the more subtle parts of a language. Very interesting.
by willpost [01/07/2007 17:51:08]
I agree with Sean- I type example code in manually as well, and I'll sometimes change variable names to keep myself on my toes. Great tip.
by Ian Gowen [01/07/2007 19:28:06]
Oh the irony! Have a look at the comments in your "How to fix the internet" post: http://withoutane.com/rants/2003/08/how_to_fix_the_internet You are letting spammers gain search engine rankings above legitimate sites by letting their spam into your comments area, and keeping it there. Its funny how saving the world always seems to focus on the outside world first, when we have little influence over the outside world, but great influence over our little piece of it.
by xoc [01/07/2007 19:28:24]
"Exact" values? I don't think that PI == 3.14159265 is a valid expression, either. Anyways, sounds like a good plan to speed up learning.
by veon [01/07/2007 20:36:38]
I had a laugh at "PI == 3.14159265" ... you are SOOO wrong! PI == 3.141592653 !!!
by Rowan [01/07/2007 20:51:13]
PS. If you beleive me, I have a bridge to sell you! ;)
by Rowan [01/07/2007 20:52:12]
xoc: You're right. My comment system really really sucks. I haven't even set up a tool to edit/delete comments. hosting guy: Thanks. But I didn't actually write this code. As I said, It's from an introductory course on erlang.
by michal [01/07/2007 21:20:23]
Nice trick - I have a tendency to this while reading text when my mind wanders...I've tried to avoid it at all if possible, although for code it makes perfect sense.
by Anthony Ettinger [01/09/2007 20:59:54]
Nice one - I've never seen this technique mentioned before but I'm sure it could really help in pesky 'code-maintenance' sessions. Anything that can make taking over someone else's code less painful has to be a good thing :)
by sim [01/24/2007 09:01:04]
Post a comment:
name: (shows up on site)
url: (shows up on site)
mail: (for michal only)
no html allowed yet. sorry: