java - Optimizing code for more clearness and efficiency -
this code works fine, need simplify more clearness , more efficiency:
int = 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 code says, need call multiple methods (i don't know finite number of them). every p.()* method returns boolean value, , n incremented if value returned true. if n==0 (this happens when every method called returns false) need call p.standard().
how can write more clear , efficient code? tried or condition, 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 didn't work (example: if p.cap() returns true other methods not called).
i need call every method.
you did not specify if every method has called, seems want call them regardless of individual results. use simple or operator: | (not 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(); }
Comments
Post a Comment