Tony Morris
Java Q&A (FAQ, Trivia)
Check out my kickstarter CLICK HERE
My book, my movies, my videos, my podcasts, my events ... the big collection of paul wheaton stuff!
Originally posted by Tony Morris:
It is vernacular for an ill-founded fear whereby many developers expose constant values to excessive scope, resulting in confusion, and providing the illusion that they are contributing positively to the robustness of their software, while actually doing the contrary.
Don't believe the hype.
ASCII silly question, Get a silly ANSI.
(Will post an example when I can think of it.)
Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
ASCII silly question, Get a silly ANSI.
Originally posted by Tony Morris:
It is vernacular for an ill-founded fear whereby many developers expose constant values to excessive scope, resulting in confusion, and providing the illusion that they are contributing positively to the robustness of their software, while actually doing the contrary.
Don't believe the hype.
Er, what? My understanding of the term magic number is that it's simply a literal number for which the logical reasoning behind it is not immediately obvious. I was not aware there was any hype surrounding it. I was also not aware a developers choice to expose or not expose it had anything to do with whether or not it's a magic number.
"I'm not back." - Bill Harding, Twister
Yes you got it right Steve.
Originally posted by Stuart Ash:
Adding to what Paul's saying, even I've had this dilemma at times. I have mostly insisted that developers in my team adhere to the convention of using constants instead of magic numbers. But like he points out, there seem to be cases where using the plain number seems more logical or natural. (Will post an example when I can think of it.)
What do others here think of this.
Originally posted by Ken Blair:
My understanding of the term must be warped because I don't see what exposing or not exposing a magic number has to do with it being a magic number nor do I see what defining a magic number as a static variable or just writing it in has to do with it either.
Originally posted by Ernest Friedman-Hill:
12 is a magic number. OZ_PER_PKG is a named constant. Named constants are better.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Srinivasa Raghavan:
In the above code will '0' be treated as a magic number.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Tony Morris:
It is vernacular for an ill-founded fear whereby many developers expose constant values to excessive scope, resulting in confusion, and providing the illusion that they are contributing positively to the robustness of their software, while actually doing the contrary.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Paul Wheaton:
Is 4 really a magic number? Some folks would argue it is.
I think "4" better than "charArrayGrowthFactor" which you can look up to see that it's "4".
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss:
Give me an example of a constant that results in confusion, and I bet I can show you a solution without a magic number that is better than the equivalent with the magic number.
The certainly can do damage by misapplying the technique of extracting a constant - most often by giving it a bad name and/or scope. That doesn't mean that the technique itself isn't valuable.
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister
Originally posted by Tony Morris:
I'll bet you can show me an example where you shift the "magic number" (cough choke) to a different position, that exceeds required scope (implies requirement defect). You cannot "omit" the "magic number", unless you redefine the term, which is yet to have any real definition stated (hence its superficial existance).
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Tony Morris:
I'm always the one who has to provide proof. I call unfair. Just because it's not in all the text books... OK, I'll stop whinging now.
A definition has not been provided, only some loose talk. Therefore, I'll provide a context. Feel free to clarify it. If it suits your definition, I'll point out why it exceeds scope. I think it should be obvious though.
Originally posted by Tony Morris:
I'm always the one who has to provide proof. I call unfair.
A definition has not been provided, only some loose talk.
void m()
{
final int numberOfDwarfs = 7;
System.out.println("Snow White and the " + numberOfDwarfs + " Dwarfs");
}
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Paul Wheaton:
A good example of a "magic number" is something like "80" in this statement:
if ( s.length() > 80 )
Some folks would call that a magic number. They think that it should be replaced with:
private static final int terminalWidth = 80 ;
...
if ( s.length() > terminalWidth )
so that you know where the "80" comes from. The added bonus is that if you later need to change that value, you can do it in one place.
My experience is that if the same number is used a lot, it's good to put it behind an identifier. But there are lots of times I think that having "magic numbers" makes my code easier to read:
Is 4 really a magic number? Some folks would argue it is. I think "4" better than "charArrayGrowthFactor" which you can look up to see that it's "4".
Originally posted by Ernest Friedman-Hill:
Let's say you've got a million lines of code. In that code, the number 12 appears 143 times. Some of them represent the number of eggs in a dozen. Some of them represent the number of people on a jury. And the balance are the number of ounces of rice per package.
Starting next week, rice comes in 13.5 ounce packages. Fix the code.
Not so easy, is it? Don'tcha wish you'd used DOZEN, JURY_SIZE, and OZ_PER_PKG instead? Now instead of changing the definition of OZ_PER_PKG in one place, you have to look at 143 different places in the code, and figure out which ones to change. Not fun.
12 is a magic number. OZ_PER_PKG is a named constant. Named constants are better.
Originally posted by Tony Morris:
I'm always the one who has to provide proof. I call unfair. Just because it's not in all the text books... OK, I'll stop whinging now.
A definition has not been provided, only some loose talk. Therefore, I'll provide a context. Feel free to clarify it. If it suits your definition, I'll point out why it exceeds scope. I think it should be obvious though.
Originally posted by Ken Blair:
Is 4 really a magic number? Some folks would argue it is. I think "4" better than "charArrayGrowthFactor" which you can look up to see that it's "4".<hr></blockquote>
Is the premise that this is the only place it's used, or is the "growth factor" being used in other places within the class? If it's just there, I agree with you but I think it should be documented there as well. If it's other places, then it might be more appropriate to have it named.
[/qb]<hr></blockquote>
Forgive me, but I don't see how an example of abusing the technique invalidates the technique. Is this not valid:
I know the idea of hardcoding some arbitrary limit into a class like that would probably make your skin crawl, it does mine. However, if you assume as a premise that this class has a maximum number of records and it's appropriate to be hardcoded, is the technique wrong?[/QB]
Tony Morris
Java Q&A (FAQ, Trivia)
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Ken Blair:
I'd definitely agree that in many situations a magic number is not appropriate, although when it's not reused simply documenting it would probably be better than making a variable.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Tony Morris:
that there are zillions of other world views suggests an underlying flaw, don't you think?
if all methods do indeed use that "magic number", then they each have their own implementation details which are not dependant on each other and so should define themselves to their local context, since if they are, then they are in violation of "perfect symbiosis of contractual operations". If you have followed the somewhat simplistic reasoning up to here, and you really care to know what I mean about by last statement, you'll have to wait.
Again, excessive scope. Why does the entire method need to know about x?
What if I wrote some more code after that method call; why should I be exposed to such a thing as 'x'?
I won't go into who excessive scope is implicitly a requirement defect, for brevity, and to prevent pain.
An interesting observation is that I can explain these extremely simple concepts to my undergraduate students who will immediately follow up with meaningful questions and curiosity, yet the claimed "experienced" people (who read all of those books that talk about such things as the feared magic numbers) fail to "get it".
That there is propaganda/marketing guff/bollocks/<preferred-term> at work is merely speculation, but it's my most plausible explanation so far.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss:
Better in which ways?
Originally posted by Tony Morris:
There was a typo in my original example. I didn't "abuse the technique"; I merely asked for a definition, for which that definition does not meet *your* world view (and that there are zillions of other world views suggests an underlying flaw, don't you think?). Let's use *your* definition anyway, since any other is an "abuse", right? Of course.
Originally posted by Tony Morris:
You have exposed a field to excessive scope, assuming that field is not used in all methods and nested classes of that class. However, if all methods do indeed use that "magic number", then they each have their own implementation details which are not dependant on each other and so should define themselves to their local context, since if they are, then they are in violation of "perfect symbiosis of contractual operations". If you have followed the somewhat simplistic reasoning up to here, and you really care to know what I mean about by last statement, you'll have to wait. Otherwise, if it is irrelevant (that you do acknowledge that excessive scope has occurred), then ignore it (since I'm starting to get over it pretty quickly) and move on.
Originally posted by Tony Morris:
Let's use some other example (if I can paraphrase it right):
Again, excessive scope. Why does the entire method need to know about x? Why not just the call to blah(int)? What if I wrote some more code after that method call; why should I be exposed to such a thing as 'x'?
Let's look at a related text book example:
Originally posted by Tony Morris:
All the text books say to use the latter, and not the former (you've read that one haven't you?). The reasoning for this is excessive scope. That is, 'i' is exposed to a scope that exceeds what is required. I won't go into who excessive scope is implicitly a requirement defect, for brevity, and to prevent pain.
Originally posted by Tony Morris:
An interesting observation is that I can explain these extremely simple concepts to my undergraduate students who will immediately follow up with meaningful questions and curiosity, yet the claimed "experienced" people (who read all of those books that talk about such things as the feared magic numbers) fail to "get it".
Originally posted by Tony Morris:
That there is propaganda/marketing guff/bollocks/<preferred-term> at work is merely speculation, but it's my most plausible explanation so far. Of course, I could be totally wrong, and I'm missing some major point, but all the dribble so far is straight from the aforementioned text books, so I'm forced to believe otherwise. Trust me, I love learning; this is not a penis extending exercise.
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
In contrast, I can't recall ever observing problems related to excess scope of a named constant.
This looks like a job for .... legal tender! It says so right in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|