Simple Things Should be Simple, Complex Things Should be Possible

October 4, 2009 § Leave a comment


Assuming you are a software developer, let me ask you a question, what do you do at work (except drinking coffee)? do you modify code written by others? or do you develop projects from scratch? either way this post is for you. For me Its almost 2 years that I adjust and modify pieces of code written by others (if I just could know where they live), anyway here is the story:

Your boss comes to your office holding a cup of coffee in his hand, and tells you about a new modification, we have to read some information from a .csv file and parse it (YEAAH finally something new to do), this is something that happens everyday, there is a problem and we have to solve it, but lets dig to see how this problem is solved by different programmers?

John, a programmer who is not a programmer (but sadly thinks he is), will consider reading a .csv file the end of the world (I don’t even know what the hell is a csv by the way), he will start spamming all the office asking how he could do it, he will get 10 different ways to solve it, and after wasting 20 days, and interrupting other coworkers during this period, its done… YES YOU CAN JOHN!!. We put John’s modification in a production environment, and we get a big fat NullPointerException, Why? on my PC worked… John its because you considered that the file should be in the directory:

C:\Documents and Settings\Users\Dump John\Desktop\file.csv

And guess what, that path is written in the source code file, and to change it we need to checkout the project from the repository, change it, recompile it and deploy it again. JOHN take this advise, go find a job in McDonald, and leave programming for real programmers.

Fred, the second programmer is one who knows everything, he has a 10+ programming experience, he read 15 books about design patterns, he collects certifications as collecting flowers, he used almost all java MVC and web development frameworks, he knows about good programming habits, ok ok you are good Fred, what is your solution?

  • Create an IDataReader interface that will have a readData() and processData() methods (you have to program  for interfaces).
  • Create a class AbstractDataReader that implements IDataReader and adds many common methods, that will be used by our subclasses.
  • Heey now the file is .csv but in the future may change so lets create those classes CSVDataReaderTSVDataReader,XMLDataReaderDBDataReaderSocketDataReaderFTPDataReaderHTMLDataReader, that will handle almost every kind of file, and lets group text ones alone, and DB alone, so lets make them descendants of AbstractTextDataReader andDBAbstractDataReader.
  • We have DataReader’s for almost any type, but we have to be sure which one to use, so lets create a DataReaderFactory, that will determine the correct file reader and return a proxy to the correct IDataReader object (object factory & proxy pattern).
  • Yeeeeah I like the idea, go on Fred…
  • The way files are processed may change and its not good to put it in a source code file, lets create a proprietary workflow language that can be expressed in plain XML, and use a parser that will parse these commands, and transform them to java code.
  • Lets adapt the delegation design pattern too, I don’t know for what, but it sounds cool I want to use it.
  • Many DataReaders can be operating at the same time, so lets create a pool of IDataReader objects, and lets use Dependency Injection to inject those objects in the classes that will use them.
  • Should we use our own Dependency Injection framework? NO (we are reinventing the wheel) Spring framework provides it, lets integrate Spring in our solution.
  • Since threads are playing we need to put synchronization stuff, and having racing conditions is the last thing we want, this is a serious framework, its Fred’s framework.
  • Everything should be configurable, so lets put everything in .properties files, no wait lets store them in a database, so we can change them without the need to restart the application.
  • Lets log all operations, and lets also provide a GUI application to manage the threads pool, logs, and configuration parameters.
  • We should do everything with Aspect Oriented Programming (AOP) in mind, so we can add and remove layers of code without interference.
  • Ok shut up Fred we got your point.

This is a perfect and nice solution Fred you are really a great programmer, I love your idea. I am not done please keep reading…

Finally Steve a duct tape programmer (as Joel Spolsky describes him in his post)  comes, Steve is just as the previous programmer Fred, but with one additional thing, HE USES THAT THING ON HIS SHOULDERS (yes the head), before even starting to work he asks one or more questions to his boss, to better understand the situation, and after talking with his boss. It is cut clear that those coming information will be only from a .csv file, and they will almost never change. So he decides to go simply with a simple class that reads a file and parse it and thats it. (of course he would make it a bit more flexible to small changes) and everything is done in, lets say… 4 hours? It could be even done in 2 hours.

Got it? No? ok lets continue… after two years both Fred & Steve changed companies (John was shoot by Steve, I told him to find a job in McDonald…), and you can’t reach them anymore, and guess what… a modification is needed. A new piece of information should be passed to the system, so the .csv file has some extra information now, and one additional step should be done when processing the file. And we need someone to do this modification… guess who?? Steve? No. Fred? No.

Got it? No? ok lets continue… after two years both Fred & Steve changed companies (John was shoot by Steve, I told him to find a job in McDonald…), and you can’t reach them anymore, and guess what… a modification is needed. A new piece of information should be passed to the system, so the .csv file has some extra information now, and one additional step should be done when processing the file. And we need someone to do this modification… guess who?? Steve? No. Fred? No.

I am the one who will do the modification.

In the first case I should waste my time navigating Fred’s framework, moving from one class to another, going up and down in the inheritance tree (Geeez Fred how many classes did you put there). Guys I just need to read a fucking .csv file and process it.

Finally I get to the point where I can add the new parameter, now what? Oooh yes I have to add the new processing step, yes its easy I just have to validate the new parameter, but please give me one day to learn how the hell can I do it using Fred’s XML workflow language, that its not even working well.

Lets go back, what is the modification? add a parameter and process it. Why for something so easy I should spend so much time? because someone (Fred) who read design patterns thought its cool to do it in that way. Wrong Fred wrong, don’t complicate your life (and mine please) if its not necessary, your boss didn’t ask you to design a general DataReader module that will read everything, even what is written on your dirty pants, he just asked you to extract and process 4 parameters from a .csv file for God sake!! (just give me Fred’s new address).

The bottom line is simple, if you have to read a file just read it as Steve did, but if you need to develop a general data reader module then go Fred’s way, be intelligent, use your head, small things should remain small and simple, its not logical to create a 50 class framework to read a file that can be read in 10 lines of code.

For me, the best solution was Steve solution, easy, simple, and fast. Have a nice day and remember what Alan Kay said:

Simple things should be simple, complex things should be possible
Advertisements

Tagged: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Simple Things Should be Simple, Complex Things Should be Possible at Omar Al Kababji.

meta

%d bloggers like this: