Hard coding means writing a fixed value directly into a program's source code instead of getting it from somewhere that can change, like a settings file, a database or the user. A password typed straight into the code, a server address baked into a script, a file path that only exists on one person's laptop: all hard coded.
It's one of the first habits new programmers pick up and one of the first ones experienced developers tell them to drop. Here's why.
A Simple Example
Here's a hard-coded database login in Python:
db = connect(
host="192.168.1.50",
user="admin",
password="Summer2024!"
)It works. But the server address, the username and the password are all stuck in the code. To change any of them, someone has to edit the source, test it and redeploy the whole program. And anyone who can see the code can see the password.
Here's the same thing without hard coding:
import os
db = connect(
host=os.environ["DB_HOST"],
user=os.environ["DB_USER"],
password=os.environ["DB_PASSWORD"]
)Now the values come from environment variables, settings that live outside the code. The same program can point at a test database on a developer's laptop and the real database in production without changing a single line.
Why Hard Coding Causes Problems
It's a security risk
This is the big one. Passwords, API keys and access tokens written into code end up wherever the code goes: shared with coworkers, copied into backups, and, far too often, pushed to a public GitHub repository. Attackers run bots that scan public code for exactly this, and a leaked cloud key can be abused within minutes. The OWASP project lists hard-coded passwords as a well-known security weakness for this reason.
Changes get painful
If the same value is hard coded in ten places, changing it means finding all ten. Miss one and you've created a bug. Worse, a password change on the server can suddenly break an app nobody remembers depended on it.
It only works in one place
A path like C:\Users\alex\Desktop\data.csv works on exactly one computer. Hard-coded values are a classic cause of the "works on my machine" problem, where code runs fine for the person who wrote it and breaks for everyone else.
What to Do Instead
- Environment variables for anything that changes between machines, like server addresses and ports.
- Config files for settings people may want to adjust, kept separate from the code.
- A secrets manager for passwords and keys. Tools like Azure Key Vault, AWS Secrets Manager or a password manager's developer features keep secrets out of the code entirely.
- Named constants for values that never change but appear in several places, so there's one spot to update and the code explains itself.
When Hard Coding Is Fine
Not every fixed value is a problem. Things that are true everywhere and never change, like the number of seconds in a minute or the value of pi, are fine to write directly into code, ideally as a named constant. Quick throwaway scripts and prototypes are fine too, as long as they stay throwaway.
The rule of thumb: if a value is secret, if it could ever change, or if it depends on where the code runs, don't hard code it.
TL;DR
Hard coding means putting fixed values like passwords, file paths or server addresses directly into source code. It's quick, but it makes code insecure, brittle and tied to one machine. Use environment variables, config files and a secrets manager instead, and save hard coding for values that truly never change.

