preload

On hard-coding of test data (1)

Posted by Albert Gareev on Jan 20, 2007 | Categories: NotesProblems

Hard-coding – storing data within the code

A printed document is called “hard copy”, while its electronic version is “soft”. We can easily apply a change on a soft copy.
And yet the code, compiled and assembled into an application, while stored electronically, is a “hard” copy too. Applying changes to it is almost an impossible thing – you would have to change machine code without breaking it. And if changes are applied on a source code, it needs to be compiled and assembled again, as well as tested and approved before promoting it to production.

Examples (VBScript).

A program has a direct reference to a file.

Dim boolResult
...
...
boolResult = objFSO.FileExists("c:\data\document.txt")

Username and password are defined directly in script.


objUsername.Set "admin1"
objPassword.Set "password1234"

Maintenance is a major issue caused by hard-coding

Modification, improvement, or bug fixing jobs are code maintenance jobs. Maintenance is always counted as expense.
Hard-coding affects maintenance cost with the following:

 

  • Hard-coding of the same value(s) across the code multiplies change effort and creates risk of missing some entries
  • Hard-coding requires every code user, who has own values, to branch/maintain a separate copy of the code
  • Hard-coding scatters data values across the code, making hard to determine and maintain the whole set of data

 

Mapping is a simplest trick to avoid hard-coding

In the nutshell, mapping is storing of a value within a code under a given name. In programming languages such entity is referred as “constant”.

Examples (VBScript).

'Declarations
Const FilePath = "c:\data\document.txt"
Const Username = "admin1"
Const Password = "password1234"
...
'Code
...
boolResult = objFSO.FileExists(FilePath)
...
objUsername.Set Username
objPassword.Set Password

Mapping allows to bring all the values together (usually, in a separate code module, or on top of a code module). Mapping also reduces change effort and change risk.

…to be continued.


  • Leave a Reply

    * Required
    ** Your Email is never shared

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.