Monday, April 16, 2007

Don't Soft Code

You have probably been taught to not hard code parameters in your code. For example rather than saying:

if (connections > 50) { // do something

You should probably write:

if (connections > MAX_CONNECTIONS) { // do something

and if you are feeling particularly ambitious, MAX_CONNECTIONS should be set by a config file, so that a sys admin or operator can change this value at deployment time.

This has been a driving force behind my designs for some time now. I try to not hard code any values in my programs. Sometimes expediency leads me to do it anyway, but at least I feel guilty for doing it. Unfortunately, I have found that avoiding hard coding values is not the panacea that is promised. You now have code that is effectively split up (part in the code, part in the config file) which can make it harder to read/understand the code. I have also seen that if the config files get large/complicated enough, no one but the programmer is going to be willing to touch this file, and it really becomes just part of the program.

I am writing this now, because I recently read an article on worsethanfailure.com about Soft Coding. The article makes a very good point - sometimes "Hard Coding" values is better than the alternative. Rather than reiterate why, I'll just tell you to go read the article. The main moral of the story though, is to remember that every design decision involves trade-offs, and it is important to consider the pros and cons of every choice. In the specific case of choosing when to hard code a parameter - you should hard code it if it will be at least as easy to maintain/change the code as it would be to change/maintain/understand the "soft coded" parameters. It is up to you, the developer, to make this determination.

Monday, April 9, 2007

Gone Programmin' (back next week)

The current TopCoder marathon match runs through this Wednesday and only the top 50 (out of 200 from last week) advance. I am currently in approximately 70th place so I haven't put any time into this blog this week. The good news is that the Topcoder algorithm tournament first round was also this weekend, and I advanced to compete again. Next week, I will hopefully have something more interesting to say on my blog.