I have some code that I want to refactor. I have lots of methods that take multiple arguments of the same type, for example:
public void foo(String name, String street, boolean b1, boolean b2) { ... }
and so on. Because the different objects can only be distinguished by name I would like to wrap them in Objects (Enums) so I can make use of the typesystem of the language (Java in this case).
public class Name {
private String value;
public String getValue() { return value; }
// ...
}
Like this I could force calling code to pass in Objects of a certain type. That would assure that it would not accidentally mix up the order of the method parameters and thus not produce unexpected behaviour at runtime:
foo(new Name("John"), new Street("Broadway"), new B1(true), new B2(false);
This makes refactoring a lot safer, you can carry the object through the system as long as you want, the data inside it, the string is safe at all times. Only when you need it, you get it by calling getValue().
Now, for the Objects that wrap strings, it is pretty straightforward, as there are lots of states instances can be in.
But how about the boolean wrappers? These are either TRUE or FALSE. The implementation just looks, well, a bit funny:
public enum Quanto {
YES() {
protected boolean isQuanto() {
return true;
}
},
NO() {
protected boolean isQuanto() {
return false;
}
};
protected abstract boolean isQuanto();
}
Even stranger I find what the calling code looks like:
public void doStuff(Quanto quanto) {
if(quanto.isQuanto()) {
// ...
}
}
Technically it does not matter of course, but it just doesn't feel right... Have you found "better" ways of dealing with this?
EDIT: What also displeases me is the fact that there are more values thinkable than YES and NO in the above example, say MAYBE...?!
Thanks!