Skip to content

Instantly share code, notes, and snippets.

@duggiemitchell
Last active January 27, 2016 14:59
Show Gist options
  • Select an option

  • Save duggiemitchell/45065053cf6ffce84680 to your computer and use it in GitHub Desktop.

Select an option

Save duggiemitchell/45065053cf6ffce84680 to your computer and use it in GitHub Desktop.
What is Hard-Coding?

One thing I love about Codeschool is, the instructors place an emphasis on steering from hard coding in functions; a term I had not heard prior. So instead of using the real value when defining variables and/or functions, we use variables as a placeholder that we can reference later within the program. For an example, say we ar building a simple program calculating the area of a circle for a given radius. If we are hardcoding the program our function would look something like this:

 function areaOfCircle(radius) {
  return 2 * 3.14 * radius;
}
calculateCircumference(1); //returns 6.28

The formula to find the area of a circle is A = πr2. In the above function, the value 3.14 is hardcoded into our program when instead we could have used PI (since PI is not exactly 3.14, it is 3.14159 to be precise). Luckily, in Javascript there is such a property that represents the ratio of a circle's circumference: Math.PI ( Math.PI ≈ 3.14159 ). Imagine, if you will, that this value is used throughout our circle calculation program, only to later discover that the value of PI should be 3.14159265359 rather than 3.14 for accuracy. We'd have to make changes everywhere that value was used. A better function would look something like this:

function areaOfCircle(radius) {
  return 2 * Math.PI * radius;
}
areaofCircle(1); //returns 6.283185307179586

One would say that three thousandths of a number is not much of a big deal, but what if your function calculations were part of a larger program calculating the flight projectory of a rocket to orbit? Anyone that has seen The Martian knows the how critical precise data is... OK I', pulling at straws here, but you get the point.

In Javascript Roadtrip: Ocean of Objects, we are introduced to Objects in Javascript. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case is considered a method. Part 4 explores how to use objects, properties, functions, and methods, and how to create your own objects.

Below, we have a findVehicle function that will accept a vehicle name and list of vehicle objects, and return the current storage location of the requested vehicle.

First, create vehicle Objects using Object Literal Notation

var vehicle1 = {type: "Motorboat", capacity: 6, storedAt: "Ammunition Depot"};
var vehicle2 = {type: "Jet Ski", capacity: 1, storedAt: "Reef Dock"};
var vehicle3 = {type: "Submarine", capacity: 8, storedAt: "Underwater Outpost"};

Second, Put all thevehicle objects in a vehicles array using the array literal syntax passing in each of the vehicle variable names.

var vehicles = [vehicle1, vehicle2, vehicle3];

Lastly, in our findVehicle function expression which accepts the name (for the individual vehicle sought) and list (for the full array of vehicles) as parameters, we will iterate through the list of vehicles. While looping through the array, for each vehicle's type property, it will check that value against the name passed into the function; and if they are equal to eachother, the function will return that vehicle's storedAt property. var findVehicle = function(name, list) { for(var i = 0; i <= list.length; i ++) { if (list[i].type === name) { return list[i].storedAt; } } }; // call findVehicle function passing in Submarine.

findVehicle("Submarine", vehicles); // returns  "Underwater Post" 

Since that vehicle's storedAt property value is at the "Underwater Post" that is the value our function will return. What is interesting about this solution is that we could have easily passed in our already defined variables as arguments to our function, which I initially tried to do and failed the test. By not hardcoding our functions, we allow for better readibility and flexibility in our programs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment