The semantics of most programming languages will not allow us to use lone basic values, except (perhaps) as data (i.e. your program), which is not entirely clear from context or could be abused in good or bad context(s). number) has no commonly known semantic (like Pi), but has a locally known semantic (e.g. number) in context, but its meaning remains unknown, unknowable, unclear or confusing.Īn overarching dependency of "magic" is how the lone basic value (e.g. number) from which its meaning is unknown, unknowable, unclear or confusing. Only the second is of primary importance for programmers and code: We have two scenarios for our magic basic values. The less known, less clear, and more confused the reader is, the more "magic" the basic value is. Therefore, we identify magic numbers by measuring the ability of a code reader to know, be clear, and understand the meaning and purpose of a basic value from its surrounding context. A basic value is not magic when its semantic meaning or purpose-of-being-there is quickly and easily known, clear, and understood (not confusing) from the surround context without special helper words (e.g. What do we mean by "magic"? To be precise: By "magic", we intend to point to the semantics (meaning or purpose) of the value in the context of our code that it is unknown, unknowable, unclear, or confusing. The issue is not the data type, but the "magic" aspect of the value as it appears in our code text. Basic values are manifest entities such as integers, reals, doubles, floats, dates, strings, booleans, characters, and so on. "Create a constant, name it after the meaning, and replace the number with it." - Martin Fowlerįirst, magic numbers are not just numbers. Semantic: The meaning or purpose of a thing. Symbolic Constant -> Provides both correct semantic and correct context for use Also, everything only defined within 1 function might be acceptable, but that depends on Context. SmtpClient.DefaultPort = 25 or TCPPacketSize = whatever (not sure if that is standardized). Places where Magic Numbers are appropriate is everything that is defined through a standard, i.e. That's why it's best to have such ambiguous and arbitrary numbers in exactly 1 place - " const int NumOrdersToDisplay = 50", because that makes the code more readable (" if a < NumOrdersToDisplay", it also means you only need to change it in 1 well defined place. " if a < 50 then bla" - if you encounter that in the middle of a complicated function, other developers who are not familiar with the code may ask themselves "WTF is 50?" your Session.Timeout = 50 call, which is also set to 25 and users start reporting too frequent timeouts).Īlso, the code can be hard to understand, i.e. Find/Replace may not work, because 50 may be used for other things, and blindly replacing 50 with 25 can have some other bad side effects (i.e. Now, what happens when someone decides to change 50 to 25? or 75? or 153? You now have to replace the 50 in all the places, and you are very likely to miss it. Now, what you do is you have the 50 in different places - your SQL script ( SELECT TOP 50 * FROM orders), your Website (Your Last 50 Orders), your order login ( for (i = 0 i < 50 i++)) and possibly many other places. 50 is the Magic Number here, because it's not set through standard or convention, it's a number that you made up for reasons outlined in the spec. A Magic Number is a hard-coded value that may change at a later stage, but that can be therefore hard to update.įor example, let's say you have a Page that displays the last 50 Orders in a "Your Orders" Overview Page.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |