Let's say i have this important method:
int generateId(int clientCode, int dataVersion) {
return clientCode * 2 + dataVersion % 2;
}
Both parameters are int
, so it's pretty easy to invoke this method with misplaced parameters, like generateId(dataVersion, clientCode)
. It will be successfully compiled and executed. But generated id will be completely wrong, which can cause severe problems.
So, my question is: is there a way to protect myself from such parameter misplacement?
For now I could only think of changing it to int
wrapper classes like:
int generateId(@Nonnull ClientCode clientCode, @Nonnull Version version) {
return clientCode.getValue() * 2 + version.getValue() % 2;
}
static class IntWrapper<T> {
private final int value;
IntWrapper(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
static class ClientCode extends IntWrapper<ClientCode> {
ClientCode(int value) {
super(value);
}
}
static class Version extends IntWrapper<Version> {
Version(int value) {
super(value);
}
}
and invoking it with generateId(new ClientCode(clientCode), new Version(version))
. Of course, it doesn't guarantee full protection, but at least it's more transparent this way.
Is there any better / other way?
Consider to configure your Object via Method-Chaining like so
configure().withDataVersion(dataVersion).withClientCode(clientCode).generateId();
While it makes the configuration far more verbose it is also clearer to read.
The DataVersion and ClientCode-Information could be moved into an inner class. configure()
initiates the inner class, the withDataVersion(int)
and withClientCode(int)
are basically setters. The generateId()
will build and return your Id like it does today.
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