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.

Like it.? Share it:

Comments are closed.