Hard code
Encyclopedia
Hard coding refers to the software development practice of embedding what may, perhaps only in retrospect, be regarded as input or configuration data directly into the source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...

 of a program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

 or other executable object, or fixed formatting of the data, instead of obtaining that data from external sources or generating data or formatting in the program itself with the given input.

Overview

Considered an anti-pattern
Anti-pattern
In software engineering, an anti-pattern is a pattern that may be commonly used but is ineffective and/or counterproductive in practice.The term was coined in 1995 by Andrew Koenig,...

, 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 sometimes required. 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, softcoding 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 coined in 1990 by R. Galichon (then a Programmer/Analyst at Mobil
Mobil
Mobil, previously known as the Socony-Vacuum Oil Company, was a major American oil company which merged with Exxon in 1999 to form ExxonMobil. Today Mobil continues as a major brand name within the combined company, as well as still being a gas station sometimes paired with their own store or On...

). The term was used as an analogy to hardwiring circuits - and was meant to convey the inflexibility which results from its usage within software design and implementation.

In the context of run-time extensible collaborative development environment
Collaborative Development Environment
A collaborative development environment is an online meeting space where a software development project's stakeholders can work together, no matter what timezone or region they are in, to discuss, document, and produce project deliverables...

s such as MUD
MUD
A MUD , pronounced , is a multiplayer real-time virtual world, with the term usually referring to text-based instances of these. MUDs combine elements of role-playing games, hack and slash, player versus player, interactive fiction, and online chat...

s, hardcoding also refers to developing the core engine of the system responsible for low-level tasks and executing scripts, as opposed to softcoding which is developing the high-level scripts that get interpreted by the system at runtime. In this case, the term is not pejorative and refers to general development, rather than specifically embedding output data.

Hardcoding and piracy

As an anti-piracy measure, software developers may hardcode a unique serial number
Serial number
A serial number is a unique number assigned for identification which varies from its successor or predecessor by a fixed discrete integer value...

 directly into a program. A program that has a unique serial number may regularly check its maker's website to verify that it hasn't been blacklisted as pirated. If that website moves or the company goes out of business, this may cause the program to fail, even for perfectly legal users, if that check is programmed to fail when no response is received.

On the opposite case, a software cracker may hardcode a valid serial number to the program or even prevent the executable from asking the user for it, allowing illegal copies to be redistributed without the need of entering a valid serial, thus sharing the same key for every copy, if one has been hardcoded.

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 vast majority of people prefer installation to the default drive and directory and testing might not include another installation directory. However it is advisable for programmers and developers not to fix the installation path of a program, since the default installation path is different in different natural language
Natural language
In the philosophy of language, a natural language is any language which arises in an unpremeditated fashion as the result of the innate facility for language possessed by the human intellect. A natural language is typically used for communication, and may be spoken, signed, or written...

s, and different computers may be configured differently. It is a common assumption that all computers running Microsoft Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

 have the primary hard disk
Hard disk
A hard disk drive is a non-volatile, random access digital magnetic data storage device. It features rotating rigid platters on a motor-driven spindle within a protective enclosure. Data is magnetically read from and written to the platter by read/write heads that float on a film of air above the...

 labelled as the C: drive, but this is not the case.

There was a similar problem with the microprocessor
Microprocessor
A microprocessor incorporates the functions of a computer's central processing unit on a single integrated circuit, or at most a few integrated circuits. It is a multipurpose, programmable device that accepts digital data as input, processes it according to instructions stored in its memory, and...

s in early computers, which were built to expect the computer's initial program code to start at the address 0 of the memory pool (or at another fixed address). This was a failsafe layout for units designed for a narrow field of purposes, but lacked much expandability.

Startup disk

Some "copy-protected" programs look for a particular file on a floppy disk or flash drive on startup to verify that they are not unauthorized copies. If the computer is replaced by a newer machine, which doesn't have a floppy drive, the program that requires it now can't be run, since the floppy disk can't be inserted.

This last example shows why hard coding may turn out to be impractical even when it seems at the time that it would work completely. In the 1980s and 1990s a PC without a floppy drive would likely have seemed unthinkable, but they are becoming more common today. A program hard coded in that manner 15 years ago could face serious problems if no update were made. Again, many companies will not be concerned if their programs can't be run 15 years later, and it may even be a form of planned obsolescence
Planned obsolescence
Planned obsolescence or built-in obsolescence in industrial design is a policy of deliberately planning or designing a product with a limited useful life, so it will become obsolete or nonfunctional after a certain period of time...

. And in any case it is impossible to provide in the code for all possible future developments.

Special Folders

Some Windows operating systems have so called Special Folders
Special Folders
On Microsoft Windows, a special folder is a folder which is presented to the user through an interface as an abstract concept, instead of an absolute folder path...

which organize files logically on the hard disk. There are problems that can arise involving hardcoding:

Profile path

Some Windows programs hardcode the profile path to developer-defined locations such as C:\Documents and Settings\Username. This is the path for the vast majority of Windows 2000
Windows 2000
Windows 2000 is a line of operating systems produced by Microsoft for use on personal computers, business desktops, laptops, and servers. Windows 2000 was released to manufacturing on 15 December 1999 and launched to retail on 17 February 2000. It is the successor to Windows NT 4.0, and is the...

 or above, but this would cause an error if the profile is stored on a network or otherwise relocated. The proper way to get it is to call the GetUserProfileDirectory function or to resolve the %userprofile% environment variable. Another assumption that developers often make is assuming that the profile is located on a local hard disk.

My Documents folder path

Some Windows programs hardcode the My Documents
My Documents
On Microsoft Windows computer operating systems , My Documents is the name of a special folder on the computer's hard drive that the system commonly uses to store a user's documents, music, pictures, downloads, and other files.- Overview :Microsoft first introduced the "My Documents" folder in...

 folder path to ProfilePath\My Documents. The program would work on most computers, but on localized versions of Windows (for example in Italian version My Documents
My Documents
On Microsoft Windows computer operating systems , My Documents is the name of a special folder on the computer's hard drive that the system commonly uses to store a user's documents, music, pictures, downloads, and other files.- Overview :Microsoft first introduced the "My Documents" folder in...

 folder is translated as Documenti), or if the My Documents folder is redirected using Folder Redirection in Group Policy in Windows 2000 or above a serious error would occur. The proper way to get it is to call the SHGetFolderPath function.

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
Magic number (programming)
In computer programming, the term magic number has multiple meanings. It could refer to one or more of the following:* A constant numerical or text value used to identify a file format or protocol; for files, see List of file signatures...

. 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, eg 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 constant
Constant (programming)
In computer programming, a constant is an identifier whose associated value cannot typically be altered by the program during its execution...

s, 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. (gettext
Gettext
In computing, gettext is an internationalization and localization system commonly used for writing multilingual programs on Unix-like computer operating systems. The most commonly-used implementation of gettext is GNU gettext, released by the GNU Project in 1995.- History :gettext was originally...

 is a tool 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.)

Hard coding in competitions

In computing competitions such as the International Olympiad in Informatics
International Olympiad in Informatics
The International Olympiad in Informatics is an annual computer science competition for secondary school students. The first IOI was held in 1989 in Pravetz, Bulgaria....

, contestants are required to write a program with specific input-output pattern according to the requirement of the questions.

In cases where the possible number of inputs is finite, a contestant might consider using an approach that maps all possible inputs to their correct outputs. This program would be considered a hard-coded solution as opposed to an algorithmic
Algorithm
In mathematics and computer science, an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning...

one.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK