Monday 28 March 2011

City Life

An example of London life that happened to me in the week.

I'm heading back to Liverpool Street to catch the train home -just another worker ant focused on the clock, when :

'Can I ask you a question'

Oh god I think, I take a look at the questioner, no clipboard that's a good sign, scabs on the face and swaying a bit -not so good. Still  I've got 5 minutes to spare, it might be quicker to play along with him.

'Go on then -if you're quick'

'Do you remember Dudley wossname -was in that film with that bird'



Ok, that came from nowhere, however I'm of the vintage that Bo Derek made a big impression on (well the male half anyway -and if the number of cornrow hairdos that were about on blondes at the time was any guide- the females weren't entirely immune).

'Uh, yes Dudley Moore'

'Who was it who was his partner, you know in the act, do you know?'

'Peter Cook' I reply.

'Brilliant mate -I had this bet with my mate that he was wrong and you've won me 20 quid'

'Who did he think it was then?'

'Peter O'Toole, wanker. I told him he was Dracula.'

Made me smile. And it's an excuse to put up a picture of Bo.



Wednesday 9 March 2011

Turing got there first -again.

Alan Turing

I was doing some research into test driven development and came across Tony Hoare's paper  The Emperors Old Clothes . Hoare is a big name in the history of computer science, inventing Quicksort at an early age and then C.S.P -I have had the misfortune to program in Occam (and Ada) which implement this, nothing wrong with the concept or really the language -but Occam simulated on a vintage PC wasn't good.

Hoare's paper is well worth a read, both from a historical perspective and for the all too familiar view of projects going horribly wrong -in this case Algol 68, PL/1 and Ada. Helped by mandated use from the military Ada did finally take off -but so did the Bristol Brabazon - but the language failed to achieve the popularity hoped for and we all ended up using C++ for our sins (which must have been many).

Anyway, back to Turing, it seems he wrote a paper in 1949 (yup) entitled 'Checking a Large Routine' the opening paragraph reads :- 
"How can one check a large routine in the sense of making sure that it’s right? In order that the man who checks may not have too difficult a task, the programmer should make a number of definite assertions which can be checked individually, and from which the correctness of the whole program easily follows"
 Sounds like the basics of Test Driven Development to me.


Sunday 6 March 2011

Why I hate setters and getters (especially in PHP).

Whatever you think about object oriented programming (and the phrase ‘Emperors New Clothes’ has been known to pass my lips, although I’m not so dogmatic now.) there’s a point where the syntactic sugar turns to syntactic saccharine -and that point is accessor methods.

Whilst I acknowledge the usefulness of encapsulation and limited visibility I loathe having to write reams of setThis() and getThat() for every blasted variable I want to use. I was going through the Zend Framework tutorial and there’s a simple four field model in there, which generates eight methods, life is too short. The model already exploits __set and __get which allow controlled access to hidden properties, so why not go the whole hog? Here’s a version that calls an accessor method if there is one (you might need to do some extra processing or formatting in it) but otherwise returns the protected variable.


class SomeClass {
    protected $_comment;
    protected $_created;
    protected $_email;
    protected $_id;

    public function __get($name)
    {
       $method = 'get' . $name;
       if (('mapper' == $name)) {
           throw new Exception('Invalid guestbook property');
       }
       if(method_exists($this, $method)) {
           return $this->$method();
       }

       $attr = "_$name";
       if(property_exists($this,$attr)) {
           return $this->$attr;
       }
       throw new Exception('Invalid guestbook property');
    }  
}

Instead of defining a setId method and calling a $someObj->setId(3), you don’t define the method and use $someObj->id = 3.

Personally I’d probably turn this into an abstract base class to support the model classes, leaving the individual classes to supply specific accessor methods if needed -thus getting rid of reams of code that I’d need to maintain. There are times when you wouldn’t use this approach, but for the database mapping example given it’s a bit of a no brainer.

Why especially in PHP? Because it’s not an O.O. language, it’s a scripting language, I like the ability to use O.O. syntax when I think it adds clarity to the program and I like the fact that I don’t have to if I think it makes life harder. Additionally libraries like Zend Framework should make life quicker and easier, it is the point of them after all, and not slower, harder and more verbose.

BTW don’t let this put you off Zend Framework, it’s a good piece of work and I’ve built a few systems using it.

linkedin