In each method, the functions inside depend on the value of the variable (which I pass to them) to maintain their correct spot within an array. They also use another variable (which is passed to them, them returned) to maintain the index of an array in which I don't want the calling function to have any kind of manipulation.
So, when I pass the value that they don't return, but I need it decremented, is it bad programming practice to do this:
Or is it better to do this:
someVariable = someVariable - 3;
The term magic number also refers to the bad programming practice of using numbers directly in source code without explanation. In most cases this makes programs harder to read, understand, and maintain. Although most guides make an exception for the numbers zero and one, it is a good idea to define all other numbers in code as named constants.
static final MILLISECS_IN_DAY = 1000 * 60 * 60 * 24;
Like in my case, I know that the method will only deal with three indexes in the array (and other methods a differing amount) because that's all the method can handle at a time.
So rather than:
marker -= 3;
It would be proper to put:
MARKER_METHOD1 = 3;
marker -= MARKER_METHOD1;
Edit:: Ok, after reading the article more thoroughly I see why. Of course I know what the number is and why I use it, but if I've written hieroglyphics and included no Stone, another programmer later may wish death upon me.
[ December 13, 2006: Message edited by: Nathan Leniz ]
Originally posted by Garrett Rowe:
...Sorry, couldn't resist
I knew that example would draw fire, but I thought it would be for daylight savings adjustments.
Note: 1000 * 60 * 60 * 24 is just a nicety, saving someone the trouble of verifying that 86400000 is (usually) correct. But yeah, it's still technically "magic numbers."
So even if we have a method that does something, and we know exactly by how much it does something, we should assign a number controlling it a variable name?
Personally, I would say that yes, you should. In this example, you're right, it's really not that big a deal. But let's examine this like it was a "real world" program.
It is going to be developed and maintained over time - perhaps years. New features will be added. Enhancements made. You know there are only 3 indexes, so you keep using the contstant of 3, over and over and over. Then, somebody else adds in some new feature, that by coincidence, also uses the number 3. You get to the point where there are dozens and dozens of "3"'s in your code.
Now, the new boss comes along. He says, "Yeah, 3 is great for your stuff, but we really need it to be 4!!!".
What do you do? You have to slog through hundreds of lines of code, looking at each and every 3, and decide if that specific one needs to be changed, or if it's a 3 that is not related to your three. It might take days, and you're probably going to miss a few that may not be discovered until it's caused serious damage somehow.
Now, write your code with a defined constant. Every new feature uses that defined constant. When somebody writes those new, unrelated features, they create their OWN constant, which also happens to have a value of 3.
NOW when it comes time to change yours to a 4, it'll take you about five minutes to find where it's defined, change one charater, save the file, and recompile. You're done, you KNOW you got ALL the changes you needed, and ONLY the changes you needed.
So, for a simple program like this, it probably doesn't make much difference. But it's a DARN good habit to get into now.