I’m sorry for going back to the IIF discussion but…It took me a while to read the posts and then I felt embarrassed because my own opinion differs in so many regards from respected members of the FoxPro community.
IF …ELSE … ENDIF Several people stated that they consider it a bad coding practice to have an IF without an ELSE. I would prefer to say that
one should always think about the ELSE but I don’t believe one should always
code it. Indeed applied mechanically one can have truly horrendous code like the following:
if vartype(tMan) <> “C”
Return this.throwerror(“Bad Man”)
else
if vartype(tPhilosopher)<>”C”
Return this.throwerror(“Bad Philosopher”)
Else
endif
endif
I know that none of you would code like this because it disobeys the rule to keep your logical structures flat but someone new or from another language might obey the rule literally.
Another example: suppose you have a program for making soup one might have look like:
if not this.lEnoughSalt
this.AddSalt()
endif
if not this.lEnoughPepper
this.AddPepper()
endif
with many many if conditions. I believe adding ELSE decreases the readability of the function by almost doubling the length of the code -- assuming you add a comment in the else. If you don't have a comment any maintenance programmer is going to assume that you just forgot to code the else and will spend an hour trying to deduce what ought to be there!
Using the IF ELSE to replace logical expressions if a=b
c=.f.
else
c=.t.
endif
There are couple reasons why I think people use the if else
a) in FoxPro the assignment operator (=) is the same as the equality operator (=) this makes expressions like
c=b=a hard to read even though they are sompletely valid.
b) The IF construction follows the english language better.
c) While in semantic logic we have the conditional (->) operator most computer languages don't have one-- instead restricting themselves to AND, OR, NOT, >,= etc. so there may be a tendency to replace the nonexistant IF
operator with the existant IF ELSE ENDIF
command I don't know because I couldn't come up with any good real world examples.
d) if else can be used to make an OR
llIsGeologist = .f.
if (llHasPulse AND ...)
llIsGeologist = .t.
endif
if (llHasBeard AND ...)
llIsGeologist = .t.
endif
really means
llIsGeologist = (llHasBeard AND ...) OR (llHasPulse AND ...)
this could have been coded
llIsGeologist = (llHasBeard AND ...)
llIsGeologist = llGeologist OR (llHasPulse AND ...)
but the first version is probably the easiest to read.
I don't use the if else to build expressions very often because (which brings us back to the first point) they don't have closure. If the llIsGeologist is not anywhere above then the program will crash sometimes: i.e. an easy way to make a bug that may not come out in testing. So I only use the if when I have expressions that I can't understand anymore because of their complexity.
iifCouldn't agree with you more. Curiously I accidentally changed mz kezboard to German and can't fix it. So I apologise for any typing errors.
DeMorgans Rule and Logical TransformationsSometimes I see people (particularly smart ones) simplifying expressions with logical transformations. For example for the rule “Programmers cannot have salary more than 50000” I saw the expression
JOB<>’Pgmr’ or SAL < 50000
instead of
NOT (JOB = “Pgmr” and SAL>=50000)
the two expressions are logically equivalent but I find the first difficult to comment while the second requires no comment at all! I think logical transformations are worth thinking about: sometimes one gets an insight when one transforms an expression but only use the transformation if the new expression can be understood in natural speech. Pity the poor maintenance programmer!
James Beerbower
James Beerbower Enterprises
Frankfurt, Deutschland