I just saw a class (an big API module), where there is alot of stuff like
readParamString(Object params, int index)
and they have in that class 10 fields
final static int PARAM1 = 1
...
final static int PARAM10 = 10
which are being used with readParam functions
but you can also use a normal int like readParamString(o, 1);
is final static int somehow better than using a normal int ?
One advantage of declaring a static final constant is to prevent multiple references to the same literal value througout the code, which could potentially lead to unwanted variation; e.g. if a programmer inadvertently changes one literal without changing the others.
In your example, I'd question the value of declaring a constant PARAM1 with the value 1, especially if this is private to the containing class and only referenced once. If however, there was some semantic meaning to the literal value then it might be more useful; e.g.
public static final int LOAD_ACTION = 1;
public static final int SAVE_ACTION = 2;
public static final int DELETE_ACTION = 4;
public static final int MOVE_ACTION = 8;
However, typically an enum would be used instead of this approach.
That's silly.
The reason for extracting numeric constants is to give them a telling name. PARAM1..PARAM10 are not very telling.
In the example you give, I'd say that having the named values is not better than just using the int directly. The parameter is a count, so the int representation of the parameter is just as descriptive as the named version.
This seems to be kind of like when I occasionally come across C code that has things like
#define ZERO 0
#define ONE 1
There's really not much point. In fact, when I see those names used (instead of when I'm looking at the definitions), I get tempted to jump to the defines to make sure the names aren't lying (It's been a long, long time, but, I've actually come across code that had FALSE defined to 1, -1 or something non-zero, for example, which is really, really bad).
Edit: on second look, it appears that the names might indicate which field in the first parameter that are of interest, in which case the names might make more sense - as long as the names have some meaningful tie to the field rather than just the numeric offset. It's hard to say whether or not that's the case, since you might be paraphrasing the code you're really working with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With