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 ||).