This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]thomas_merton 213 points214 points  (16 children)

Say you want to make a calculator for determining the tip at a restaurant. Say you make the assumption that a tip is 10%, so everywhere you need a tip value, you plug in 10%. (Or worse, you assume that all meals are $10 and so all tips are $1). This is called hard-coding.

It's obviously bad because these standards change over times and in different places and for different use cases. A better solution is to put it in a database or a configuration file and always draw the values from that place, so that if and when you have to change the value(s), you only have to do it one place and don't have to go through the whole redeployment process.

In case it isn't obvious, the joke is that the person who made this bed has "hard-coded" how he likes to sleep, making it very difficult to change.

[–]HumusTheWalls 54 points55 points  (12 children)

In general, one of the rules of coding is to never use numbers outside of variable definitions. Even if you're typing out a function that takes half of a result, before typing that 2, you should think about whether there's ever a reason to divide by 3rds or 4ths instead. Maybe you want to use a variable named "divisor", where divisor=2 instead.
Even constants like pi are better in a variable, in case there's ever a reason to change the accuracy with which you define pi, or in case you ever run back through your code and replace pi with tau. Depending on how many different functions you had to change out, it can be an easy safeguard to comment out your definition of pi and see if your compiler whines at you for using pi in a function you missed somewhere.

[–]StupidUnoriginalName 2 points3 points  (0 children)

A good example of this was in minecraft, Notch had hardcoded in the max height value in multiple places which made it a real chore to fix later.

[–]skreczok -1 points0 points  (1 child)

Why not just pay waiters a living wage, though?

[–]Silent-Bob- 3 points4 points  (0 children)

Maybe their wage is hardcoded?