Stephen Bertram wrote:
We're having a quiet (NOT) discussion about the PCness of the following code:
Which is preferable or what is a tidier method?
I'd do:

functionFindFirstMatch (List: TItemList; Status: TItemStatus):ListItem;
var
i : Integer;
begin
Result := -1;
i := 0;
while ((i < List.Count)
and (List[i].Status <> Status)) do
inc(i);

if i < List.Count then
Result := List[i];
end;

Reasons:
- Default Result initialised
- only one exit point
- I'd usually return a listitem rather than the
index, so that you can do:
item := FindFirstMatch(l, s);
if item <> nil then
// do some stuff to the item
rather than
itemindex := FindFirstMatch(l, s);
if (itemindex > -1) then
begin
item := l[i];
// should I test item here? Probably not.
// do some stuff to the item

- Personally I just prefer :
while (i < count) and (not cond(i)) do inc(i);
as opposed to
for i := 0 to count -1 do
if cond(i) then
exit;

but its probably mostly because I can never remember
whether to break or exit or continue, and which one
breaks out of a procedure, and which one breaks right
out of nested loops (D'oh).

---------------------------------------------------------------------------
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