• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

magic numbers

 
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We all (hopefully) know that "magic numbers" - numeric literals - in code are generally A Bad Thing. In C you would typically replace them with macro #defines.

So I thought I'd share this with you - it's been written by professionals (not in my company), so it must be right:
And yes, many of them are actually used 🤣
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like it comes straight from TheDailyWTF
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you must admit it does make the code easier to maintain - if say the value of 106 changed to 105, you'd only need to update the #define in that file. Marvelous.
 
Marshal
Posts: 79247
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the number on line 17 should be called NR_0xTEN.
 
Campbell Ritchie
Marshal
Posts: 79247
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which country does that code come from? Germany?
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure if I'm missing a joke, but I believe it originates from an American company, although that company no longer exists.

I didn't get the NR_OxTEN comment either  
 
Master Rancher
Posts: 4831
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's a reference to the German language and its tendency to make really long words.  Kind of like Java .

The NR_0xTEN refers to the fact that 16 can be written 10 in base 16, shown in Java literals as 0x10.  Which looks like ten, but isn't.

Well, I laughed when I saw it.

As for the original post... to be honest, I've seen this sort of thing more than once in real code - it's sadly common.  I think "never use magic numbers" is often taken to ridiculous extremes.  I once had a code review from a regular presenter at the NYC Java users group - we worked for the same bank at the time - and he was encouraging the "introduce constant"  refactor so frequently, I pointed out it was leading to exactly this sort of silliness.  I don't think he got the point.  Sigh.
 
Marshal
Posts: 28233
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are professionals but they don't know how to spell FORTY?

And why do we have ONE_HUNDRED_SIX but ONE_TWENTY (instead of ONE_HUNDRED_TWENTY)?

Still... it's magic so it must be beyond my comprehension.
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I think it's a reference to the German language and its tendency to make really long words.  Kind of like Java .

Right - got it.

The NR_0xTEN refers to the fact that 16 can be written 10 in base 16, shown in Java literals as 0x10.  Which looks like ten, but isn't.

My bad - I know about hex numbers, but hadn't spotted that Campbell was referring to 16  

So, reading it again...  
 
Mike Simmons
Master Rancher
Posts: 4831
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Still... it's magic so it must be beyond my comprehension.


I guess we now have a sufficiently advanced technology.
 
Bartender
Posts: 5466
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm... Project Euler nr 17 had me writing one hundred AND twentyone, stating that was official English. I don't mind replacing magic numbers with magic letters, but if so, use correct spelling
 
Campbell Ritchie
Marshal
Posts: 79247
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I think it's a reference to the German language and its tendency to make really long words. . . .

Yes, and the fact that the German abbreviation for number (Nummer) is Nr.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would propose the following refactorings:

Because why not?
 
Campbell Ritchie
Marshal
Posts: 79247
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic