Thursday, February 24, 2011

YAGNI

You Ain't Gonna Need It


Or perhaps, Einstein said it better "Make everything as simple as possible, but not simpler.".


There are many tools at our disposal when we write code. And once we get to know about a new tool, we tend to use it. We often use it a lot. Too much in fact.



Take the simple factory class.


A wonderful thing, and I would not have a word said against it. 


Well maybe just a word or two..


So you start writing your application which, along the way, will store some data. First pass it's a CSV file. And knowing that things change, and not wanting to tie your application to a particular storage, you start out with an IDataArchiver class. You might implement an Oracle, MySql and Ingress class later. 


So far so good.


Then you think, you know, if we dynamically peek into the dlls in the directory to figure out which ones implement this class, and then we can build a factory, and then we can add a new dll for a new database and we don't even have to recompile the code. Sweet. Only no-one has actually asked for this.


YAGNI - You Ain't Gonna Need It


Write the factory class, have it always return your CSVDataArchiver which implements IDataArchiver. You are done.

  • CSVDataArchiver can be "internal", and only seen by the factory.
  • You are working to an interface. 
  • You can use Mock Objects to test things.
  • You have created a single hook where you can drop in other implantations a(if they are ever written)
  • It has taken less time than reading this post.
  • You have removed dozens of bugs in complex code by not writing it.
  • They guys who come after you to change the code will still respect you in the morning.
Save the "cool stuff" for you hobbies. There are no marks in the commercial world for "cool". 

1 comment:

  1. Very true. "I always wanted to try this out" is a hazardous reason to justify a design decision.

    I once came across a great name for this:
    RDD: Resume-Driven-Development

    http://www.healthcareguy.com/2007/01/19/resume-driven-development-rdd/

    ReplyDelete