Comment On Avoiding Magic Constants

Everyone knows that "Magic Numbers" are bad, Bad, BAD! Take, for example, dividing something into quarters. Because including the literal "4" in your code would be a Magic Number, many developers agree that it's best to define a constant named "FOUR", and then use that constant instead. Makes sense, right? [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: Avoiding Magic Constants

2010-08-18 09:02 • by the beholder (unregistered)
public static final int INTEGER_FRIST = 1;

Re: Avoiding Magic Constants

2010-08-18 09:02 • by Kelderek (unregistered)
Good to see that INTEGER_ONE and INTEGER_FIVE are defined twice.
(Just in case)

Re: Avoiding Magic Constants

2010-08-18 09:02 • by COMMENT_FRIST (unregistered)
COMMENT_FRIST = 1;

Re: Avoiding Magic Constants

2010-08-18 09:03 • by The Nerve (unregistered)
Yeah, but why don't they do


public static final Long LONG_VALUE_TEN = Long.valueOf(INTEGER_VALUE_TEN);
public static final Long LONG_VALUE_ZEARO = Long.valueOf(INTEGER_VALUE_ZEARO);

public static final BigDecimal BIGDECIMAL_ZERO = new BigDecimal(INTEGER_VALUE_ZEARO);
public static final BigDecimal BIGDECIMAL_ONE = new BigDecimal(INTEGER_VALUE_ONE);

public static final BigDecimal NEGATIVE_NUMBER_ONE = new BigDecimal(-INTEGER_VALUE_ONE);


Those comparator values are bogus, which is an example why it's ok to use integers sometimes.

Re: Avoiding Magic Constants

2010-08-18 09:05 • by thenextguy (unregistered)
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)

Re: Avoiding Magic Constants

2010-08-18 09:06 • by frits
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.


Re: Avoiding Magic Constants

2010-08-18 09:08 • by The Nerve (unregistered)
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..

Re: Avoiding Magic Constants

2010-08-18 09:11 • by kerberos (unregistered)
TRWTF: public static final String ONE_STRING = "9";

Re: Avoiding Magic Constants

2010-08-18 09:13 • by Matt Westwood (unregistered)
If that;s Java it won't compile because of the repeated INTEGER_FIVE etc.

Re: Avoiding Magic Constants

2010-08-18 09:13 • by MatDey (unregistered)
public static final String ONE_STRING = "9";


lolz

Re: Avoiding Magic Constants

2010-08-18 09:13 • by Severity One
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)
318304 in reply to 318300
kerberos:
TRWTF: public static final String ONE_STRING = "9";


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)
318305 in reply to 318301
Matt Westwood:
If that;s Java it won't compile because of the repeated INTEGER_FIVE etc.


Copy fail.

Re: Avoiding Magic Constants

2010-08-18 09:16 • by The Boss (unregistered)
TRWTF is that this isn't in an XML file.

Re: Avoiding Magic Constants

2010-08-18 09:16 • by Erasmus Darwin
318307 in reply to 318300
kerberos:
TRWTF: public static final String ONE_STRING = "9";


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.

Re: Avoiding Magic Constants

2010-08-18 09:17 • by Someone You Know
Allen's colleague:

public static final int COMPARE_RESULT_ZERO = 0 ;
public static final int COMPARE_RESULT_ONE = 1 ;
public static final int COMPARE_RESULT_NEGATIVE = -1 ;


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".

Re: Avoiding Magic Constants

2010-08-18 09:19 • by Anonymous (unregistered)
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)
318313 in reply to 318307
Erasmus Darwin:
kerberos:
TRWTF: public static final String ONE_STRING = "9";


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.


TRWTF is that Java has no way to change constant values dynamically.

Re: Avoiding Magic Constants

2010-08-18 09:20 • by Paula (unregistered)
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
318315 in reply to 318296
thenextguy:
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)

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)
318316 in reply to 318307
Erasmus Darwin:
kerberos:
TRWTF: public static final String ONE_STRING = "9";


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.
And he's also prepared to face possible issues if oracle ever decides to change the lenght of literals 1 or 10

