3

Unchangeable, constant values should be stored in constants rather than variables for both safer and cleaner code.

The latter doesn't apply to all cases of unchangeable values though: There's the following method that is only called once, on initialising the app that uses the same value of a String twice. The String is only referenced and used inside the method.

My question is: What's the best way of variable/constant definition? Being a simple String in a large application, performance and memory can be neglected, it's more about readability and maintenance.

Is it as variable inside the method:

protected void init() {
    final String thestring = "thevalue";

    methodA(thestring);
    methodB(thestring);
}

or is it as constant on class level (although only used in the method):

private static final String THESTRING = "thevalue";

protected void init() {

    methodA(THESTRING);
    methodB(THESTRING);
}

or a third, better solution? Please also take into consideration that there can be more, similar methods in the same class.

msp
  • 3,272
  • 7
  • 37
  • 49
  • for maintainability one would rather keep theValue in a configuration/property file. – Shailesh Aswal Jul 03 '14 at 11:06
  • Why not define a new `ENUM` with this string and use it? – TheLostMind Jul 03 '14 at 11:08
  • "Which is best?", in situations like this, is very much in the eye of the beholder. In many cases you should just use the literal string inline in your code and not define a "variable" to hold it. In other cases what you do is pretty well defined by translation concerns. And if the variable needs to be easily modified (eg, it's a URL or some such) then placing it at the top makes it easier to find, even if only used in one place. – Hot Licks Jul 03 '14 at 11:34

3 Answers3

3

For me the best solution is to use variable inside the method - because it's internal variable. So other methods shouldn't see it. Consider the encapsulation and clean code, when you try to move this variable on class level you will get a long list of class variables.

Another thing is memory. After method is executed the variables are destroyed. When you define it as a static it will be in your memory all the time.

KirkoR
  • 788
  • 6
  • 13
  • 2
    Usually constants are shared across multiple instances of a class, so they should be at class level..It doesn't make sense to have a separate `PI` object for each instance of MATH. – TheLostMind Jul 03 '14 at 11:17
  • But PI is public variable, used by many others. In above example those variables will be used only in one method. – KirkoR Jul 03 '14 at 11:22
  • I guess @kajax answer clears your doubt... Keeping that value at class level inceases both extensibility and readability. – TheLostMind Jul 03 '14 at 11:34
  • In my opinion, in this case "extensibility and readability" is not and argument. It's still final variable used internally for one method. Readability is the same, in this case Extensibility is over programming. – KirkoR Jul 03 '14 at 11:49
1

I can think of three places to put your variable (all final ofc), each has it advantages and disadvantages.

  1. Local variable.
  2. Private static field inside your class.
  3. Public static field inside some Properties class.

1 - Advantages: variable can only be seen inside your method - high code safety. Disadvatages: variable is buried inside a method, can be difficult to find and change.

(I'll skip 2 because it is just compromise between 1 and 3)

3 - Advantages: your field is among other configurable fields, that makes it easy to change your setting. Disadvantages: field is public and everyone can see it (but String is immutable so no one will be able to change it).

Summary: depends on how much you expect you will need to change your variable (e.g. balancing, color changing, ...). If you are sure that this string value is the right one, i wouldn't fear to put that into local variable.

kajacx
  • 12,361
  • 5
  • 43
  • 70
0

Typically constants are not instance specific. It is thus a better practice to store constants as static variables rather than as member variables. The advantages are:

  • There is only one allocation of the variable instead of one allocation per object.
  • You don't need to create an instance variable to access a constant, e.g. PI is declared to be static in the java Math class.
Debasis
  • 3,680
  • 1
  • 20
  • 23
  • Doesn't really apply in this case, because as written in the question, the method is only called once. So there's only one allocation in both cases. Plus when declared as static the memory wouldn't be released. – msp Jul 03 '14 at 11:53