- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Aaah, you kids and your crazy classes! When I was your age, anybody who'd ever touched BASIC (and none of that "Visual" foolishness, I mean real BASIC) knew instinctively that the value of pi was 4*ATN(1), good to whatever precision you stored it in.
Admin
I totally disagree. There is very little at best advantage of using a constant like NUMBER_FOUR instead of just 4. Numbers and constants like these should always be avoided and the constant should say something like NUMBER_OF_CARDS_IN_HAND.
Admin
Suggested reading: http://xkcd.com/725/ :-)
Admin
Good thing we have an authoritative opinion on that now. Now I realize my spontaneous chuckling every time I read a Bert Glanstron retread is due to something other than its funniness.
Admin
I think you are doint it wrong. What if the array you are allocating changes from int to double in the future ? there is a risk you forget to change the 4. Prefer using sizeof(your_variable) instead of the numbers. Just a suggestion..
Admin
Admin
Admin
public static final String PAULA_BEAN = "Brilliant";
Admin
Wow.
Admin
Say... This would be handy when I localize to countries where 1 = 2...
Admin
Admin
You need a better sarcasm detector dude. There is not a single non-sarcastic sentence in the entire article.
Admin
See, what they should have done is this:
Admin
I have found a practical use for defining constants for much-used integer values such as 0 and 1 in embedded systems. In a system with 32 kbytes of ROM space I simply could not afford to allow the C compiler to litter it with numerous 32-bit zeroes and ones. Declaring
and using them everywhere I could saved me about 2 kbytes of ROM.
OTOH, I doubt today's CodeSOD has any such practical purpose.
Admin
Peter, Andrew, James, and John, Come and follow me!
Admin
Remove the comment and use:
malloc( 4 * sizeof(int) + sizeof(char) )
Now the expression and comment do not need to be maintained in step if anything needs to change. A comment if added should explain why you need 4 ints and a char worth of storage, else it only gets in the way.
Admin
public static final int COMPARE_RESULT_EQUAL = 0 ; public static final int COMPARE_RESULT_GREATER = 1 ; public static final int COMPARE_RESULT_LESSER = -1 ;
If you are going to create a constant, use the real meaning!
In agreement with other posters that this still does not help in dealing with the results a Comparable method. Even if it was your own object, it would be a bad idea to assume these are the only possible return values. -2 means less than just like -1. Maybe some helper methods?
class CompareUtil { static boolean isLessThan(int compareToReturnValue) { return compareToReturnValue < 0; } }
if (CompareUtil.isLessThan(a.compareTo(b))) ... same as if (a.compareTo(b) < 0)
I don't see any real readability benefit...
Hopefully the compiler will inline everything for you!
Admin
Any reasonable compiler should produce the same output for either case... it would only be slower during compilation, so you should always feel free to go with what you feel is cleaner when doing arithmetic with consts.
Which should also include these if the compiler is any good.
Admin
Magic constants aren't that bad. Physicists use them all the time (Graviational constant, speed of light, etc).
Admin
Oh, now I see what happened here. The guy who wrote the constants class was not writing the constants to be used. He was simply (as a scientist) documenting what the constants were. So, in his inspection of the code and the Java Language Specification, he discovered that FOUR was indeed 4, and that INTEGER_FOUR is actually 4.
Good job! And thanks for showing us why this isn't a WTF!!!
Admin
Sure. That way if one of the places that needs a constant 5 is changed to need, say, a 6, we can just redefine one of the INTEGER_FIVE's to INTEGER_FIVE=6, while leaving the other unchanged, so we don't break the other code.
Admin
Human sees:
God wrote:
Admin
Java has set precision for all its types across all platforms. That way the source doesn't need to be compiled to byte code for each platform.
Admin
I recall when I was a kid, my math teacher explaining that pi cannot be exactly expressed by any fraction or finite-length string of decimals. He conluded by saying, "The only way to express the value of pi exactly is to say 'pi'."
Which is not quite true -- you could give an algorithm for calculating it -- but you get the idea.
Admin
now it is.
The most important apps in most companies are written in COBOL (mainly your payroll app).
Admin
Yes, I have fond memories of tracking down a bug caused by exactly this in a Fortran program 20-some years ago. Somebody inadvertantly redefined the constant "1" to be "2". Of course the problem didn't show up until many lines later in the code. And so I was doing dumps going, "Okay, so now X=3, we add 1 to it ... and how did it end up being 5? I must have missed another update ..."
Admin
Using,
malloc(4 * sizeof(int) + sizeof(void *));
might indicate a NULL terminator, whereas,
malloc(4 * sizeof(int) + 1);
is more likely to indicate a nul terminator.
Admin
This is why you need to use constants line ONE, TWO, etc. You never known when someone is going to redefine the integers to be multiples of arctan(1), just so that they can set PI equal to 4 and be exactly correct.
Admin
Crap, that broke this fix (again):
Admin
beautiful
Admin
Admin
This is intriguing, how could you have \0 to be a terminator of an array of ints? How would the code that handles that look like? How would you store values larger than 2^24 and divisible by 256?
Admin
By proving my code to be not portable (at least I fail at i18n), you have also brought to my attention the reason why English part of the word can not grasp the concepts of Metric System. It must be very difficult when you have 8 fingers.
Admin
My favorite part is how most of the readers here don't understand Java enough to make informed comments on CodeSODs that contain them (I think that goes for the admins too).
Admin
No, we're up late at night, learning to spell the word 'you'.
Admin
Hmmmmm.
public static final int MEANING_OF_LIFE = 42;
Admin
Remember you're dealing with pure retardation there...
Admin
+1024
Admin
This is hardly new...
The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 at every appearance, the variable PI can be given that value with a DATA statement and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of pi change.
-- Early FORTRAN manual for Xerox Computers -- http://en.wikiquote.org/wiki/Fortran
Admin
Wow man, you just strawmanned into the void. Nobody claimed this to be new...just crap.
Admin
FTFY
Admin
That sentence is not sarcastic, it's entirely true. Using FOUR is not really any better than using the integer 4.
Admin
Java does have a way of doing this. It's called reflection.
Thanks to some particularly evil use (as an experiment, I was proving a point and destroyed the code soon after) I managed to get
to output the string "Goodbye All!".
Admin
Without modifying the JVM, I can pretty much guarantee that you cannot set 3 = 4 application-wide as demonstrated in the FORTRAN example above.
Admin
This would be pretty easy to do without reflection. System has a
method, which would allow you to set it with your own PrintStream class. You could use Proxy (class) to create a Proxy (design pattern) that pointed back to , but it's no more elegant than the hard-coded solution, and I doubt would meet Larry's definition.In other words: not a dynamically-defined constant: dynamically-modified static source code.
Admin
It gets WORSE. On a recent project (that is still actively being developed) I worked on, they are using Fortran. The original program dates back to the 70's or so, and they thought that this was the case even in Fortran 66 (that is what they originally used). There was a common block specifically setup to have all sorts of constants that could be globally referenced. Among these were such things as 0.0, 1.0, PI, .5 and all that. It was thought that this would reduce the code size and be more "efficient". The problem is that this is a false assumption, as it took MORE code to reference these constants that it saved in having them in a single place. It was a BIG FALSE ECONOMY. Some original programmer (back over 30 years or so) never took the time to see the difference, and crazy follow on programmers continued the silly practice. I for one made an effort to personally remove these when I saw them.
Lord help us if the value of PI gets changed to 3, and all the planes fall out of the sky (that is where the program is used).
Ph, and they thought it would be more efficient to do their own number formatting as well, another waste of time and resources.
Admin
What you're all discussing here are MANIFEST constants.
And they ARE very useful things. Much of my software uses them.
BUT they need to be named for what they represent, and NEVER for their value!
For example: MIN_SONG_LENGTH_SECONDS = 67; and NOT SIXTY_SEVEN = 67; (!!!)
Admin
My eyes! The goggles do nothing!
Admin
I really liked the 'magic' numbers I read about recently used in 'Montgomery' division
like 1431655766 and 613566757 (assuming ints are 32 bit, and longs are 64)
(((int)x*(long)143655766)>>32) == x/3 (((int)x*(long)613566757)>>32) == x/7
Many compilers make this optimization automatically when dividing int by other contants ints. I used it manually once for a small performance gain in a program that had to divide ints by other ints in a narrow range (3,4 or 5)
Admin
I am amused and appalled at this programmer enthusiastically adopting the principle of magic numbers while completely failing to understand the concept.