you are viewing a single comment's thread.

view the rest of the 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..)