Using Generic containers in Delphi XE - always?

后端 未结 7 1207
星月不相逢
星月不相逢 2021-02-03 10:45

Generic containers can be a time saver when having a item, and a strongly typed list of those items. It saves the repetitive coding of creating a new class with perhaps a TList

7条回答
  •  梦谈多话
    2021-02-03 11:15

    If you need polymoprhic lists then Generics are a hindrance, not a help. This does not even compile, for example, because you cannot use a TDogList where a TAnimalList is required:

      uses
        Generics.Collections;
    
      type
        TAnimal = class
        end;
    
        TDog = class(TAnimal)
        end;
    
        TAnimalList = TList;
        TDogList = TList;
    
    
      procedure FeedTheAnimals(const aList: TAnimalList);
      begin
        // Blah blah blah
      end;
    
    
      var
        dogs: TDogList;
      begin
        dogs := TDogList.Create;
        try
          FeedTheAnimals(dogs);
    
        finally
          dogs.Free;
        end;
      end;
    

    The reasons for this are quite clear and easily explained, but it is just as equally counter intuitive.

    My own view is that you can save a few seconds or minutes (if you are a slow typist) by using a generic instead of rolling a type safe container more specific and appropriate to your needs, but you may well end up spending more time working around the problems and limitations of Generics in the future than you saved by using them to start with (and by definition if you haven't been using generic containers up to now then you don't know what those problems/limitations might be until you run into them).

    If I need a TAnimalList then the chances are I need or could benefit from additional TAnimal specific methods on that list class that I would like to inherit in a TDogList, which in turn may introduce additional specific members relevant to it's TDog items.

    (Animal and Dog being used for illustrative purposes only, of course. I don't actually work on veterinarian code currently - LOL)

    The problem is, you don't always know this at the start.

    Defensive programming principles suggest (to me, ymmv) that painting yourself into a corner for the sake of a bit of time saving is likely to end up costing a lot in the future. And if it doesn't, the additional "cost" of not taking the up front saving is itself negligible.

    Plus your code is more shareable with users of older Delphi versions, if you are inclined to be so generous.

    :)

提交回复
热议问题