Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
One exit per procedure/function/codeblock to what purpos
Message
De
08/10/2003 22:51:54
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00835552
Message ID:
00836621
Vues:
18
[scroll down]

>I'm sorry, I don't agree with your assertion ** IN THIS INSTANCE **.
>
>While your code is readable and operates flawlessly, and I would NEVER assert that this was "bad" or "incorrect" code, I would structure it as follows:
>
>
>Procedure someproc
>*-- Evan would replace
>* Local ...
>*-- with
>Local luReturnValue, ...
>With This
>  If !.GetValidDateRanges()
>    *-- Evan would replace
>    * Return .F.
>    *-- with
>    luReturnValue = .F.
>  Endif
>  * Some other code ...
>  Select ... From ...Into Array .arrItems
>  If Type('.arrItems[1,1]')#'N'
>    *-- Evan would replace
>    * Return .F.  && No items
>    *-- with
>    luReturnValue = .F.
>  Endif
>  For ix=1 To Alen(.arrItems,1)
>    If .arrItems[ix,2] = 'somecondition'
>      *-- Evan would replace
>      * Return .SomeOtherProc(ix)
>      *-- with
>      luReturnValue = .SomeOtherProc(ix)
>    Endif
>    If .arrItems[ix,3] = 'somecondition2'
>      *-- Evan would replace
>      * Return .SomeOtherProc2(ix)
>      *-- with
>      luReturnValue = .SomeOtherProc2(ix)
>    Endif
>  Endfor
>  * Some other code where also there are few other returns ...
>Endwith
>*-- Evan would add here:
>RETURN ( luReturnValue )
>
>Endproc
>
>
>This accomplishes EXACTLY the same end result with a SINGLE EXIT POINT, and does NOT slow the code down or make the code ANY LESS READABLE. It also does not restrict breaking the code down into "handler routines" as you suggest (which is what I also do -- great minds -g-).

You'd still need to add checking for luReturnValue in each IF after the first one:
    If luReturnValue  and .arrItems[ix,3] = 'somecondition2'
      luReturnValue = .SomeOtherProc2(ix)
    Endif
and it would still not be exactly the same, because return .someotherproc2(ix) could have returned .t. - and your code would still continue processing.

Somewhere in Codebook (or was it COM Codebook) I've found a very elegant way of such "so far so good" processing. In pseudo code, it's something like
llOK=.f.
do case
   case not this.firstcheck()
   case not this.secondcheck()
   case not this.thirdcheck()
...
   case not this.enthcheck()
   otherwise
      llOK=.t.
endcase
return llOK
This, of course, works just because all the check functions are returning .t. for success, .f. for fail. If the functions in Cetin's case were supposed to return some other type of result if successful, and .f. if they fail, then it's either getting done his way (with returns all over the place), or will be very complicated, because you'd have to do all the checking after each call, and keep luRetValue for value, and llContinue as an indicator of whether to go into any further tests (to simulate early return).

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform