Sunday, February 1, 2009

The art of reading code


It’s harder to read code than to write it” – Joel Spolsky

Code is read more often than it is written” – Adam Barr

There is plenty of information about writing code, improvising your code, writing an efficient code blah, blah.. blah.. but ability to read code is an area often overlooked.

Let me start with a simple question. We are going to see 3 program examples and I have only one question, what does this program do? I can give one hint, the answer is not, it depends, we aren’t Lawyers :)


1)

using System;
using System.Windows.Forms;
class Script
{
static public void Main(string[] args)
{
MessageBox.Show("Hello World!");
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine(args[i]);
}
}
}

Code language: VB.NET

2)

main() { printf("hello, world"); }

main( ) { extrn a, b, c; putchar(a); putchar(b); putchar(c); putchar('!*n'); }

a 'hell'; b 'o, w'; c 'orld';

Code language: C++

3)

<service name="GreeterUMO">

<inbound>

<stdio:inbound-endpoint system="IN" transformer-refs="StdinToNameString"/>

</inbound>

<component class="org.mule.example.hello.Greeter"/>

<outbound>

<filtering-router>

<vm:outbound-endpoint path="chitchatter"/>

<payload-type-filter expectedType="org.mule.example.hello.NameString"/>

</filtering-router>

<filtering-router>

<vm:outbound-endpoint path="userErrorHandler"/>

<payload-type-filter expectedType="java.lang.Exception"/>

</filtering-router>

</outbound>

<default-service-exception-strategy>

<vm:outbound-endpoint path="systemErrorHandler"/>

</default-service-exception-strategy>

</service>
<custom-transformer name="StdinToNameString" class="org.mule.example.hello.StdinToNameString"/>
<service name="ChitChatUMO">

<inbound>

<vm:inbound-endpoint path="chitchatter" transformer-refs="NameStringToChatString"/>

</inbound>

<component class="org.mule.example.hello.ChitChatter"/>

<outbound>

<outbound-pass-through-router>

<stdio:outbound-endpoint system="OUT" transformer-refs="ChatStringToString" />

</outbound-pass-through-router>

</outbound>

</service>
Code language: Mule
In essence, all three of them do pretty much the same thing, which is write "Hello World"
Why read code?

Style: look at examples of how others write code

Transition: The code you own today might be owned by someone else and vice versa
Example: Used to figure out how something is done.
Improvise: add to or change the code
Review: participate in formal or informal review
Bug: debug an issue in a broader code base
Interview: developing interviewing skills.
Lets chew on this topic a bit, will talk about reading code in detail in my next blog.

del.icio.us Tags:

Technorati Tags:

No comments:

Post a Comment