When is hardcoding good?

Hardcoding is always seen as a bad practice. It makes the code inflexible. For example, making the code aware of the environment itself is a good thing so that we can reuse the code and avoid code duplication. Here, we are trying to copy a file to a user directory.

cp doc.txt to $HOME/

is better than

cp doc.txt to alex/

If this snippet is used in a deployment script for example, it can save a lot of time.

However if this snippet is used in a live environment, using variables might not be good because it can cause unpredictable behaviour and makes troubleshooting harder. Hence, hardcoding in production environment becomes a good thing.

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.