This morning’s keynote speaker at RailsConf mentioned, in passing, working on some code without named constants. When something needed to be changed you’d have to find the right instance of it and change it, and hope you changed it everywhere you needed to. It’s elementary that you should use named constants for just that reason. But this story reminded me of an early programming experience I had with FORTRAN IV on a CDC 6400.
You see, when you write a 1 in your code, you’re kind of using a named constant. That 1 is is the name of a value (that happens to be one, or 000000000000000000000000000000000000000000000000000000000001 in bits on the 6400). We always expect that the value of the constant named 1 has to be one. But in my freshman programming class, I managed to change the value of 1 in my source code to zero. FORTRAN programmers will probably know how I did that.
This made for a wonderfully oddly behaved program. The first time through a loop always worked, later ones always failed, and it didn’t matter what values I set before each run. The story had a happy ending: I learned how to read core dumps and a bit more about machine language, and moved on to other programming languages that make it harder to change the values of literal constants.