Should Class Helpers be used in developing new code?

前端 未结 10 1624
眼角桃花
眼角桃花 2021-02-14 10:51

Delphi 8 introduced Class Helpers for the purposes of mapping the VCL/RTL to the .NET object hierarchy. They allow injecting methods into an existing class without overriding t

相关标签:
10条回答
  • 2021-02-14 11:22

    These sound like C# extension methods. I would say that while extension methods like these are useful when you don't have the ability to modify a class that you need to extend with functionality, they are a poor way to design your own code. When designing your own code, you'd like all the functionality to be located in the same code file as much as possible rather than spread across different classes. I'd say use them for what they were intended for -- basically as decorators to add new functionality to closed classes -- and don't use them in designing your own code.

    0 讨论(0)
  • 2021-02-14 11:23

    I agree with Vegar in this: class helpers as a emergency tool. When you know they are the only way to get things done in the time provided. Later, if there's time to it, remove them.

    I one time forgot a parametrization thing, and if class helpers didn't exist in Delphi 2006 it would cost A ENORMOUS LOT OF TIME..... With class helpers, it took 6 hours to make thigs work right. BUT, it was an emergency situation - class helpers are an obscure language feature and it create difficulties to new developers to follow the flow of the program.

    0 讨论(0)
  • 2021-02-14 11:27

    Microsoft based LINQ heavily around their Extension Methods. In that light you should use Class Helpers in new code if that improves your code. See What are good uses for class helpers? for some good uses.

    0 讨论(0)
  • 2021-02-14 11:31

    Depends what you mean by "new code".

    They aren't really relevant for classes you are newly developing, so in that case, no, they probably shouldn't be used.

    But even in a brand new project, you may still need to modify an existing class that you can't change in other ways (vcl class, third-party class, etc). In this case, sure, I'd say go ahead.

    They're not evil in and of themselves. Like most other things, you just need to understand how they work and use them in an appropriate context.

    0 讨论(0)
提交回复
热议问题