Re: Avoiding Magic Constants

2010-08-18 09:24 • by Ethan Qix (unregistered)
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)
318319 in reply to 318292
the beholder:
public static final int INTEGER_FRIST = 1;


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)
318320 in reply to 318296
thenextguy:
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)


Why don't u write malloc(4 * sizeof(int) + 1) instead?

Re: Avoiding Magic Constants

2010-08-18 09:27 • by guest (unregistered)
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

Re: Avoiding Magic Constants

2010-08-18 09:27 • by powerlord
318322 in reply to 318299
COHERENCE_NOT_FOUND:
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..


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.

Re: Avoiding Magic Constants

2010-08-18 09:29 • by SR (unregistered)
system.out.println(Constants.INTEGER_ZEARO + "th");

Re: Avoiding Magic Constants

2010-08-18 09:33 • by Thief^
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.

Re: Avoiding Magic Constants

2010-08-18 09:34 • by pallen (unregistered)
ALPHALET_L ALPHALET_O ALPHALET_L

(I'm not spam!)

Re: Avoiding Magic Constants

2010-08-18 09:35 • by vtcodger (unregistered)
***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)
318328 in reply to 318322
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)
318331 in reply to 318319
argh:
the beholder:
public static final int INTEGER_FRIST = 1;


Don't u guys ever sleep? Perhaps a new joke next time at least? Is it too much to ask?


Apprently it is.

Re: Avoiding Magic Constants

2010-08-18 09:40 • by bertram
318332 in reply to 318315
Steve The Cynic:
thenextguy:
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)

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.


Sigh. Sense of humour fail.

Re: Avoiding Magic Constants

2010-08-18 09:41 • by toth
Way, way back in the yearo ZEARO...

Re: Avoiding Magic Constants

2010-08-18 09:46 • by OhHi (unregistered)
isFileNotFound(TRWTF_ZEARO) = 1;

Re: Avoiding Magic Constants

2010-08-18 09:49 • by The MAZZTer
318335 in reply to 318294
COMMENT_FRIST:
COMMENT_FRIST = 1;


Assertion fail!

Re: Avoiding Magic Constants

2010-08-18 09:50 • by Seol (unregistered)
318336 in reply to 318322
powerlord:
COHERENCE_NOT_FOUND:
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..

On another track, Java already has java.math.BigDecimal.ZERO and java.math.BigDecimal.ONE as well.
Well, yeah, it's sensible to have static frequently-used values for immutable classes to save having to repeatedly create new objects.

Re: Avoiding Magic Constants

2010-08-18 09:54 • by JayC (unregistered)
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.

Re: Avoiding Magic Constants

2010-08-18 10:00 • by SDR (unregistered)
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)

Re: Avoiding Magic Constants

2010-08-18 10:06 • by dkf
318341 in reply to 318338
Billy The Squid:
I want to see the code when they have to change INTEGER_FOUR to have a value of 5 due to some management decision...
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)
318342 in reply to 318313
Larry:


TRWTF is that Java has no way to change constant values dynamically.


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)
318343 in reply to 318314
Paula:
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


"That is brillant!" ??? Error: Won't compile!

Re: Avoiding Magic Constants

2010-08-18 10:17 • by noobtuber (unregistered)
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)
318345 in reply to 318308
Someone You Know:
Allen's colleague:

public static final int COMPARE_RESULT_ZERO = 0 ;
public static final int COMPARE_RESULT_ONE = 1 ;
public static final int COMPARE_RESULT_NEGATIVE = -1 ;


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".


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)
318347 in reply to 318339
SDR:
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


I sincerely hope your use of "literally" is literally incorrect.

Re: Avoiding Magic Constants

2010-08-18 10:27 • by Smitty
318348 in reply to 318337
JayC:
TRWTF is that this is actually an attempt to make Java Text-To-Speach friendly.


I hope you're trying to be ironic...

Re: Avoiding Magic Constants

2010-08-18 10:28 • by Andreas (unregistered)
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.
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment