Programming Tips #1 “Don’t Repeat Yourself”
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Over the years I have built up what I call my ‘programming wisdom’ this is what
has replaced my early years of supposed ‘raw talent’. When I was age 11 I wrote my first program to play Chess, it wasn’t exactly Deep blue yet it could beat my parents but not me (is never good if you can make something more clever than yourself).
I could probably write a much better chess program now and in fact its one of my favorite questions in an interview to ask ‘how do you start writing a program to play chess’ (I digress) the point is that I would now write a better chess program because I could draw upon my abilities into research and planning, I may not come up with any amazing ideas but it would likely be solid, reliable and probably completed on time.
So I thought to myself what have I learnt that I can put across that will be useful to my fellow programmers who have not made all the mistakes I have. I plan on writing a long series of programming tips over the coming weeks (disturbed only by solicitors, investors and other scary programming time eating monsters).
Many of these tips may seem obvious but so many of these if I asked in an interview and the programmer would say something like ‘yes I always use OO’ and yet two weeks later I would find them ignoring OO principals because ‘it was just quicker to do it this way’ and in doing so completely miss the point.
#1 Don’t repeat yourself
So for today’s tip the most obvious of all programming mistakes. This can mean so many things but it encapsulates my whole approach to programming. At every step I am always thinking ‘how could this be extended’, now you are probably asking yourself why is this relevant to not repeating yourself? To me every new piece of code is an opportunity to save myself time later. An example,
When I was looking at the Zend Framework (PHP) at the new filter system that uses chains (classes returning classes) I used it twice before I wrote a helper. This is an example chain.
$filterChain = new Zend_Filter(); $filterChain->addFilter(new Zend_Filter_Alpha()) ->addFilter(new Zend_Filter_StringToLower());
And this is what I replaced it with,
$filterChain = new Zend_Filter_Helper('alpha-StringToLower');
Now although this probably took me 3-4 hours to write this, over the last few months it has probably saved me far more time and will continue to save me time. It is so important to not just think at this level but at every scale of programming to think ‘what if I went one step further’. It can mean that 6 weeks down the line a sudden change in requirements can mean instead of a major slippage actually mean a £500 bonus from your boss.
Programming tips
Comment by David Coallier on 6 November 2007:
You’d be better because the real good chess players from the 70s-80s wrote good books about it ?
Hmm Deep Blue, dear Gary, we’ll see how he does in the russian presidential
We can’t say it’s not a smart man.
Good article, I hope to see more and more so people start understanding that writing good code is crucial.
Comment by Manfred on 6 November 2007:
This looks like the Decorator Pattern, no?
Comment by Stephan Schmidt on 7 November 2007:
Good tip. Saves a lot of time in the future.
Perhaps it would be even better to write:
$filterChain = filter().add(new Zend_Filter_Alpha_StringToLower())
or with some more Fluent Interface style:
$filterChain = create()
.alpha()
.toLower();
(where alpha and toLower could also be combined)
But in every case, I would have written a new Zend_Filter_Alpha_StringToLower which has better reusability thann Zend_Filter_Helper(’alpha-StringToLower’);
Strings (”alpha-StringToLower”) are also not very refactoring friendly.
I’ve written about how to add fluent interfaces to Google Collections. They do something similiar, they filter collections.
http://stephan.reposita.org/archives/2007/10/17/creating-a-fluent-interface-for-google-collections/
More about fluent interfaces was written by Martin Fowler
http://martinfowler.com/bliki/FluentInterface.html
Peace
-stephan
–
Stephan Schmidt :: stephan@reposita.org
Reposita Open Source - Monitor your software development
http://www.reposita.org
Blog at http://stephan.reposita.org - No signal. No noise.
Comment by Bill Karwin on 7 November 2007:
Nick, have you looked at the new Zend_Filter_Input class, which allows you to declare multiple filters or validators for multiple inputs?
In your example, you want a chain of two filters, and no validators.
$input = new Zend_Filter_Input(array('value' => array('Alpha', 'StringToLower')), null);
Then you can use it like this:
$input->setData(array('value' => $someValue));
echo $input->value; // outputs filtered value, also applies html-escaping
See also:
http://framework.zend.com/manual/en/zend.filter.input.html
Comment by Nick Halstead on 7 November 2007:
Bill, yes I have looked at it (I live and breath the platform, how sad am I) but this was just purely an example and is not what we use anymore.
Decorator pattern, isnt that something to do with me avoiding DIY? - but seriously, yes Decorator is part of it, but I was more trying to get across an overall approach of using every programming paradigm (whichever your most comfortable with) to mean that you save yourself time in the long run.
Trackback by PHPDeveloper.org on 9 November 2007:
Nick Halstead’s Blog: Programming Tips Series…
Nick Halsted has posted the first two in a series of ……
Trackback by Anonymous on 4 April 2008:
Ebony Bitches Sucking Dick On Video…
Ebony Bitches Sucking Dick On Video…
Trackback by Anonymous on 4 April 2008:
Brittny Spers Giving A Blow Job…
Brittny Spers Giving A Blow Job…
Trackback by Anonymous on 4 April 2008:
How To Spell Go Fuck Yourself In Portuguese…
How To Spell Go Fuck Yourself In Portuguese…