This code works fine, but I need to simplify it for more clearness and hopefully more efficiency:
int i = 0;
if (p.cap()) n++;
if (p.creditcard()) n++;
if (p.email()) n++;
[...]
if (p.price()) n++;
if (p.url()) n++;
if (p.zip()) n++;
if (n == 0) p.standard();
As the code says, I need to call multiple methods (I don't know the finite number of them). Every p.()* method returns a boolean value, and n is incremented only if the value returned is true. If n==0 (this happens when EVERY method called returns false) then I need to call p.standard().
How can I write a more clear and efficient code? I tried with the or condition, something like this:
if (!( p.cap() || p.email() || p.isbn() || p.number() || p.phone() ||
p.price() || p.time() || p.url() || p.zip() || p.creditcard()
)) {
p.standard();
}
But obviously it didn't work properly (example: if p.cap() returns true the other methods are not called).
I need to call every method.
You did not specify if every method has to be called, but it seems you want to call them all regardless of individual results. So use the simple or operator: | (not the short circuit or ||).
if (!( p.cap() | p.email() | p.isbn() | p.number() | p.phone() |
p.price() | p.time() | p.url() | p.zip() | p.creditcard()
)) {
p.standard();
}
With some boilerplate, you could abstract this into some sort of validator interface:
interface Validator {
boolean validate(Foo p);
}
Validator[] validators = new Validator[] {
new Validator() { boolean validate(Foo p) {return p.cap();} },
new Validator() { boolean validate(Foo p) {return p.creditcard ();} },
new Validator() { boolean validate(Foo p) {return p.email();} },
// …
}
public int validateAll(Foo p, Validator[] validators) {
int valid = 0;
for (Validator v : validators) {
if (v.validate(p)) valid++;
}
return valid;
}
if (validateAll(p, validators)) p.standard();
This is a net increase in code, but it has the advantage of communicating "run all these checks on p" clearly, and the list of checks is extensible.
(I admit this might easily be a solution that's way too heavy for your needs.)
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