Write Programs for People First, Computers Second

There are several computer books that have become classics. One of these is Code Complete by Steve McConnell. The first edition was written in 1993. That goes back to when I started collecting a paycheck as a professional developer. And it precedes classics like the Gang of Four’s Design Patterns (1994), The Pragmatic Programmer: From Journeyman to Master (1999), Agile Software Development: Principles, Patterns, and Practices (2002) and Clean Code (2008). Some consider this book to be the first collection of coding practices.

In this book, McConnell stresses the importance of readable code.

codecomplete_readable

He notes that writing readable code is one of the things that separates the great coders from the rest.

A great coder [level 4] who doesn’t emphasize readability is probably stuck at Level 3, but even that isn’t usually the case. In my experience, the main reason people write unreadable code is that their code is bad. They don’t say to themselves “My code is bad, so I’ll make it hard to read.” They just don’t understand their code well enough to made it readable, which puts them at Level 1 or Level 2.

Even before Code Complete, the book Structure and Interpretation of Computer Programs, written by Abelson and Sussman, was published in 1985. The full text of the book is available online (link). The preface to the first edition (link) contains the oft repeated line:

programs must be written for people to read, and only incidentally for machines to execute.

Why is readability so important?

As famed developer Joel Spolsky notes: It’s harder to read code than to write it.

We have all been there. We saw a problem, rolled up our sleeves, and wrote some code that we thought was quite clever. Maybe something like this (credit for this gem goes to this code golf puzzle).


"Gdkkn\x1fVnqkc".split("").collect(&:succ)*''

Or maybe this (modified from a DailyWTF sample)

var m = function(p1) {
	if (p1 > 0 || p1 < 0) {
 		return parseInt(("-" + p1).replace("--", ""));
	}
	else { return p1;} };

And we walked away feeling like this

solo_i-rock

Unfortunately, the rest of our team took one look at it and reacted more like this

solo_shooting-self

Why? Because the code is hard to read. And if it is hard to read it will be hard to understand. And if it is hard to understand it will be hard to maintain.

Bob Martin explains this more in Clean Code, noting that we spend more time reading code than writing it.

Indeed, the ratio of time spent reading vs. writing is well over 10:1. We are constantly reading old code as part of the effort to write new code.

… There is no escape from this logic. You cannot write code if you cannot read the surrounding code. The code you are trying to write today will be hard or easy to write depending on how hard or easy the surrounding code is to read. So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read.

Advertisements

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