What is Hard Coding?

Hard coding is a software development practice in which values are fixed into the code and can’t be easily changed without modifying the code itself.

This practice is considered bad programming style and can make the code less flexible, reusable and harder to maintain.

In this article, we’ll explore the concept of hard coding, its disadvantages, and how to avoid it.


What is Hard Coding?

Hard coding is the act of manually coding values directly into the source code of a software program.

These values are typically used as inputs, outputs, or as control parameters.

For example, in a simple program, a hard-coded value could be the width and height of a window.

The problem with this approach is that if you want to change the width or height of the window, you’ll have to modify the code, recompile it and redistribute it to all users.

Why is Hard Coding Considered Bad Practice?

Hard coding has several disadvantages, including:

  • Lack of Reusability: Hard-coded values are not reusable, meaning that if you need to use the same value in another part of your code, you’ll have to copy and paste it, making your code more difficult to maintain.
  • Lack of Flexibility: Hard-coded values make the code less flexible, as changing a value requires modification of the code.
  • Maintenance: Changing hard-coded values can be a time-consuming and error-prone process, especially if the values are scattered throughout the code.
  • Testability: Hard-coded values make it difficult to test your code, as you can’t easily change the inputs to test different scenarios.

How to Avoid Hard Coding in Your Code

To avoid hard coding in your code, you should use configuration files, environment variables or user inputs to supply values to your code.

  • Configuration Files: Configuration files allow you to store values that can be changed without modifying the code. For example, a configuration file could store the width and height of a window. Your code can then read the values from the configuration file and use them to set the window size.
  • Environment Variables: Environment variables are values that are stored outside of the code and can be accessed by the code at runtime. This makes it easy to change values without modifying the code.
  • User Inputs: User inputs allow your code to retrieve values from the user, making it possible to change the values without modifying the code.

Code Example

Let’s take a look at a code example in Python to demonstrate how to avoid hard coding in your code.

Consider the following code, which sets the width and height of a window:

width = 500
height = 400

# Create the window using the hard-coded values
window = Tkinter.Tk()
window.geometry("{}x{}".format(width, height))
window.mainloop()

In this example, the width and height of the window are hard-coded into the code.

To avoid hard coding, we can use a configuration file to store the values.

import configparser

# Read the values from the configuration file
config = configparser.ConfigParser()
config.read("config.ini")
width = int(config["Window"]["width"])
height = int(config["Window"]["height"])

# Create the window using the values from the configuration file
window = Tkinter.Tk()
window.geometry
"{}x{}".format(width, height)
window.mainloop()

In this revised code, we’ve used the configparser library to read values from a configuration file (config.ini).

The configuration file contains the values for the width and height of the window, which are then used to set the size of the window.

This makes it easy to change the window size without modifying the code.


Conclusion

Hard coding is a practice that should be avoided in software development.

It can make your code less flexible, reusable, and harder to maintain. Instead, you should use configuration files, environment variables, or user inputs to supply values to your code.

By doing so, you’ll make your code more maintainable, reusable, and testable.