Tick-the-Code In Action: Rule MAGIC
This video shows how fast ticking code can be. As you can see, a trained Tick-the-Code checker can find and mark (=tick) several rule violations in a minute. This is the fastest rule to check of the 24 Tick-the-Code rules. It is an excellent rule to start with and it points sometimes to code that is unnecessarily hard to maintain.
Three things need to happen when a customer reports an error in a software product. First the cause of the error needs to be found, then the cause needs to be removed and the product retested. In most cases, it is the finding that takes the most time. Errors are often easy to fix, once the cause has been located. In Tick-the-Code, it is the finding phase that has been made as fast as it humanly can be.
The rule MAGIC is used to look for hardcoded numbers, character constants and strings of characters. When source code violates the MAGIC rule, maintaining it is more difficult than it needs to be. Maintenance becomes difficult because the code doesn't contain all the information it could and should.
Good quality code uses descriptive names for constant numbers and character constants. For localization reasons, it is also a good idea to keep character strings in one place instead of sprinkling them in the code.
Ticking all hardcoded numbers, character constants and strings leads to many false positives, too. There are situations where a plain number is most communicative. A count is best expressed with a number and in a 'for' loop it is completely fine to use '0' as an initializer. This is, however, not a problem for Tick-the-Code.
Another objection you might have is that it would be much faster and reliable to look for magic numbers with a program. Ticking them manually has its value too. Assume that you're really not in the mood to do a code inspection. You're in a hostile state of mind. You just can't find anything complicated in the source code. What if you looked first for something really simple, something that anybody, even a program could do? You'd succeed for sure and each tick you'd mark would move your mood from anger or resentment towards interest and relaxation. Once you're calm, you can move on to more challenging rules.
You might think that there will be masses of findings if you tick even every zero and one. Sure, but it is very likely that out of a thousand lines of code, you'd find maybe around a hundred magic numbers or so. That would already mean that you have filtered the source code to be analyzed from a thousand LINES down to a hundred FINDINGS. You've effectively reduced the amount of code to less than 10%.
If you still don't agree that looking for magic numbers can be useful, how come you still have errors in your code? Why is it so difficult to maintain your code? If you don't think it is the magic numbers, Tick-the-Code offers 23 other rules to try.
In Principles of Good Coding, rule MAGIC belongs to the category Missing Info. Using a literal number in the middle of executable code is missing information about its source - and what's more important - information about its relationship with other literal numbers. When you need to change a literal number, you must change all its instances (and only those) and all other numbers that somehow depend on its value. That task is unnecessarily difficult if you use magic numbers.