The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

If you control both caller and callee: do not “override” functions by introducing a function with the same name

Posted by jpluimers on 2020/12/22

Every now and then I see people “overriding” a function by introducing a function with the same name.

Often this raises a lot of confusion, because the override will only work if you have the unit of the override closer in your user scope.

Example:

Unit AdoOverrideUnit;

interface

function VarTypeToDataType(VarType: Integer): TFieldType;

implementation

uses
  Data.DB;

function VarTypeToDataType(VarType: Integer): TFieldType;
begin
  Result := Data.DB.VarTypeToDataType(VarType);
  // override Result for some ADO specific data management layer case.
  // ...
end;

end.

In this case it is much better to call the override AdoVarTypeToDataType instead of VarTypeToDataType.

Otherwise, when AdoOverrideUnit is not closer in scope than Data.DB, the wrong method will be called which is hard to track down.

–jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.