Sharing functions/methods with class and non-classes

TylerK

New Member
Hello,
I'm running into an issue in development - I have an include that is utilized by several programs.
I want to use the functions in this include for one of my classes. However, we can only define methods within classes so I can't simply inject the include in my class and utilize the functions in the include.

Is there a way to invoke the functions in the include from my class without having to create another class and converting all these functions into methods solely for my one class that requires it ?

I am trying to avoid having duplicate code.. obviously not the best programming practice.
Any suggestions would be great.

Thanks for you time,
Tyler
 

Cringer

ProgressTalk.com Moderator
Staff member
I've done just this. It's horrible, but for some legacy code it's necessary.

Build a Service Class with your methods in it.

The service class method essentially just runs a .p wrapper to the procedure call in the include, or in my case the super procedure.

ParameterService.cls
Code:
    METHOD PUBLIC CHARACTER getParameter (pcParameter AS CHARACTER):
        DEFINE VARIABLE cReturn AS CHARACTER NO-UNDO.

        RUN getParameterWrapper.p (pcParameter, OUTPUT cReturn).

        RETURN cReturn.
    END METHOD.
getParameterWrapper.p
Code:
BLOCK-LEVEL ON ERROR UNDO, THROW.

DEFINE INPUT  PARAMETER pcParameter AS CHARACTER NO-UNDO.
DEFINE OUTPUT PARAMETER pcWert      AS CHARACTER NO-UNDO.

{ system/launch-param-manager.i }

FUNCTION getParameter RETURNS CHARACTER (pcParameter AS CHARACTER) IN SUPER.

/* ***************************  Main Block  *************************** */
pcWert = getParameter(pcParameter).
Hope this helps.


Edit: The plan is to slowly resolve all the calls to the old procedure with the Service Class. Then we can trash the super procedure and put the logic directly into the service class, but that's a long way off!
 

TomBascom

Curmudgeon
A lot depends on the nature of the include.

A simple include that is merely used to avoid cut & paste can be handled much more easily and with a more varied array of techniques than a parameterized include that has to unwind multiple levels of argument quoting.

If you shared a sample of what you are looking at some more specific suggestions could be offered.
 

tamhas

ProgressTalk.com Sponsor
Alternatively, create a class with the desired functionality and revise the includes and references to use the new class. More work, but much cleaner in the end.
 

Cringer

ProgressTalk.com Moderator
Staff member
Absolutely agree, but in practise this is less than easy. If it's a core function of the application, such as the parameter function in the application I mentioned above, then refactoring, testing and supporting 500+ occasions its used is not going to be a pretty job. And that's just one of the procedures in the toolkit. To totally refactor the super procedure would be in the region of 5000 changes, all of which would need testing.
That's the real cost of technical debt...
 

tamhas

ProgressTalk.com Sponsor
But also the sort of refactoring which is likely to be amenable to automation. I'm not belittling the job, just pointing out that it is good practice to recognize when something is done wrong by current best practice and to consider what might be done to fix it properly instead of always doing the minimal work.
 
Top