| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Next » |
|
public static final int INTEGER_FRIST = 1;
|
|
Good to see that INTEGER_ONE and INTEGER_FIVE are defined twice.
(Just in case) |
|
COMMENT_FRIST = 1;
|
|
Yeah, but why don't they do
Those comparator values are bogus, which is an example why it's ok to use integers sometimes. |
|
Auch, that hurts!
I use magic numbers all the time, I just make sure there's a comment line next to it explaining them. e.g. malloc(17) // 4*4(int) + 1 (\0) |
|
Now, if 1 turned up to be 9,
I don't mind, I don't mind. If all the hippies cut off their hair, I don't care, I don't care. Dig, 'cause I got my own world to live through And I ain't gonna copy you. |
|
ONE == INTEGER_VALUE_ONE == PLUS_ONE == COMPARE_RESULT_ONE
This is a perfect example of my new favorite design pattern: Cargo Cult. |
Re: Avoiding Magic Constants
2010-08-18 09:09
•
by
COHERENCE_NOT_FOUND
(unregistered)
|
|
public static final float FLOAT_NOT_QUITE_PI = (LONG_VALUE_TEN + LENGTH_TWELVE) / (NINE_STRING + (COMPARE_RESULT_NEGATIVE * (PLUS_ONE + BIGDECIMAL_ONE) ));
mmmmmm... FLOAT_NOT_QUITE_PI.. |
|
TRWTF: public static final String ONE_STRING = "9";
|
|
If that;s Java it won't compile because of the repeated INTEGER_FIVE etc.
|
|
public static final String ONE_STRING = "9";
lolz |
|
This is for future compatibility. If Java will ever drop primitive types, this guy is prepared.
|
Re: Avoiding Magic Constants
2010-08-18 09:13
•
by
by
(unregistered)
|
Or maybe that's the beauty of the Constants class. If you want 5 to universally be 6, all you have to do is change a value in Constants! |
Re: Avoiding Magic Constants
2010-08-18 09:15
•
by
by
(unregistered)
|
Copy fail. |
|
TRWTF is that this isn't in an XML file.
|
Re: Avoiding Magic Constants
2010-08-18 09:16
•
by
Erasmus Darwin
|
On the contrary, that shows the real flexibility of using constants. He was able to change ONE_STRING from "1" to "9" without having to dig through countless lines of source code. |
It seems likely these are being used somewhere in conjunction with a Comparable or Comparator. If that's the case there's another WTF here in assuming that compareTo() methods will return -1 for "less than" and +1 for "greater than". The only assumption you're allowed to make is that it returns a negative value for "less than" and a positive value for "greater than". |
|
Ahh, I see the WTF - what if they want a short or a float? Clearly this developer didn't go far enough with their implementation. Kudos for having constants specifically defined for comparison though - you just never know when they'll redefine the return values on comparator methods. Last time I updated my distribution I found that 17 was greater-than, 43 was less-than and -4 was equality. If only I'd had the foresight to use constant values instead of inline integers.
|
Re: Avoiding Magic Constants
2010-08-18 09:20
•
by
Larry
(unregistered)
|
TRWTF is that Java has no way to change constant values dynamically. |
|
UPPERCASE_T_STRING + LOWERCASE_H_STRING + LOWERCASE_A_STRING + LOWERCASE_T_STRING + ONE_SPACE_STRING + LOWERCASE_I_STRING + LOWERCASE_S_STRING + ONE_SPACE_STRING + LOWERCASE_B_STRING + LOWERCASE_R_STRING + LOWERCASE_I_STRING + LOWERCASE_L_STRING + LOWERCASE_L_STRING + LOWERCASE_A_STRING + LOWERCASE_N_STRING + LOWERCASE_T_STRING + EXCLAMATION_MARK_STRING
|
Re: Avoiding Magic Constants
2010-08-18 09:21
•
by
Steve The Cynic
|
Tsk. The 4*4(int) is presumably to indicate that you want 4 lots of 4-byte ints, right? But ints aren't 4 bytes (well, not necessarily, anyway: I once worked on a system where it would have been reasonable for CHAR_BITS == INT_BITS == 16, and plenty of systems a bit later with sizeof(int) == 2). Also, this is just duplicating the magicness in the comments: what is the first 4? And why not this? malloc( 4*sizeof(int)+1 ); // one int per season of year, 1 byte for trailer Now the comment explains what the magic number means. |
Re: Avoiding Magic Constants
2010-08-18 09:23
•
by
the beholder
(unregistered)
|
And he's also prepared to face possible issues if oracle ever decides to change the lenght of literals 1 or 10 |
|
I think the last line was the funniest :
public static final String ONE_STRING = "9"; |
Re: Avoiding Magic Constants
2010-08-18 09:24
•
by
argh
(unregistered)
|
Don't u guys ever sleep? Perhaps a new joke next time at least? Is it too much to ask? |
Re: Avoiding Magic Constants
2010-08-18 09:27
•
by
argh
(unregistered)
|
Why don't u write malloc(4 * sizeof(int) + 1) instead? |
|
public static final Long LONG_VALUE_TEN = Long.valueOf(10);
public static final Long LONG_VALUE_ZEARO = Long.valueOf(0); Guy apparently needs a spell check as well as a programming lesson |
Note to spoil your joke or anything... but Java already has a double not quite pi constant: java.lang.Math.PI On another track, Java already has java.math.BigDecimal.ZERO and java.math.BigDecimal.ONE as well. |
|
system.out.println(Constants.INTEGER_ZEARO + "th");
|
|
The Long and BigDecimal versions may make sense, if the java implementation is stupid enough to assume a constant number is an int and does a cast every time you assign a constant to a Long or BigDecimal.
|
|
ALPHALET_L ALPHALET_O ALPHALET_L
(I'm not spam!) |
|
***TRWTF is that this isn't in an XML file***
Well, maybe. If 1 really equals 9 when XML is used, that might explain some problems I've been encountering. |
Re: Avoiding Magic Constants
2010-08-18 09:35
•
by
wtf
(unregistered)
|
|
I see the problem.
public static final int INTEGER_FOUR = 4; should have been public static final int INTEGER_FOUR = FOUR; That's it, right? Fixed. |
Re: Avoiding Magic Constants
2010-08-18 09:40
•
by
Bill's kid
(unregistered)
|
Apprently it is. |
Sigh. Sense of humour fail. |
|
Way, way back in the yearo ZEARO...
|
|
isFileNotFound(TRWTF_ZEARO) = 1;
|
Re: Avoiding Magic Constants
2010-08-18 09:49
•
by
The MAZZTer
|
Assertion fail! |
Re: Avoiding Magic Constants
2010-08-18 09:50
•
by
Seol
(unregistered)
|
Well, yeah, it's sensible to have static frequently-used values for immutable classes to save having to repeatedly create new objects. |
|
TRWTF is that this is actually an attempt to make Java Text-To-Speach friendly.
|
Re: Avoiding Magic Constants
2010-08-18 09:57
•
by
Billy The Squid
(unregistered)
|
|
There are times when people mock me for not being able to code in C, C++, or Java - I'm a PHP developer (NO, stop screaming, that which I write is non WTF worthy - I fix WTF level code more often than not, have a severe hatred of anyone who's looked at "PHP for dummies" and thought "Hey, I've heard of PHP").... I can understand C++, Java, have written in Assembler, RPG, and Cobol in the past (RPG Cobol itself is a WTF), yet I code in PHP for the fun of it.
But this... This is evil. This code is wrong. This is beyond "magic numbers", and is just laughable. But better yet: I want to see the code when they have to change INTEGER_FOUR to have a value of 5 due to some management decision... I want to see STRING_FOUR have a value of "12". Think of it. And then laugh nervously with me. |
|
I have been quietly reading, enjoying, and sharing the wft for years. Today I am oddly compelled to comment. This one is literally melting my brain.
OMFGWTF |
Re: Avoiding Magic Constants
2010-08-18 10:05
•
by
Pretty Mad Developer
(unregistered)
|
|
This kind of code often comes from having the "magic constant" rule enabled in PMD, and a "zero PMD warning" policy. This proves, if anything, that blindly applied rules are almost always stupid.
TRWTF is, what do you do if Oracle suddenly decides to change Java keywords? That's where C got it right with preprocessor directives: #define IF_KEYWORD if (captcha: "odio" -> "hatred" in spanish) |
Not 5, but 5.1 (i.e., a floating-point value). The level of confusion caused by that will be truly a matter to cheer the soul…of Satan and his minions. |
Re: Avoiding Magic Constants
2010-08-18 10:07
•
by
NoAstronomer
(unregistered)
|
LMAO That had me spraying coffee over my keyboard. Should be a featured comment. If only more languages had the ability to change constants on the fly. It's the way of the future. |
Re: Avoiding Magic Constants
2010-08-18 10:10
•
by
LennyPain
(unregistered)
|
"That is brillant!" ??? Error: Won't compile! |
|
public static final int FOUR = 4;
public static final int INTEGER_FOUR = 4; ah now it makes sense |
Re: Avoiding Magic Constants
2010-08-18 10:19
•
by
Matt Westwood
(unregistered)
|
Oh wake up. You just check the return value as greater-than-or-equal to the ONE value and less-than-or-equal-to the "NEGATIVE" value. Cor I dunno, do I have to do ALL your design for you. |
Re: Avoiding Magic Constants
2010-08-18 10:24
•
by
Matt Westwood
(unregistered)
|
I sincerely hope your use of "literally" is literally incorrect. |
I hope you're trying to be ironic... |
|
What is so bad about magic numbers?
There are definitely _some_ areas, namely all numbers that are used in more than one place. But I have seen more code where the developer did too much, than I have seen code where magic numbers were a bad thing. Some examples? Replace boolean-parameters with named constants like: DO_THIS = true; DONT_DO_THIS = false; The method-call read: foo(DO_THIS) and foo(DONT_DO_THIS) This does not help reading! Or the guy converting all assertion-messages to string-constants! Did not help the reading either. Magic numbers are way overrated (I guess because it is so easy to check if someone uses them). You should use your brain instead of converting everything into a constant. |
| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Next » |