#TechForNewbies: What does ‘hard-coded’ mean?

Maggie Hunt

--

When you code, you use values to hold data. Sometimes these variables will hold values which are input into your programme — like answers to questions or requests — but sometimes you will use values which don’t hold anything calculated or dynamic, so you write their value into your code directly. These are hard-coded values — values which you have to change your code to change the value of.

To illustrate and explain this, here is some Python code which asks your name, and when you give it, greets you:

name = input("Please enter your name:")
print("Hello ", name)

Output:

Please enter your name:

I type ‘Maggie’

Hello, Maggie

In this code, the ‘name’ is a variable that changes based on your input.

Sometimes, especially in larger and more complex pieces of code, you may not need a variable’s value (e.g. the ‘name’ above) to change, ever. You may then want to set the value of ‘name’ to ‘Maggie’ permanently, as in this instance, the sole purpose of the code is to greet me:

name = "Maggie"
print("Hello ", name)

Output:

Hello, Maggie

The ‘name’ value here has been hard-coded — i.e., set in stone within the code. It is essentially fixed within the code, and remains unaffected by calculations or other code.

Why might you want to hard-code a value?
The three C’s which justify Hard-Coding are Constancy, Consistency and Conciseness.

  1. Constancy

You may wish to hard-code a value because it is constantly true — for example, your company’s name. E.g:

companyName = "My Firm"
print("Hello, welcome to ", companyName, ". Here at ", companyName, " we like to code.")

Output:

Hello, welcome to My Firm. Here at My Firm we like to code.

Values which will always hold true for the entire programme can be hard-coded. It is wise to store even these values in variables (or constants — more on those later) so you can reuse each hard-coded value more than once, without writing out the value more every time.

2. Consistency

The value you wish to use could be something which always needs to be replicated exactly. For example, a url or other long string which will be used multiple times in your code:

imageUrl = "www.myfolder.com/static/images/logo.png"

@app.route("/")
def index():
return render_template("index.html", imageUrl=imageUrl)

@app.route("/about")
def about():
return render_template("about.html", imageUrl=imageUrl)

This way, you know you will never misspell your url, or mis-type other long variable values or important numbers.

3. Conciseness

When dealing with lengthy values that repeat multiple times, it is practical to assign them to variables for cleaner code — perhaps the lyrics to ‘Barbie Girl’. You can call these variables when needed:

lyrics = "I'm a Barbie Girl, in a Barbie World! Life in plastic, it's fantastic!"
print(lyrics)

repeat = input("Would you like to repeat that?")

if repeat =="yes":
print(lyrics)

else:
print("ok")

Output:

I'm a Barbie Girl, in a Barbie World! Life in plastic, it's fantastic!

Would you like to repeat that?

I input ‘yes’

I'm a Barbie Girl, in a Barbie World! Life in plastic, it's fantastic!

This way I have saved characters in my code, which made it much easier to read.

Any more tips?

You could be using all kinds of values in your code, which won’t change (‘static values’). They could be written directly in your code, or could be set externally to your code’s operations, and depended on constantly for your code to run — API keys, for example. There is a whole range of possible uses for hard-coded values.

It is crucial that you always organise your code so hard-coded values are easy to find and change if they need to be — they should all be in the same file, or section of your file. This way, when you change the value of the lyrics you want to print or the image you want to show, you and your team know where to look. Using a hard-coded value stored in a single variable means that modifications only need to be made in one place when changes are needed!

Hammer and Chisel Vector Icon Illustration by Vecteezy

--

--

Responses (1)

Write a response