Mark Needham

Thoughts on Software Development

Coding: Rules of thumb

with 6 comments

I recently came across a post by Ayende where he talks about the need for tests to justify themselves and describes his approach to testing which doesn't involved TDDing all the code he writes.

While this approach clearly works well for Ayende I really like the following comment by Alex Simkin:

Anyway, this post should be marked MA (Mature Audience Only), so younger programmers wont use excuse to not write unit tests because Ayende doesn't do it.

This reminds me of a conversation I was having with a few colleagues a while ago where we were discussing whether we should look to test drive absolutely everything that we write or whether we should make that decision on a case by case basis.

Personally, I don't yet have the experience to be able to tell when it's appropriate not to drive code (excluding spiking) and pretty much every time that I've decided something was 'too easy' to test drive I've managed to make a mistake and not realised it until much later than would have been the case with a test driven approach.

I'm finding that this applies to more than just testing though and it seems that where an approach is known to reduce a lot of potential pain then it might be useful to favour that approach unless we find an intriguing reason not to.

Thinking along those lines I think it makes sense to follow some 'rules of thumb' and not break them unless we absolutely have to.

Some rules of thumb which I've come across are:

  • Don't put anything into the session
  • Don't put getters onto objects – make all code adhere to the tell don't ask principle
  • Write production code test first
  • Write classes which adhere to the single responsibility principle
  • Favour composition over inheritance
  • Favour constructor injection over setter injection

Given a situation where I want to reuse some code and I know that I could choose between doing it with composition or inheritance I wouldn't consider this a 50-50 choice but instead would look to use composition unless it became really difficult to do this and inheritance was clearly the answer in which case I would use that.

Obviously these 'rules of thumb' might be a bit restrictive for people at the higher levels of skill of the Dreyfus model and I imagine that even with people with less experience they could be quite dangerous if applied too literally.

I wonder whether being dogmatic about a few rules of thumb would be more or less dangerous than making decisions not to follow a useful practice because you mistakenly believe you have the ability to tell whether or not it applies in a given situation?

Corey Haines wrote a really good post where he talks about the misuse of the word 'pragmatic' which seems to address this area although he comes to a slightly different conclusion:

This is a common thing that I hear from software developers: they have a little bit of experience, think that they understand something to the point where they can make their own decisions on what it means to 'be pragmatic.' Often times, people use the term 'pragmatic' as a way to hide a lack of skill and experience. Or, sometimes, it is used in ignorance: someone doesn't realize that they don't understand something well enough. Usually, though, it is brought to play when someone is justifying cutting corners on something…this can come back later to bite you in the ass.

Think your 9 months of trying TDD makes you an expert, someone who can suddenly decide when it is 'pragmatic' to not design your system with TDD. Or, don't even worry about designing your system with TDD, just talk about automated testing.

Are you being 'pragmatic' about automated testing, skipping it things you don't know how to do or are hard?

Typically when people try to justify a shortcut this is exactly the way they will justify it. The irony is that quite often much suffering is gained from that decision so it wasn't really that pragmatic after all.

I realise that there aren't really any rules for software development that we can follow all the time and expect to gain success with but it does seem that some approaches have already been proven to be more effective than others so it might be useful to make use of them.

Written by Mark Needham

October 4th, 2009 at 4:59 pm

Posted in Coding

Tagged with

6 Responses to 'Coding: Rules of thumb'

Subscribe to comments with RSS or TrackBack to 'Coding: Rules of thumb'.

  1. Reminds me of a blog post I wrote back in 2003: http://www.harukizaemon.com/2003/12/crossing-road.html

    Simon Harris

    4 Oct 09 at 5:25 pm

  2. Nice post Mark,
    I particularly like your rule of thumb #1. People seem to forget it all the time – or worse, don't realize the problems that hanging stuff in the session might cause.

    I think these rules are nothing but standards professionals should follow. Like Uncle Bob likes to say, a doctor washes his hands before attending any patient because he is a professional.

    Likewise, a professional developer writes tests – among other things. Period.

    Nice thoughts here, as usual…

    Leonardo Borges

    4 Oct 09 at 8:55 pm

  3. [...] This post was mentioned on Twitter by planettw. planettw said: Mark Needham: Coding: Rules of thumb: I recently came across a post by Ayende where he talks about the need for.. http://bit.ly/4qKLXW [...]

  4. @Leonardo – yeh I know the session is a killer! The ultimate global bag of data as well as leading to the VM running out of memory if you forget to clear it out properly. Nightmare!

    Mark Needham

    4 Oct 09 at 9:45 pm

  5. Great discussion.

    I don't agree with Uncle Bob's suggestion that professionals need to write tests.

    Probably following this kind of rule, coming from guys like him, is the easier way to become a good programmer.

    But I'm afraid you can loose something when you do that, maybe never realizing the real benefits and costs of applying or not a given technique.

    I'm not that one pragmatic who just don't want to try new techniques, btw. Absolutely.

    I just think being dogmatic isn't the better way.

    Ayende's point is really useful.
    Justify all your choices, and when someone ask you about testing, you'll explain the big picture, without having to say "Uncle Bob said it's imperative".

    Rafael Noronha

    5 Oct 09 at 2:36 am

  6. [...] Coding: Rules of thumb – Mark Needham shares some of his rules of thumb for development that he tries to avoid breaking unless absolutely necessary. [...]

Leave a Reply