Published by Fabian on 20 Mar 2008 at 10:54 am
if cascade
I am currently doing a code review, where I have found this nice pearl, I have to share.
I really hope that wordpress does not kill the lovely formatting
if (initialisePools()) { if (replicateServices()) { if (replicateLocations()) { if (replicateLocationData()) { if (replicateContacts()) { if (replicatePackages()) { if (replicateCustomers()) { if (replicateCompanies()) { if (replicateGroups()) { replicateUsers(); ok=true; } } } } } } } } }
A shorter way to write would be:
ok = initialisePools() && replicateServices() && replicateLocations() && replicateLocationData() && replicateContacts() && replicatePackages() && replicateCustomers() && replicateCompanies() && replicateGroups() && replicateUsers();
any other proposals?
PS: Of course things can be always done differently, so your proposal must assume that invoking the methods separately makes sense
NiKo on 20 Mar 2008 at 12:21 pm #
Hey, this looks like a filterchain, or equivalent. Could be refactored as something like this (2mn coded, should be heavily enhanced):
NiKo on 20 Mar 2008 at 12:22 pm #
Ok, here’s a link for the code above
http://www.hashbin.com/1RE
NiKo on 20 Mar 2008 at 12:28 pm #
OOPS, it’s java here, I missed it :p
But I guess the patter should apply anyway
Guildenstern on 20 Mar 2008 at 1:54 pm #
no proposal but an annotation:
is the execution order of these functions relevant? If yes I wouldn’t like your proposal Fabian, because non-expert programmers might no know in which order the if statement is processed. Since I don’t JAVA I have no clue. My C++ compiler would resolve this from left to right but is that really specified?
Fabian on 20 Mar 2008 at 2:16 pm #
Its a standard left to right short circuit boolean operations. I am not aware of a language not doing so
Fabian on 27 Mar 2008 at 11:55 am #
in the same code i found:
lovely
Guildenstern on 08 Apr 2008 at 3:36 pm #
I found today something like this:
up to foo8()
but at least with different else clauses
Apotomo Cookbook » Blog Archive » Using Cells View Inheritance to clean up your views on 12 Apr 2010 at 8:00 pm #
[...] start getting really ugly when there are more conditions to respect and end up in a medieval if-cascade. It’s geting even worse when those deciders are copied and spread across multiple [...]