Programming Psychology - Return home early
Posted by Felix Geisendörfer, on Apr 25, 2008 - in PHP & CakePHP » Testing, Debugging & Refactoring
Hey folks,
this is an experimental post on the psychology of programming. As of lately I have become very interested in the thought processes that cause us to write certain code. Because I believe understanding the patterns in your own thinking will by far make the biggest impact on how good you will get as a programmer. Forget design patterns, forget unit testing, forget all those functions you know. Important is to question why they exist and how they could be improved. I'm not saying you cannot be a good programmer if you do not care about this. But I am saying you will most likely never exceed the skills of all the other programmers out there. You will more or less write the same code everybody else does.
In the scope of this post I will try to explain a very simple thought pattern that I am calling "return home early" that evolved out of me analyzing my coding habits. The simplest way for you to follow the thought process is to have a look at the following bad code:
-
function edit($id = null) {
-
$this->Post->set('id', $id);
-
if ($this->Post->exists()) {
-
$post = $this->Post->read();
-
if (!$this->RequestHandler->isGet()) {
-
if ($this->Post->validates()) {
-
if ($this->Post->save($this->data['Post'])) {
-
$this->Message->add('Post was saved successfully.', 'ok');
-
} else {
-
$this->Message->add('A db problem permitted your data from being saved', 'error');
-
}
-
} else {
-
$this->Message->add('Post was saved successfully.', 'ok');
-
}
-
} else {
-
$this->data = $post;
-
}
-
} else {
-
$this->render('404');
-
}
-
}
What you see is a typical Post::edit action like the one used on this blog (in fact I derived it from it). I tried to cripple the code so badly that you can tell what is wrong with it right away:
There are way too many levels of nesting for something that should be dead simple.
But yet, most projects that I have been involved with had code like this in them. This is not because most programmers are stupid, I think this is a result of logic thinking. Even so I luckily never had to draw much flow charts in my life, I think that my brain somewhat works like one. I think most of us who think logically develop their thoughts in form of a tree, jumping from node to node, leaving dead ends all over because we are not able to focus on too many things at once. This is also why mind mapping is a useful technique that I started to embrace lately. It helps us to increase the amount of connections per node in our thinking and lets us discover new nodes (ideas) previously unreachable with our normal thinking.
Another technique that really helps understanding your thinking is to visualize it. Close your eyes and try to see the patterns in whatever it is you are doing (not just programming) for a second. Applied to our particular example above, we could visualize the code above like this:

Now suddenly it becomes very easy to simplify what we are doing. All we have to do is to align those nodes that are not dead ends. Visually speaking that means to make a re-arrangement like this:

In terms of code this means to get rid of anything that looks like an else statement and use a return statement wherever we hit a dead-end (return home early). Our particular example for example could be re-written as follows:
-
function edit($id = null) {
-
$this->Post->set('id', $id);
-
$post = $this->Post->read();
-
if ($this->RequestHandler->isGet()) {
-
return $this->data = $post;
-
}
-
-
$result = $this->Post->save($this->data['Post']);
-
if ($this->Post->validationErrors) {
-
return $this->Message->add('Please fill out all fields', 'error');
-
}
-
$this->Message->add('Post was saved successfully.', 'ok');
-
}
What you can see right away is that we have saved a couple lines of code, but that's not even the exciting part. The exciting part is that by recognizing a pattern in our thinking we were able to produce much simpler results! An interesting implication of this technique is that it is much harder for us to reverse engineer our logic than it is for us to produce it. This is why reading other people's source code can be difficult at times. It is not that their code is difficult, it is just that it's laid out in the way of the thought process that created it. So when you sometimes read a piece of code and really enjoy reading it, it is most likely a sign of conscious thinking and reorganization of typical thought patterns into human readable patterns.
Note: I do not use the word refactoring in this post because the word has too much association with design patterns for my taste. Design patterns itself do not make things simpler! The process of reorganizing your natural thinking does.
Regarding the second code example: I cheated a little bit in terms of saving if statements by using my Assert class to simplify things even further.
All right, I'm really interested in hearing if some of you would enjoy more posts like this. I also still have tons of behaviors and components in my backlog for publishing, but this is a fascinating topic to me right now that I would like to write more about.
-- Felix Geisendörfer aka the_undefined