in reply to deep usage of if-else!!

Not sure i really like this, but it's logicaly equivalent and cleans it up a little ..
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... } } } }

Replies are listed 'Best First'.
Re^2: deep usage of if-else!!
by rootcho (Pilgrim) on May 01, 2006 at 18:27 UTC
    yes $o->check() returns 0 or 1, but in most of the cases it is too long i.e. parameter passed to it, that is why i'm using $o->state.
    But yes as u and Hue-Bond said I can see which are the most frequently combination of checks and actions and separate them into methods, this way I will shorten things abit.(before i read your replies i figured out to shorten one of them).
    But then, i can do this for just several of them, otherwise i will go into the trap of too-many-methods ;)
    That is why i was wondering that probably someone already invented something along these lines ;))
    thanx for your replies
      yes $o->check() returns 0 or 1, but in most of the cases it is too long i.e. parameter passed to it
      too 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() ){ ... }
Re^2: deep usage of if-else!!
by Anonymous Monk on May 01, 2006 at 19:24 UTC
    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.