Refinements couldn't do anything that procedures and functions cannot do - but it was a nice element of the language, that its syntax actually focused on the principle of top-down programming.
It works like this in Delphi: Let's write a procedure that sorts a file, line by line. Here is our first code:
procedure SortFile (filename:string);
begin
// Read file into memory
// Sort the lines
// Write file out of memory
end;
In Elan, the comments would be refinements, and you would write the actual implementation elsewhere. Delphi doesn't have refinements, but you could write procedure calls instead.
Another method is to implement each of these comments by actually writing the code, letting the comments group the lines:
procedure SortFile (filename:string);
var
sl:TStringList;
begin
sl:=TStringList.Create;
try
// Read file into memory
sl.LoadFromFile (filename);
// Sort the lines
sl.CaseSensitive:=False;
sl.Sort;
// Write file out of memory
sl.SaveToFile (filename);
finally
FreeAndNil (sl);
end;
end;
This approach usually leads to very readable procedures. Usually, it also means that complex algorithms are written using several small and simpler algorithms.
No comments:
Post a Comment