$o->check(...);
unless ($o->state) {
action..
} else {
$o->check(...);
unless ($o->state) {
action....
$o->blah()
if ($o->state) {
actions..check..
unless($r->state) {
and so on...deeper and deeper...
}
}
}
}
Most of the checks are on $object->state(), but not nececerily.You should try to divide that piece of code in logical, reusable parts that can be abstracted into functions. That way, the nesting won't go very deep and your program will gain in readability and maintainability.
--
David Serrano
if( do{$o->check(...); ! $o->state} ){
action..
}elsif( do{$o->check(...); ! $o->state} ){
action....
$o->blah()
if ($o->state) {
actions..check..
unless($r->state) {
and so on...deeper and deeper...
}
}
}
It's hard to say w/o knowing what the check() and state() methods do and their return values.. In general, I would say to push things off into methods ..
$o->check(...);
unless ($o->state) {
$o->actionSet1;
} else {
$o->check(...);
unless ($o->state) {
$o->actionSet1;
}
}
So what does the check() method return? if it did return $this->state then things simplify alot:
unless ( $o->check(...) ) {
action..
} else {
unless ( $o->check(...) ) {
action....
$o->blah()
if ($o->state) {
actions..
unless( $o->check(...) ) {
and so on...deeper and deeper...
}
}
}
}
yes $o->check() returns 0 or 1, but in most of the cases it is too long i.e. parameter passed to ittoo long? so? that's fine, or bundle the params into a hash, or store in the object:
if( ! $obj->check(
blah => 1,
stuff => 2,
foo => 'bar',
...
)){
...
}
my %params = (
blah => 1,
stuff => 2,
foo => 'bar',
...
)
if( ! $obj->check(%params) ){
...
}
$obj->blah(1);
$obj->stuff(2);
$obj->foo('bar');
...
if( ! $obj->check() ){
...
}
if( do{$o->check(...); ! $o->state} ){
action..
}
Why the do block instead of a comma?
if($o->check(...), ! $o->state} ){
action..
}
...not that I think that putting multiple things inside the conditional of an if is a good idea.
In this situation I try if possible to take advantage of early exits:
If this is inside a while or for loop use last as the final statement in any block preceeding an else and omit the else. If it is inside a sub and there are no statements following the topmost if, use return in place of last in a similar fashion.
If need be change the sense of the test to take advantage of this technique. For example, assuming a sub:
$o->check(...);
unless ($o->state) {
action..
return;
}
$o->check(...);
return if $o->state;
action....
$o->blah()
return unless $o->state;
actions..check..
return if $r->state;
and so on... at the same level
perlmonks.org content © perlmonks.org and Anonymous Monk, davidrw, GrandFather, Hue-Bond, rootcho
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03