PHP in Action

Fake it and raise your IQ

dagfinn | 08 February, 2007 21:45

Implementing a new piece of software is working with a black box that doesn't even exist yet. You know something about what's supposed to come out of the black box, but typically not much.

So  where and how on earth do you  start?  Let's say we know one thing about the result; call it the known fact. Do we start by  making educated guesses about what should be in the black box? A lot of development is like that.

The smarter way to do it is to start with the smallest possible step from the known fact. What is the smallest inference, the one that wil tax our intelligence the least? Of course, you may want to challenge your intelligence, but I'm talking about a way to make the most difficult problems tractable by chunking them so small you can do them while drunk.

Enough of this philosophical mumbo-jumbo; what does it mean in practice? Well, it's test-driven development, and more specifically, it's what Kent Beck calls the Fake It patttern. You implement a test for the result, then you hard code the result. It's an absolutely weird idea that's magical in its effect because it get you started. And as the German expression goes, aller Anfang ist schwer--all beginnings are difficult.

Let's say we want to create a class to represent URLs. What do we know about it? It's going to be able to do all sorts of parsings, takings-apart and gluings together, but above all, it must be able to spit out a URL string. So we start by implementing a test for this behavior:

class UrlTest extends UnitTestCase {

    function 
setUp() {
        
$this->url = new Url('http://www.example.com');
    }

    function 
testUrlObjectReturnsUrlString() {
        
$this->assertEqual('http://www.example.com',$this->url->toString());
    }
}

Now we do the simplest thing that could possibly work by creating the class like this:

class Url {
    function 
toString() {
        return 
'http://www.example.com';
    }
}

It's shocking, of course. This class doesn't listen, it just talks. And it's completely phony and useless except as a first step that will lead to something else. Slightly better is to actually keep the URL string in an instance variable:

class Url {
    private 
$urlstring;

    function 
__construct($urlstring) {
        
$this->urlstring $urlstring;
    }

    function 
toString() {
        return 
$this->urlstring;
    }
}

It's still pointless, but now we're ready to choose another small step that will actually do something real: get the host name or something else interesting. And we can do that by writing a test for that single behavior and implementing it with, say, a single regular expression. No grand plans are necessary, at least not until we've learned something about the nature of the task by tinkering with it. Actually, it's probably wise to use the built-in PHP function parse_url(), not least because it's likely to handle weird variations appropriately. But the point is we could do without it if we wanted to without getting confused by the complexity of the problem.

 

 

Comments

Re: Fake it and raise your IQ

Mark from web site hosting reviews | 29/10/2007, 21:31

$this-> isnt working for urls? at least not for me, or problem is with something else

Re: Fake it and raise your IQ

dagfinn | 31/10/2007, 02:26

I'm afraid I don't understand the question. All I can say is, the example is not supposed to "work" in the sense of doing anything real or useful. It's a preliminary exercise.
 
Accessible and Valid XHTML 1.0 Strict and CSS
Powered by LifeType - Design by BalearWeb