Should Class Helpers be used in developing new code?
Asked Answered
H

10

10

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.

Hallucinate answered 10/12, 2008 at 2:31 Comment(0)
F
15

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.

Fremantle answered 10/12, 2008 at 4:32 Comment(1)
You will always have other options though, like decoration and adapter-patterns etc.Sonja
S
8

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...

Sonja answered 10/12, 2008 at 10:5 Comment(0)
M
6

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.

Mawkin answered 10/12, 2008 at 7:2 Comment(1)
.net extension methods is not limited in the same way that class helpers are. You wouldn't suggest using extension methods in .net, if all linq support would blow up by it, would you? You can only have one class helper for each class. If you define one, it will break any existing ones.Sonja
C
3

I use them a lot. I use Remote Objects and the objects there are created by the RO engine so you cannot add to them without descending from them and then other bits of messing around. Class Helpers mean I can treat them like any other object. and while a class can only have one helper, you can descend helper classes so you get the inherited behaviour.

Council answered 10/12, 2008 at 13:33 Comment(2)
So, if you cant derive, you use a class helper. If there is a class helper already, you derive from it. What if you cant derive from it? Will you make a class helper for the class helper...?Sonja
I use class helpers in exactly the same way with the auto generated RemObjects SDK units.Jauch
A
2

Sorry, can't help but be Captain Obvious for a moment: If the internal Delphi people themselves state "they should not be viewed as a design tool to be used when developing new code" then by definition they shouldn't be used. They are there for extending the VCL for their own purposes only. Who else is going to give you a better reason than the people that wrote it?

Abridge answered 10/12, 2008 at 3:14 Comment(3)
I think the real question was "Why" and "because I told you not to" rarely works as an answer -- trust me, I have kids. :-)Damiano
@DarianMiller Touche, you raise a good point. Maybe my question would be better stated "Should the documentation be updated to remove the warning . . . "Hallucinate
I also have kids. You don't stop warning just because they want take your 'because' for an answer...Sonja
L
2

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.

Liss answered 10/12, 2008 at 15:8 Comment(0)
S
2

Maybe a good aproach you can use is (as I use it):

  1. Always give preference to inheritance over class helpers, use them only when inheritance is not an option.
  2. Give preference to Class helpers over bare global methods.
  3. If you're going to need the extendend functionality in more than a Unit, try something else (like class wrappers).

.Net Extensions methods are way too similar and where created and supported for the exactly same reason: Make an Extention of the base classes (rather than an upgrade wich in Delphi.Net was not an option in order to try to make Delphi native code kind of "compatible" with .Net code - IMHO this was too ambitious)

Anyway, Delphi Class helpers are still quite a tool in some situations.

Subnormal answered 11/12, 2008 at 0:52 Comment(0)
D
1

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.

Damiano answered 10/12, 2008 at 2:36 Comment(3)
Yes, I think C# extension methods were based on Class Helpers.Hallucinate
Indeed they were -- we hold the patent on the idea. ;-)Bounce
I talked to Anders at PDC2005 when they were announced and he more or less confirmed they were an extension of them.Hallucinate
G
1

I find myself using them more and more as a design construct.

Situations in which I use them :

  • In a client/server setup, I extend shared base-classes with class helpers to provide server- or client-only functionality.
  • To complement VCL/RTL classes (and other third party code) with handy tooling functions.
  • To work around differences when classes don't share the same inheritance tree (using helpers makes it possible to have have generic Count and Items properties, for example).

In fact, I wish Delphi would accept multiple helpers for the same base class - I've even filed a request for this if I'm remembering correctly.

Generator answered 10/12, 2008 at 6:45 Comment(0)
W
1

I found this article very interesting. It deals with C++ but the main ideas are language independent. The main gist is that global routines are sometimes preferrable to methods even in an OOP environment. From this view point, there's less need for class helpers.

Whitton answered 10/12, 2008 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.