Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Hate Nested IFs? Consider this...
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows 2000 SP4
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01086217
Message ID:
01086585
Views:
22
>>>I agree with the use of CASE statement for error control. Espacially in parameter validate at the top of procedure/methods. But, let simplify it a bit.
>>>
>>>
>>>local llSuccess
>>>llSuccess = .F.
>>>DO CASE
>>>   CASE NOT 1st_Logical_Test
>>>   CASE NOT 2nd_Logical_Test
>>>   CASE NOT 3rd_Logical_Test
>>>   OTHERWISE
>>>       *-- Actual code goes here.
>>>       llSuccess = .T.
>>>ENDCASE
>>>RETURN llSuccess
>>>
>>>
>>>I, also, general place a error message in the false CASE blocks to notify the caller as to why the routine failed.
>>
>>This is what YAG does regularly in COM Codebook, and I tend to disagree with JimN on this one. This is, IMO, an elegant use of the fall-through property of the DoCase construct. It has to perform all the logical tests to reach the success clause, and the first failure precludes the execution of other tests.
>>
>>To me, this looks like the standard checklist approach:
>>
lCanDrive=.f.
>>do case
>>   case not this.HaveCar()
>>   case not this.HaveLicense()
>>   case not this.drunk()
>>   case not this.tired()
>>   case not this.car.tank.empty()
>>   ...
>>   otherwise
>>   lCanDrive=.t.
>>endcase
>>
>>So, if someone doesn't have a car or a license, we don't care whether he has had a drink or two, and we surely won't check the tank in a car he doesn't have.
>>
>>I don't see anything wrong with this approach, on the condition that the method names are meaningful.
>
>I like and use the approach, but unlike some other languages, you have to remember that a VFP case statement only evaluates until it finds the first match, then drops out of the case statement. All other case statements are disregarded, so the order of your cases is sometimes critical. It can be a dangerous approach, if you aren't aware of the fact that the case statement only runs until it evaluates a true condition.

True, but and compoind IF statement with a string of OR and AND statements is also the the same. FoxPro evaluates the condition until one is evaluated as False. Then the remainder are ignored.
lCanDrive= this.HaveCar() ;
        AND this.HaveLicense() ;
        AND NOT this.drunk() ;
        AND NOT this.tired() ;
        AND NOT this.car.tank.empty()
This too, the order of the condition is critical. In the case of the CASE statement approuch, if a conditrion fails, and operation can be preformed.
lCanDrive=.f.
do case
   case not this.HaveCar()
       merssagebox("Buy a Car")
       return .f.
   case not this.HaveLicense()
       merssagebox("Get a License)
       return .f.
   case not this.drunk()
       merssagebox("Get a Taxi")
       return .f.
   case not this.tired()
       merssagebox("Take a nap")
       return .f.
   case not this.car.tank.empty()
       merssagebox("Buy Gas")
       return .f.
   otherwise
       lCanDrive=.t.
endcase
Where as in the IF statement, if is difficult to relate to the user what caused the condition to fail.
Greg Reichert
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform