Today I saw something like this:

public class SomeClass {
    public static final int A_CONSTANT = SomeOtherClass.ANOTHER_CONSTANT;

Does this make sense? What is it good for? I wouldn't do it but I am just wondering since I didn't find anything about it

  • It would reduce the amount of code, if the constant is often used. So you have not to refer to SomeOtherClass.ANOTHER_CONSTANT but only to A_CONSTANT. – IQV May 12 '17 at 6:07
  • 1
    @IQV This could also be solved with a static import. – Timothy Truckle May 12 '17 at 6:34
  • 2
    Maybe for some reason both values bust be the same but serve different purpose in both classes so that different names are desired. – Timothy Truckle May 12 '17 at 6:36

Things like this are easier to answer if you don't anonymize away the meaningful names.

public class InfinityStone {
    public static final int MAX_POWER = Integer.MAX_VALUE;

It's really a name change after all. The meaning is the point. Sure, I just want to use a number but I want make my thinking clear each step of the way by using the best name I can come up with. That way when things need to change the programmer that comes after me isn't looking at some weird number wondering why I chose it.

Why not just use Integer.MAX_VALUE everywhere? Because that name has limited meaning to my InfinityStone context. I'm trying to create a place where one model and one domain language dominates. It's distracting to see an implementation detail like Integer.MAX_VALUE scattered about in there.

The longer I spend programming the more convinced I am that the best thing you can do for a design is give things good names.

  • Yup, this is it. Good names with good context are key in so many ways. – Thijs Riezebeek May 14 '17 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.