Kurt Wrote:
>>But you are relying on a side-effect of calling the function, which is
a sneaky way of 
>>introducing errors. ie:

>>   x := 127;
>>   // there is an item with status = ON?
>>   if FindFirstMatch(List, ON, x) then
>>     item := something
>>   else
>>     item := List[x];
>>   // Now what is the status of item?

I think this may be taking the usage out of context a little. There is
nothing sneaky about misusing an answer of true or false to define if an
item is found. It is a lot more sneaky to use -1 as a false answer
because array and property indexes can happily use negative values. Ie
Array[-10..10] of TObject

This is one appropriate usage and example of a boolean function
returning true or false

If FindFirstMatch(Alist,Astatus,AitemIndex { or AITem}) then
Begin
  //Do something with Alist[AItemIndex]
  // >> if this was Aitem instead of itemindex then do something with
AItem
End else
Begin
  //Handle Exception for nothing in list without status matching.
End;

If there was no match then you don't write Code that GV's on invalid
indexing.

>>   x := FindFirstMatch (List, OFF);
>>   item := List[x]; // Bang! there is no item List[-1]!

This is just bad programming that the boolean result stops from
happening in the same fashion as

   x := FindFirstMatch (List, OFF);
   if x <> -1 then
     item := List[x] else
     item := nil ;

They all do the same thing except with the second option, you still have
to check for

  if assigned(Item) then
  begin
    //do something
  end else
  begin
    // do something when no match.
  end;

  the boolean function option is the only method that prevents you from
having to then provide multiple boolean tests following the execution of
the procedure.

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to