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 Reply