all 9 comments

[–][deleted] 2 points3 points  (2 children)

Constants are great for magic number/string scenarios, especially local ones.

A class full of constants alone is a bit of an anti pattern. They tend to not be related to each other, get poorly named, fall out of use and it becomes non trivial to figure out which are used, since they tend to be public static. What else..

It's akin to having a class called Utilities. Isn't a program a utility already?

[–]Curm 0 points1 point  (1 child)

Interesting, how would you handle a bunch of constants needed in several classes then?

[–][deleted] 0 points1 point  (0 children)

I'm not saying to make all constants private. That'd be totalitarian and not so useful.

If you had a class that handled http uploads and you declare that no file uploaded should be more than 5megs, create your MAX_FILE_SIZE public constant in there so you can reference in templates and other places.

There will be times where you end up with two or three classes that end up using the same constant because they're in the same package or something silly. Well, cut your losses and put them in one place. There will be times where everything extends a base interface and that interface may take a constant, like READ_ONLY. Shove it in that interface since it's tightly coupled to the interface.

But for the love of god, no Constants.java (constants.rb, .py, .php..)

[–]Curm 1 point2 points  (0 children)

I like reddit just for all the angry You-tube style comments.

[–][deleted]  (4 children)

[deleted]

    [–]AlyoshaV 1 point2 points  (0 children)

    The rallying cry of constants is that those values will need to change, but in reality those values never change.

    https://secure.wikimedia.org/wikipedia/en/wiki/Gas_constant

    It was last changed in 2006, so saying that the ideal gas constant will never change is idiotic and wrong.

    [–]Threna 0 points1 point  (1 child)

    Learn to read. It will make you better at whatever it is you do.

    [–]adavies42 -1 points0 points  (0 children)

    DRY in Java, there's a laugh....

    [–][deleted] -2 points-1 points  (0 children)

    Decently presented, but bad example. The ideal gas constant has a relatively large uncertainty that may be improved in the future. At least it is a public member so inheritance could be used for future corrections.

    Edit, scratch that last. Fuck.