Programming Practices – Are Default Values Good or Evil?

programming practices

The question about default values in general – default return function values, default parameter values, default logic for when something is missing, default logic for handling exceptions, default logic for handling the edge conditions etc.

For a long time I considered default values to be a "pure evil" thing, something that "cloaks the catastrophe" and results in a very hard do find bugs. But recently I started to think about default values as some sort of a technical debt … which is not a straight bad thing but something that could provide some "short term financing" get us to survive the project (how many of us could afford to buy a house without taking out the mortgage?).

When I say a "short term" – I don't mean – "do something quickly first and do refactor it out later before it hits the production". No – I am talking about relying on a hardcoded default values in a production software. Granted – it could cause some issues, but what if it only going to cause a single trouble in a whole year.

Again – I am talking about the "average" mainstream software here (not a software for a nuclear power station) – the average web site or a UI application for the accounting software, meaning that people lives are not at stake, nor millions of dollars.

Again, from my experience, business users would rather live with the software which "works somehow", rather then wait for a perfect one. And the use of default values helps a lot if you develop a software in a RAD style. But again – the longest debug sessions I have spent were because of the bugs introduced by a default value which either stopped being "a default" along the way or because a small subsystem has recently been upgraded and as a result of this upgrade it does not handle the default correctly (e.g. empty list vs null, or null string vs empty string).

So my question is – are the default values good or evil. And if they are a technical debt – how do measure up how much you can borrow so you can afford the repayments?

Best Answer

The concept of Convention over Configuration is impossible without sensible default values. The key word here is "sensible". The default values have to make sense for at least 80% (if not more) of all the uses of a library/service/framework.

General rules of thumb:

  • If a value is sensible for 80% or more uses, it needs to be a default value
  • If there are no values that are used for the majority of cases, do not use defaults.
  • Default values prevent stupid mistakes from the setup code. If the defaults are reasonable for most cases, fewer people mess with the working configuration.
  • Non-standard configurations are more visible when you use defaults.
  • Bad defaults are worse than no defaults.

Essentially, once you learn how the default configuration works, you can make educated decisions about how/when to do non-standard configurations.