ArticleslgStudy

science

Hard coding

Hard coding is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Hard coding rather than just read about it. In short: 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.

Key takeaways

  • Hard coding belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Hard coding to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Hard coding from memory before moving on to harder problems.

Reference excerpt

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, preprocessor macros, external constants, databases, command-line arguments, and is determined at runtime.

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, preprocessor macros, external constants, 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.

Digital rights management As a digital rights management (DRM) measure, software developers may hard code a unique serial number directly into a program. Or it is common to hard code a public key, creating the DRM for which it is infeasible to create a keygen. On the opposite case, a software cracker may hard-code a valid serial number to the program or even prevent the executable from asking the user for it, allowing unauthorized copies to be redistributed without the need of entering a valid number, thus sharing the same key for every copy, if one has been hard-coded.

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. There was a similar issue with microprocessors in early computers, which started execution at a fixed address in memory.

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, 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:

Profile path Some Windows programs hard-code 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 or above, but would cause an error if 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 SHGetFolderPath function.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Hard coding

Start with the simplest possible case. Write down what Hard coding claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Hard coding before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Hard coding ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Hard coding

In research
Hard coding appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Hard coding in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Hard coding is common in secondary-school and first-year university syllabi. It links to neighbouring topics Anti-patterns, so understanding it makes those chapters shorter.
In everyday life
Look for Hard coding outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Hard coding” →

How to study Hard coding in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Hard coding means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Hard coding out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Hard coding in simple terms?

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…

Why does Hard coding matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Hard coding?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Hard coding.

Tags

  • Anti-patterns

Keep exploring