[fpc-pascal] New feature: IfThen() intrinsic

Michael Van Canneyt michael at freepascal.org
Tue Feb 2 12:01:45 CET 2016



On Tue, 2 Feb 2016, Mark Morgan Lloyd wrote:

> Michael Van Canneyt wrote:
>
>> Call me old fashioned, but then I think
>>   foo:=iif(Condition1,
>>            iif(Condition2, ThenExpr1, ElseExpr1),
>>            iif(Condition3, ThenExpr2, ElseExpr2));
>> is more clear.
>> 
>> The "if then" construct is really confusing.
>> "if then" is a statement, not an expression.
>
> Yes, and that's why to get the desired semantics it's more appropriate to use 
> if then else  rather than  iif()  etc.
>
> Please correct me if I'm wrong, but my understanding is that in all cases 
> where parameters are passed to a procedure or function they are evaluated and 
> placed on the stack before the code is called. However the  if then else 
> statement never, under any circumstances, executes code in the untaken 
> statement/block, and it is that which is the required behaviour for an 
> "inline if" expression.

I cannot remember a single case in 25+ years where I had to pay attention to this.

I think that people who rely on the order of the arguments evaluation 
need to rethink/restructure their code, because their code is IMHO 
fundamentally flawed: it is not robust at all.

I think you should write robust code. And the reason is simple:

I can say with relative confidence that most programmers in my company can't even
tell you what the order is, much less that they should respect it, even
after 10 years of programming. So if they were to get their hands on code
that does rely on it: no way to tell what would happen.

Hence, I think code relying on order of evaluation is not 'robust' at all.

(that said, I do understand that the compiler must understand and keep a
strict order.)

Michael.



More information about the fpc-pascal mailing list