A comment on ‘Clean Code by Robert C. Martin’

On page 300 of his book Clean Code, Robert C. Martin discusses the use of magic numbers (G25: Replace magic numbers with named constants). Overall I agree with his reasoning, magic numbers like the number of rows of a report or number of columns of a grid or that sort of thing should be replaced by named constants.

I also agree that obvious numbers, like the author’s own example in the book, where he uses 8 for an eight hour work day, doesn’t actually need to be replaced. By the context of the code, the author reasons (and I agree) the hardcoded number 8 is sufficiently explained.

He moves on to argue that the number 5280, for the number of feet to a mile, is also self-explanatory. Like 3600 seconds in one hour or 24 hours to a day. Martin reasons that the number of feet to a mile is so commonplace that the number 5280 is immediately recognizable by a programmer.

And in the FEET_PER_MILE case, the number 5280 is so very well known and so unique a constant that readers would recognize it even if it stood alone on a page with no context surrounding it.

Here Martin, in my opinion, makes a mistake. While the number of feet to a mile may be as clear as day to an American programmer (or any programmer used to the Imperial measuring system),  that number will be almost a complete mystery to a programmer used to the metric system (as are most programmers in the world).

Now, I’m sure that Robert C. Martin has reflected over this and remarked on this on occasion and in online corrections, but in my version of the book (printed in 2009), the argument remains.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.