Content deleted Content added
Tag: Reverted |
CortexFiend (talk | contribs) Link suggestions feature: 3 links added. |
||
(7 intermediate revisions by 6 users not shown) | |||
Line 1:
{{Short description|Putting data in the source code of a program}}
{{redirect|Hardcoded|the video game|Hardcoded (video game)}}
{{More citations needed|date=May 2021}}
'''Hard coding''' (also '''hard-coding''' or '''hardcoding''') is the software development practice of embedding data directly into the [[source code]] of a [[computer program|program]] or other executable object, as opposed to obtaining the data from external sources or generating it at [[Run time (program lifecycle phase)|runtime]].
Hard-coded data typically can
Data that is hard-coded is best suited for unchanging pieces of information, such as [[physical constant
==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.<ref name="Dustin2002">{{cite book|author=Elfriede Dustin|title=Effective Software Testing: 50 Specific Ways to Improve Your Testing|url=https://books.google.com/books?id=K0qWBUOAf6IC&pg=PA188|year=2002|publisher=Addison-Wesley Professional|isbn=978-0-201-79429-8|pages=188–}}</ref>
Hard coding is often required, but can also be considered an [[anti-pattern]].<ref name="Janca2020">{{cite book|author=Tanya Janca|title=Alice and Bob Learn Application Security|url=https://books.google.com/books?id=UT4DEAAAQBAJ&pg=PA15|date=14 October 2020|publisher=Wiley|isbn=978-1-119-68740-5|pages=15–}}</ref> 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
The term "hard-coded" was initially used as an analogy to hardwiring circuits
In the context of run-time extensible [[collaborative development environment]]s such as [[Multi-user dungeon|MUD]]s,
==
==
As a [[digital rights management]] measure, software developers may
On the opposite case, a
== Fixed installation path ==
Line 33:
==Startup disk==
Some "[[copy protection|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, the great majority of PCs were fitted with at least one floppy drive, but floppy drives later fell out of use. A program hard-coded in that manner 15 years ago could face problems if not updated.
==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:
Some Windows programs hardcode the path to <code>[[My Documents]]</code> as <code>''ProfilePath''\My Documents</code>. These programs would work on machines running the English version, but on [[internationalization and localization|localized]] versions of Windows this folder normally has a different name. For example, in Italian versions the <code>My Documents</code> folder is named ''Documenti''. <code>My Documents</code> 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 <code>SHGetFolderPath</code> function.▼
===Profile path===
Some Windows programs hard code the profile path to developer-defined locations such as <code>C:\Documents and Settings\''Username''</code>. This is the path for the vast majority of [[Windows 2000]] 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 <code>GetUserProfileDirectory</code> function or to resolve the <code>%userprofile%</code> [[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
==Solution==
Line 55 ⟶ 61:
In rare cases where the possible number of inputs is small enough, 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 [[algorithm]]ic one (even though the hard-coded program might be the output of an algorithmic program).
==
'''
===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 number (programming)#Unnamed numerical constants|magic numbers]] in their code
The term is generally used where
===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
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<!-- but generally aren't! -->, 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. <!-- Witness the scores of CMS systems that are so much harder to work with than a bunch of html files in www/data! -->
To avoid
===Achieving flexibility===
Several legitimate design patterns exist for achieving the flexibility that
==See also==
|