Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Class Helpers be used in developing new code?

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 the the class or modifying the original. Later versions of Delphi found class helpers improved and they were ported to Win32.

In the help it says "they should not be viewed as a design tool to be used when developing new code."

Class Helpers violate traditional OOP, but I don't think that makes them a bad thing. Is this warning warranted?

Should class helpers be used when developing new code?

Do you use them when developing new code?

Why or why not?

Per Malcolm's comments: New code means daily application development, where you have some 3rd party libraries, some existing code, and then code you are writing.

like image 267
Jim McKeeth Avatar asked Dec 10 '08 02:12

Jim McKeeth


3 Answers

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.

like image 183
Malcolm Groves Avatar answered Oct 17 '22 13:10

Malcolm Groves


Before embracing class helpers as a new tool for fancy code, I think you have to understand the limitations is includes. There is only possible to provide one class helper for one class. So what will happen if you provide class helpers for your classes, and your classes derives from a common class that some other have provided a class helper for?

CodeGear introduces class helpers as 'a hack' to prevent breaking things, not as a cool design feature. When you design code, design it without class helpers. I know you can. When dealing with existing code that you can control, use refactoring. When there is no other way, reach for class helpers.

Thats my opinion any way...

like image 8
Vegar Avatar answered Oct 17 '22 13:10

Vegar


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.

like image 6
Lars Truijens Avatar answered Oct 17 '22 13:10

Lars Truijens