Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to protect myself from parameter misplacement

Tags:

java

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?

like image 959
esin88 Avatar asked Jan 04 '23 19:01

esin88


1 Answers

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.

like image 63
Nikolas Avatar answered Jan 15 '23 19:01

Nikolas