I'm writing a method that forms part of the public interface of a Java class. It broadly allows the caller to specify the values to assign to a number of database entities - so they must supply both the IDs of the entities themselves, and the values to assign to them.
I'm wavering between implementing this as a List<Pair<Integer, Integer>>
or just two List<Integer>
arguments. Both would obviously work, and neither would cause any implementation or efficiency problems within my method. It's basically the same information in any case (a 2xn array), just striped differently.
So I'd like some opinions on which one you think would be better, and why.
Advantages I see so far for the list of pairs:
- More accurately reflects the actual relationship between the entities
- Eliminates some classes of dynamic error (e.g. mismatched list lengths)
Advantages for the pair of lists:
- Does not rely on any non-JDK classes (simple as
Pair
is to grasp as a concept) - Does not require construction of any auxiliary objects just to carry the data around
- The callers are more likely to have the arguments in separate lists anyway, so they don't need to realign the data before calling the method
Both cases have identical type-safety, as well as the same possibility for argument mismatch (e.g. entering values first and IDs second when it should be the other way around). This latter problem can be avoided by creating a trivial wrapper around Integer
called something like PrimaryKey
, which has its own pros and cons and is orthogonal to this issue anyway as this can be used just as well in both cases.
However there's a middle ground that could feasibly be a third option - a trivial container class with integer fields for objectId and value. This doesn't enlist the compiler's help in ensuring the objects are correct through typing, but it does provide an extra layer of security in the assignments. I don't think I'd go for this, though, as I don't like the idea of polluting a public interface with a trivial class like this.