The only right way to write code!

I was reading a blog the other day and the author was talking about writing a Java application in 10 minutes as a test.  They finished off by saying that even with time to consider the solution they do not think they could improve upon it.  When I looked at the code my first thought was “what a mess”, you see I’m someone who likes pretty things.

Once I stopped and thought about it I realised that pretty would not make it work better, run faster, have less errors or be any better than it was, it would only appease my sense of order.  The code did the job it needed to do and only took 10 minutes!  The author of this code was now free to solve another problem with the rest of the day.  Wow!

There are many reasons to write code in certain way: –

  • Quick and dirty to get the job done
  • Elegant and clever, the code looking beautiful
  • Well behaved code, it follows good design principals or some such other view point
  • Well structured, all the objects are objects and everything is encapsulated

From time to time we all think our approach is the best, until that is we change our minds.

The truth is probably that we are all right to some degree or other.  At the end of the day the code needs to do what it needs to do.  If it fails this simple test then all the rest is just hot air.

Yes, try and make it well-structured if the conditions allow and are needed.

Yes, follow good design principals where you can and where it adds value.

Yes, elegant and clever code is also usually short and compact so can be easier to work through for maintenance.

And yes, don’t eat into the profit margin, churn out the code that does the job.

Good, I’m glad we cleared that up.  Let’s move on to coding shall we?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.