language-agnostic

Best practices for avoiding hardcoded values IRL


In theory, source code should not contain hardcoded values beyond 0, 1 and the empty string. In practice, I find it very hard to avoid all hardcoded values while being on very tight delivery times, so I end up using a few of them and feeling a little guilty.

How do you reconcile avoiding hardcoded values with tight delivery times?


Solution

  • The assumption behind the question seems to me invalid.

    For most software, configuration files are massively more difficult to change that source code. For widely installed, software, this could easily be a factor of a million times more difficult: there could easily be that many files hanging round on user installations which you have little knowledge and no control over.

    Having numeric literals in the software is no different from having functional or algorithmic literals: it's just source code. It is the responsibility of any software that intends to be useful to get those values right.

    Failing that make them at least maintainable: well named and organised.

    Making them configurable is the kind of last-ditch compromise you might be forced into if you are on a tight schedule.