I've often run through a validation pattern where, to be valid, some variables must contain one of a prefixed number of values.
PSEUDO CODE:
IF x == CONSTANT_1 || X == CONSTANT_2 || ... || x == CONSTANT_N
THEN X is valid
In order to avoid the chain of OR terms, I created a static final unmodifiable set, which contains all the constants:
public final static String CONSTANT_1 = *value* ;
public final static String CONSTANT_2 = *value* ;
...
public final static String CONSTANT_N = *value* ;
public final static Set SET_OF_CONSTANTS = Collections.unmodifiableSet(new HashSet(){
private static final long serialVersionUID = 1L;
{
add(CONSTANT_1);
add(CONSTANT_2);
...
add(CONSTANT_3);
}
});
And I perform the check in the following way:
if(!SET_OF_CONSTANTS.contains(x)){
//X NOT VALID
}
I'd like to know if this is a good programming practice, if there are any alternatives, and if it's true that using a Hash Table query (O(1) in theory) instead of the OR terms-chain improves performance and maybe also code-readability.
To make any variable a constant, we must use 'static' and 'final' modifiers in the following manner: Syntax to assign a constant value in java: static final datatype identifier_name = constant; The static modifier causes the variable to be available without an instance of it's defining class being loaded.
The static keyword means the value is the same for every instance of the class. The final keyword means once the variable is assigned a value it can never be changed. The combination of static final in Java is how to create a constant value.
public makes it accessible across other classes. static makes it uniform value across all the class instances. final makes it non-modifiable value. So basically it's a "constant" value which is same across all the class instances and which cannot be modified.
Making anything "private" means it is only available from within the class it was defined, "static" makes that variable available from ANYWHERE in that class, and "final" does not allow that variable to be changed, adding the modifier "final" changes your "variable" to a "constant" due to it's constant value instead of ...
Overall, I think this is very good style.
There's not a huge difference, but I'd personally define SET_OF_CONSTANTS
like so:
public final static String CONSTANT_1 = "*value*";
public final static String CONSTANT_2 = "*value*";
...
public final static String CONSTANT_N = "*value*";
public final static Set<String> SET_OF_CONSTANTS = Collections.unmodifiableSet(
new HashSet<String>(Arrays.asList(
CONSTANT_1,
CONSTANT_2,
...
CONSTANT_N
)));
It's not entirely clear to me whether you even need the separate CONSTANT_1
constants, or whether you can simply fold the values into SET_OF_CONSTANTS
.
As far as performance goes, I would not start optimizing anything until I've profiled the code on real data.
Finally, note that when x
is a string, the following is probably incorrect:
IF x == CONSTANT_1 || x == CONSTANT_2 || ... || x == CONSTANT_N
Here, the ==
should probably be replaced with calls to equals()
.
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