Hard Coding.
Hard coding embeds data such as paths, credentials or rates directly in source code, so changing them requires editing and rebuilding the program.
Hard coding is the software development practice of embedding data directly into the source code of a program or other executable object, as opposed to obtaining the data from external sources or generating it at runtime.
Hard-coded data typically can be modified only by editing the source code and recompiling the executable, although it can be changed in memory or on disk using a debugger or hex editor.
Data that is hard-coded is best suited for unchanging pieces of information, such as physical constants, version numbers, and static text elements.
Soft-coded data, on the other hand, encodes arbitrary information through user input, text files, INI files, HTTP server responses, configuration files, databases, command-line arguments, and is determined at runtime. Preprocessor macros and constants kept in a separate header or module are not soft coding in this sense: they give a value a single name and place, but it is still fixed at compile time, so changing it requires a rebuild.
Overview
Hard coding requires the program's source code to be changed any time the input data or desired format changes, when it might be more convenient to the end user to change the detail by some means outside the program.
Hard coding is often required, but can also be considered an anti-pattern. Programmers may not have a dynamic user interface solution for the end user worked out but must still deliver the feature or release the program. This is usually temporary but does resolve, in a short-term sense, the pressure to deliver the code. Later, soft coding is done to allow a user to pass on parameters that give the end user a way to modify the results or outcome.
The term "hard-coded" was initially used as an analogy to hardwiring circuits, and was meant to convey the inflexibility that results from its usage within software design and implementation. In the context of run-time extensible collaborative development environments such as MUDs, hard coding also refers to developing the core engine of the system responsible for low-level tasks and executing scripts, as opposed to soft coding which is developing the high-level scripts that get interpreted by the system at runtime, with values from external sources, such as text files, INI files, databases, command-line arguments, HTTP server responses, configuration files, and user input. In this case, the term is not pejorative and refers to general development, rather than specifically embedding output data.
Backdoors
Hard coding credentials is a popular way of creating a backdoor. Hard coded credentials are usually not visible in configuration files or the output of account-enumeration commands and cannot be easily changed or bypassed by users. If discovered, a user might be able to disable such a backdoor by modifying and rebuilding the program from its source code (if source is publicly available), decompiling, or reverse-engineering software, directly editing the program's binary code, or instituting an integrity check (such as digital signatures, anti-tamper, and anti-cheat) to prevent the unexpected access, but such actions are often prohibited by an end-user license agreement.
Fixed installation path
If a Windows program is programmed to assume it is always installed to C:\Program Files\Appname and someone tries to install it to a different drive for space or organizational reasons, it may fail to install or to run after installation. This problem might not be identified in the testing process, since the average user installs to the default drive and directory and testing might not include the option of changing the installation directory. However, it is advisable for programmers and developers not to fix the installation path of a program, since the default installation path depends on the operating system, OS version, and sysadmin decisions. For example, many installations of Microsoft Windows use drive C: as their primary hard disk, but this is not guaranteed.
Special folders
Some Windows operating systems have so-called special folders which organize files logically on the hard disk. There are problems that can arise involving hard coding:
Profile path
Some Windows programs hard-code the profile path to developer-defined locations such as C:\Documents and Settings\Username, the default in Windows 2000 and Windows XP, or C:\Users\Username, the default since Windows Vista. Such a path breaks when the program runs on a different Windows version or when the profile is stored on a network or otherwise relocated. The correct way to get it is to call the GetUserProfileDirectory function or to resolve the %userprofile% environment variable. Another assumption that developers often make is that the profile is located on a local hard disk.
My Documents folder path
Some Windows programs hard-code the path to My Documents as ProfilePath\My Documents. These programs would work on machines running the English version, but on localized versions of Windows this folder normally has a different name. For example, in Italian versions the My Documents folder is named Documenti. My Documents may also have been relocated using Folder Redirection in Group Policy in Windows 2000 or above. The proper way to get it is to call the SHGetKnownFolderPath function (SHGetFolderPath on versions before Windows Vista).
Solution
An indirect reference, such as a variable inside the program called "FileName", could be expanded by accessing a "browse for file" dialogue window, and the program code would not have to be changed if the file moved.
Hard coding is especially problematic in preparing the software for translation to other languages.
In many cases, a single hard-coded value, such as an array size, may appear several times within the source code of a program. This would be a magic number. This may commonly cause a program bug if some of the appearances of the value are modified, but not all of them. Such a bug is hard to find and may remain in the program for a long time. A similar problem may occur if the same hard-coded value is used for more than one parameter value, e.g. an array of 6 elements and a minimum input string length of 6. A programmer may mistakenly change all instances of the value (often using an editor's search-and-replace facility) without checking the code to see how each instance is used. Both situations are avoided by defining constants, which associate names with the values, and using the names of the constants for each appearance within the code.
One important case of hard coding is when strings are placed directly into the file, which forces translators to edit the source code to translate a program. (There is a tool called gettext that permits strings to be left in files, but lets translators translate them without changing the source code; it effectively de-hard codes the strings.)
Soft coding
Soft coding is a computer coding term that refers to obtaining a value or function from some external resource, such as text files, INI files, configuration files, command-line arguments, databases, user input, HTTP server responses. It is the opposite of hard coding, which refers to coding values and functions in the source code.
Programming practice
Avoiding hard coding of commonly altered values is good programming practice. Users of the software should be able to customize it to their needs, within reason, without having to edit the program's source code. Similarly, careful programmers avoid magic numbers in their code to improve its readability and assist maintenance. These practices are generally not referred to as soft coding.
The term is generally used where soft coding becomes an anti-pattern. Abstracting too many values and features can introduce more complexity and maintenance issues than would be experienced with changing the code when required. Soft coding, in this sense, was featured in an article on The Daily WTF.
Potential problems
At the extreme end, soft-coded programs develop their own poorly designed and implemented scripting languages, and configuration files that require advanced programming skills to edit. This can lead to the production of utilities to assist in configuring the original program, and these utilities often end up being soft coded themselves.
The boundary between proper configurability and problematic soft-coding changes with the style and nature of a program. Closed-source programs must be very configurable, as the end user does not have access to the source to make any changes. In-house software and software with limited distribution can be less configurable, as distributing altered copies is simpler. Custom-built web applications are often best with limited configurability, as altering the scripts is seldom any harder than altering a configuration file.
To avoid soft coding, consider the value to the end user of any additional flexibility you provide, and compare it with the increased complexity and related ongoing maintenance costs the added configurability involves.
Achieving flexibility
Several legitimate design patterns exist for achieving the flexibility that soft coding attempts to provide. An application requiring more flexibility than is appropriate for a configuration file may benefit from the incorporation of a scripting language. In many cases, the appropriate design is a domain-specific language integrated into an established scripting language. Another approach is to move most of an application's functionality into a library, providing an API for writing-related applications quickly.
Example
This module embeds a database address, credentials and the VAT rate in its source. Running the same build against a staging database, rotating the password or following a tax change all mean editing and redeploying the code — and the password ends up in version control.
def connect():
return Database(host="10.0.0.12", user="admin", password="s3cr3t!")
def price_with_vat(amount):
return amount * 1.2
Values that differ between environments or change over time now come from the environment; the code keeps only the logic (see also Hardcoded Configuration).
import os
from dataclasses import dataclass
@dataclass(frozen=True)
class Settings:
db_host: str
db_user: str
db_password: str
vat_rate: float
@classmethod
def from_env(cls) -> "Settings":
return cls(
db_host=os.environ["DB_HOST"],
db_user=os.environ["DB_USER"],
db_password=os.environ["DB_PASSWORD"],
vat_rate=float(os.environ["VAT_RATE"]),
)
def connect(settings: Settings):
return Database(host=settings.db_host, user=settings.db_user, password=settings.db_password)
def price_with_vat(amount, settings: Settings):
return amount * (1 + settings.vat_rate)
Adapted from the Wikipedia article “Hard coding” (revision 1355219913), available under the CC BY-SA 4.0 license; the text has been edited and examples added.