![]() 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 |