Hardcoded

5404
9
03-14-2015 09:27 AM
PROBERT68
Frequent Contributor

What does it mean hardcorded ? I am trying to get a grasp of what it means ? Thanks

Tags (2)
0 Kudos
9 Replies
RichardFairhurst
MVP Honored Contributor

Hard coded (not hardcorded) means that actual values that the user cannot change are used in the code rather than variables that can be changed at run time to match user inputs.  So if I hard code a path, file name, and field names that work for one of my data sources on my computer in the code, then it will not work for you unless you have an identical path to the same file name with the same field names on your computer.  If on the other hand I replaced those hard coded values with variables that take user input at run time, then the path, file name, and field names could be chosen by each user for their particular computer at run time.

Programming with hard coded values is usually done first to test the code to make sure that the core program function is working properly for a known input.  After that is established the hard coded values are frequently replaced by run time variables that generalize the program to work for any valid inputs that meet the minimum criteria of the program.  Often many additional tests have to be written for run time variables to ensure that the program validates the run time user inputs to make sure they meet the minimum requirements that the hard coded input met before allowing the program to use them.

PROBERT68
Frequent Contributor

Do you have a good website that give the examples ?

0 Kudos
RichardFairhurst
MVP Honored Contributor

I don't really have a website to refer you to, but I can give a very simple example.

Here is some hard coded python code:

import arcpy
from arcpy import env

# Set hard coded workspace
env.workspace = "C:/data"

# Set hard coded copy inputs
arcpy.Copy_management("majorrds.shp", "C:/output/majorrdsCopy.shp", "")
arcpy.AddIndex_management("C:/output/majorrdsCopy.shp", "STREET_NAM", "StreetIndex", "UNIQUE", "ASCENDING")

With this code I have hard coded all inputs.  To change the inputs I have to find every place I use a given value and replace it with a new input.  So all of the paths have to be on any computer than runs this (both "C:/data" and "C:/output") and the new file I create "C:/output/majorrdsCopy.shp" has to be found and replaced twice.  Also "majorrds.shp" has to contain a field named "STREET_NAM" in order for it to be in the output copy so I can index that field.

Below is the same code but it uses variables that will make it easier for me to change different parts of the inputs before I run the code by just changing the variable values.  However, these variables are still based on hard coded values, because the actual string values are written in the code:

# Import system modules
import arcpy
from arcpy import env

# Set local variables
inwksp = "C/data"
outwksp = "C:/output"
in_data =  "majorrds.shp"
out_data = "majorrdsCopy.shp"
out_data_full = outwksp + "//" + out_data
data_type = ""
field_name = "STREET_NAM"
index_name = "StreetIndex"
unique_option = "UNIQUE"  # use "UNIQUE" or "NON_UNIQUE"
ascending_option = "ASCENDING" # use "ASCENDING" or "DESCENDING"

env.workspace = inwksp

arcpy.Copy_management(in_data, out_data_full, data_type)
arcpy.AddIndex_management(out_data_full, field_name, index_name, unique_option, ascending_option)

Every variable can have its values substituted with another string value at the top of the program without having to find where it is actually used in the code (finding all the lines of code that use a hard coded value can be much more difficult as the program gets longer).  This makes it easier for me to test the code with different inputs during the testing phase.

Now here is the code where nothing is hard coded and it assumes there is a user input interface (like a tool) where the user chosses all of the variable inputs at run time:

# Import system modules
import arcpy
from arcpy import env

# Set run time variables
inwksp = arcpy.GetParameterAsText(0)
outwksp = arcpy.GetParameterAsText(1)
in_data =  arcpy.GetParameterAsText(2)
out_data = arcpy.GetParameterAsText(3)
out_data_full = outwksp + "//" + out_data
data_type = ""
field_name = arcpy.GetParameterAsText(4)
index_name = arcpy.GetParameterAsText(5)
unique_option = arcpy.GetParameterAsText(6)  # use "UNIQUE" or "NON_UNIQUE"
ascending_option = arcpy.GetParameterAsText(7) # use "ASCENDING" or "DESCENDING"

env.workspace = inwksp

arcpy.Copy_management(in_data, out_data_full, data_type)
arcpy.AddIndex_management(out_data_full, field_name, index_name, unique_option, ascending_option)

Unlike when I change the variables, because the user is providing every input I cannot be sure that every input makes sense.  This program would require validation of the inputs in the tool set up by configuring the inputs to only accept certain values, setting up the Tool Validator to test for valid path and file name combinations and perhaps with validation steps set up as part of the code shown.

DanPatterson_Retired
MVP Emeritus

It is an 'anti-pattern' where the assumptions about the programming system environments are embedded in its implementation... although anti-pattern is a term with a relative brief history, hardcoding has a long history in programming... generally a frowned upon practice but at least your code works...at least for the programmer

0 Kudos
RichardFairhurst
MVP Honored Contributor

Most over-night batch programs that perform nightly updates are hardcoded, since all user inputs were predefined during the program creation for a specific implementation and it is not desirable to wait for user input at run time.  The mid-level stage that converts all hard-coded values to variables is best for that code, since changing the code in response to database design changes are relatively easy to implement as long as the core function of the batch job is not changed.  That level of code also can be easily adapted as a template for other batch jobs.  Error trapping and validations are normally designed around the possibility of system level failures (a server went down) and log them so that recovery can be performed,

However, anything that needs an interactive interface has to go beyond hard coded values.  In many cases, once user input is involved the code devoted to validation and error trapping can become much larger than the code that actually performs the task the program was designed to do, since users are unpredictable and may fail to do anything that your program expects and may violate every rule it follows.

DarrenWiens2
MVP Honored Contributor

This discussion is so unnecessarily long. Hardcoded values are directly written in the code, rather than obtained from user input or existing data.

TedKowal
Occasional Contributor III
DanPatterson_Retired
MVP Emeritus

Finally someone made reference to Dr Ghou Ghull's reference documents on 'anti-pattern' .... which is where the references to the original term is cited....I suspect someone was having a little fun... at least I hope that was the case... if not... seriously????

0 Kudos
TedKowal
Occasional Contributor III

LOL

0 Kudos