Reference table values in a war against magic numbers

Posted by Alex N. on Stack Overflow See other posts from Stack Overflow or by Alex N.
Published on 2010-05-29T19:44:12Z Indexed on 2010/05/29 19:52 UTC
Read the original article Hit count: 176

This question bugged me for years now and can't seem to find good solution still. I working in PHP and Java but it sounds like this maybe language-agnostic :)

Say we have a standard status reference table that holds status ids for some kind of entity. Further let's assume the table will have just 5 values, and will remain like this for a long time, maybe edited occasionally with addition of a new status. When you fetch a row and need to see what status it is you have 2 options(as I see it at least) - put it straight ID values(magic numbers that is) or use a named constant. Latter seem much cleaner, the question though is where those named constants should leave? In a model class? In a class that uses this particular constant? Somewhere else?

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about language-agnostic