Monday 18 May 2009

Extension methods make your code more readable

There has been a long standing argument for and against using extension methods. Recently I am on the quest to write code which is as readable as possible. That really is my prime aim - I want someone who is simply scanning my code to be able to, at a high level, understand exactly what is going on in that function. All other aims come second to that (e.g. until a requirement comes along which says otherwise).

Well, six months ago, I would never have made this statement - "Extension methods are a massive help here!". Honestly, they just are. I know people hate them because you can't really tell where the method comes from, and it looks wierd, and it breaks OO because you should inherit to add functionality (well actually, I disagree with that statement being made blindly)...

So I have a function which takes in some dates and returns a status depending on how the dates compare. Here is a simplified version of the function:

public Status GetStatus(DateTime now, DateTime modifiedOn, DateTime createdOn)
{
  if (modifiedOn.Date == now.Date)
    return Status.Amended;
  else if (createdOn.Date == now.Date)
    return Status.New;
  else
    return Status.Unchanged;
}

So what's wrong with that? Well you have no idea what the if statement is actually doing! Tell me, if you scan that quickly, can you tell what the business rule is here? I am pretty sure most people will have to scan that a few times to get what is going on there. I considered adding a comment, but comments are a definite code smell (see DRY) so this is how I changed it (using an extension method)

public Status GetStatus(DateTime now, DateTime modifiedOn, DateTime createdOn)
{
  if (modifiedOn.SameDayAs(now))
    return Status.Amended;
  else if (createdOn.SameDayAs(now))
    return Status.New;
  else
    return Status.Unchanged;
}

Simple but effective methinks. I think it's definitely clearer (though could perhaps be made even better). Do you agree?

Friday 15 May 2009

A shout out to Benjamin Mitchell

Benjamin has recently started blogging (and about time too!). I have worked as a developer on one of his teams before, and he is a good guy to work for and really knows his stuff. He's one of those PMs who is always looking to improve his team's knowledge, skills and processes.

I hope to see postings about Agile techniques and Lean Development, and hopefully some good anecdotes too!

Thursday 14 May 2009

I am on Twitter

Well, after much nagging by my good friend Mr Norton, followed by a number of people at the AltNet Beers the other night ("what, you aren't on Twitter mate???"), I have finally succumbed and signed up. You can find me at http://twitter.com/nmosafi. I hope to be posting about all my fun and frustrations at work, and hopefully will avoid doing so whilst completely hammered!

Wednesday 13 May 2009

Last night's AltNet Beers

Firstly, thanks to Seb for organising the ALT.NET beers conference last night. I had a great time and met some very interesting people who I hope to see at future conferences.

The first topic was on two related questions, which were something along the lines of "why would I use a non-relational database" and "how would I architect a database differently if I was writing more than I was reading".

Personally, I wasn't really getting the question here. The whole thing about "writing more than I read" made no sense to me - why bother writing it if you're never going to read it. The speaker was unable to really identify where his pain points are with his system. I felt that there was an interesting discussion to be had, but the conversation didn't seem to inspire many people and it wasn't carried too far. I would welcome this discussion continuing elsewhere and if it does I would be keen to take part in it.

We talked a bit about document databases here, Ayende naturally getting involved as he has designed (not built, mind) a document database before. The idea is that if you want to store fairly unstructured data, you would use a document database. Again, this isn't something I know much about and I wasn't sure what the benefits of a document database are over, say, storing the document on a file system and then indexing the information you want to search separately (e.g in a relational database for querying, or search appliance for searching).

Note: I was going to discuss this further outside with Ayende but the topic changed to BDD which is something I have recently starting doing so I wanted to hang around for that one! If you're reading then perhaps we can discuss it next time?

So as I said, we moved onto BDD which Ian Cooper summed up excellently with a great analogy, which I won't repeat here. What it bogs down to is that whilst test driven development is about ensuring that we do things right, behaviour driven development is about ensuring that we do the right thing. Given that we had a bunch of Lean/Kanban enthusiasts there, I thought it quite appropriate topic. With BDD you minimise rework by getting things done right the first time, this will naturally help to you can increase throughput and decrease wastage, which is what Lean's all about.

So having recently started a new job (which explains my lack of recent blog posts) I am trying to introduce BDD to the team, using SpecUnit.Net. They don't do TDD at all, and I think BDD is not only easier to adopt but also adds much more value. I am actually new to BDD, having consistenly read about it I'd not had the chance to actually try it on a proper project. I am impressed with the effect and the way it makes you think about your code, so much so that I couldn't see myself not developing in this way ever again!

Anyway I actually had some interesting conversations after the event about BDD. I will try to gather my thoughts about this soon and perhaps capture them on a future blog post.