Code SOD for the day: someone has left me a pile of code where they use "var" on object parameters to mean "contents of object are changed in this method". I am having to learn how to read that code (while I clean up huge numbers of memory leaks).

Code SOD for the day: someone has left me a pile of code where they use "var" on object parameters to mean "contents of object are changed in this method". I am having to learn how to read that code (while I clean up huge numbers of memory leaks).

    procedure PopulateList(var aList:TList);
    begin
         for i:= 1 to 1000 do
             aList.Add(MakeNewObject());
    end;

To me "var" on a parameter means "this thing will change", and on an object that means "this gives you back a different object", as in WalkDownTheQueue(const aQueue:TQueue; var aCurrentObject:TSomeObject); in which example we will ignore the oddness of that particular method.

The worst part of this is that some of those lists contain heterogenous objects. But at least Delphi has a common ancestor and sensible inheritance, so I can just call .Free; on them and move on. Assuming, that is, that the contents is actually an object. And references are unique. Both of which I've been wrong about so far.

Comments

  1. yes, you just push your data through an additional intermediate class and it works. Which is better than casting, except for performance. And readability. These things can always be done, it's just ugly and I end up spending a lot of time on C++ minutae instead of working.

    ReplyDelete
  2. Many encryption algorithms expand the size of the original clear text - or at least round it up to the nearest block size.  This might explain the CONST issue.

    ReplyDelete
  3. yeah, but that library is encrypting constant input into a newly created output, so padding is not an issue.

    ReplyDelete

Post a Comment