Do one thing at a time

Software component should do one thing

Or: you are (almost certainly) not blocked.

You know the feeling, right? You need to write a component to do a thing with some XML. But first you need to parse a string to get the XML. But before even that you need to connect to a remote server. But first you need manage the credentials for the server. What’s more, you’ll need an extension so that supports making a MUTP (Made Up Transfer Protocol) connection, since that’s what the brief calls for.

So, like one of your own recursive calls, you begin at the bottom of the stack. And that’s where the problems start. something is up with your development environment. Some twisty piece of dependency nonsense prevents you installing the MUTP extension – it returns a meaningless message before failing. So there you are at Stack Overflow looking at variations on the apt or yum error message you pasted into Google and wondering if perhaps a career in the law would have been a better option after all.

OK. Stop. Breathe. You are not blocked. Really. Part of that rising feeling of panic is the fact that you are starting so far away from the real thing you want to accomplish – the doing of a something with some XML. But you can’t get on with the core task until the other stuff is all wrapped up, can you? In fact, if that’s true for your code, the problem is really with your design. Take a look at this fake code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Smushed {
 
    function process() {
 
        // this is brittle
        $creds = file_get_contents("../creds.txt");
        list($host, $user, $pass) = explode($creds);
 
        // and I can't install this extension
        $conn = mutp_connect($password);
        $str = "";
 
        while ($line = mutp_readline($conn)) {
            $str .= $line;
        }
 
        // so I can't do this
        $els = new SimpleXMLElement($str);
 
        // or finally work with the XML
        foreach ($els as $el) {
            // do something
        }
    }
}

The problem here is that I’ve smushed too much into one class. The routine I want to manage needs either a string or possibly an XML node. It should not care about configurations or connections or anything else. I can solve my problem by fixing my design.

1
2
3
4
5
6
7
8
class JustDoOneThing {
 
    function process(SimpleXmlElment $els) {
        foreach ($els as $el) {
            // do something
        }
    }
}

Now I’ve thrown out everything but what I care about. And now I can isolate it from my system and put it through its paces with a unit test.

This principle is often called the one responsibility rule and you can apply it almost as cleanly to your things-to-do list as you can to your software design. By clarifying and narrowing the responsibilities of the components in your system, you can focus on one thing at a time. Multitasking is as bad for your code as it is for your time management.

“The machine” by zeitfaenger.at is licensed under
CC BY 2.0

Leave a Reply

Your email address will not be published. Required fields are marked *