Skip to content
BENIBIX Learn

Hard-Coding

Development🧩 Programming ConceptsData and Variables

Also known as Hard-Coded Value, Hardcoding, Hardcoded

Typing a value right into the code. To change it, you must change the code.

Think of it like

It is like writing a price on a toy in pen. To change it, you need a new toy.

Example

Jack typed the tax rate 0.2 into twelve places. When the rate changed, he had to find and fix all twelve.

Why it matters

Values change: prices, links, keys. When each one lives in one named place, a change is one edit.

Definition

Hard-coding is writing a value straight into the code, where it is hard to change. A tax rate, a file path or a password typed into the source are all hard-coded. To change one, someone must edit the code and ship it again. The fix is to keep the value in a named constant, a settings file or the environment.

Example code

python
tax = price * 0.2       # hard-coded
TAX_RATE = 0.2
tax = price * TAX_RATE  # named once
The first line hides the rate in a sum. The second names it once.
The last card is hard-coding. The rate 0.2 sits inside the sum, so it is hard to find and change.

Common mistake

Hard-coding a password or API key. It ends up in Git, where anyone with the code can read it.

Key takeaways

  1. 1
    Baked in. The value lives inside the logic.
  2. 2
    Costly to change. Each change means a code edit.
  3. 3
    Name it once. Use a constant or a settings file.

Learn it properly