Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

21
  • 214
    0 and 1 are exceptions to this rule. Commented Apr 7, 2009 at 23:35
  • 51
    @Jonathan Parker, except when they're not (TRUE/FALSE) Commented Jun 2, 2010 at 3:11
  • 121
    Just because a magic number will never change doesn't mean it shouldn't be replaced by a constant. My code is full of global constants like HzPerMHz and msecPerSecond. These will never change, but they make the meaning clearer, and provide some protection against typos. Commented Jun 3, 2010 at 23:33
  • 20
    ... It is essential to get in the habit of defining constants to give meaning to otherwise "arbitrary" numbers, even in trivial methods, where it seems obvious. Why? Because in the future, that method may get added to. What at first seemed obvious, is now somewhere within many lines of code. As for being spaghettish, AFAIK all decent modern IDEs make it trivial to find the value of a constant variable. Usually can just hover over any use of the variable. And even back in the day, when we didn't have such niceties, it was very, very worth it, in the long run. Commented Sep 29, 2014 at 0:26
  • 12
    @Sergey "What about things like: connection.setTimeout(50); Should 50 be a constant here? It seems to be pretty clear, that 50 is a connection timeout" - It's a magic number regardless of a single or multiple call. Why 50? Why not 51? You may need to change it because you're deploying to an environment which requires different timeouts. I'd much rather change a constant than hunt through code. Commented Apr 9, 2015 at 9:47