12-27-2020 4:49 PM
Hello experts,
I am familiar with functions modules and function group, but not with classes.
When should I write a class instead of a function module?
(I search for a clear answer but could not find)
Thanks in advance
Hagit
12-27-2020 5:31 PM
Hi,
In my personal opinion there are mainly 3 reasons to still use function modules, these cases are:
Personally I always write my core logic in classes and if I need the logic to be RFC enabled, I just write a wrapper function module around my class-based logic.
When using screens I even divide the logic in three parts:
But again this is a personal opinion, I don't think there's just one answer to this question. Both ways of working, work perfectly although object oriented programming is more future proof and definitely the way to go.
Best regards,
Geert-Jan Klaps
12-27-2020 5:31 PM
Hi,
In my personal opinion there are mainly 3 reasons to still use function modules, these cases are:
Personally I always write my core logic in classes and if I need the logic to be RFC enabled, I just write a wrapper function module around my class-based logic.
When using screens I even divide the logic in three parts:
But again this is a personal opinion, I don't think there's just one answer to this question. Both ways of working, work perfectly although object oriented programming is more future proof and definitely the way to go.
Best regards,
Geert-Jan Klaps
12-27-2020 7:51 PM
thanks for your answer.
Why do you think that a static method in a class is better than a function module?
12-27-2020 8:43 PM
Hi,
There's no real advantage here, both a function module or a static method can be used to hold your logic. Although a class-based approach (using static or instance methods) in general is better than using function modules, since you can make use of certain features like better error handling using exception classes, method chaining, ...
Best regards,
Geert-Jan Klaps
01-04-2021 7:04 AM
Four reasons to use a static method of a class instead of a function module
1. The syntax is neater
class=>_do_my_stuff( importing .... exporting ...)
or
result = class=>_do_my_stuff( ... ).
against
CALL FUNCTION 'Z_DO_MY_STUFF'...
2. The name of the method and class are checked at syntax check time, as are parameter types. No more "function not found" dumps due to a typo. No more conversion errors.
3. You can group related methods together rather more conveniently than with FM
4. They work better when programming in Eclipse!
I only use FM for screen handling and where I need RFC. The work of the FM is done using local or global classes.
01-04-2021 11:12 AM
if you start using Class, forget the static method. There is a place for Abap developper who use it ... next to Hungarian notation
01-04-2021 11:39 AM
01-10-2021 12:27 PM
Matthew Billingham What did you want to say by writing:
data(my_instance) = my_class=>get_instance( ).
Is there a situation where it is better to write a static method than an instance method?
Thanks
Hagit
01-11-2021 7:45 AM
Exactly that. This is an occasion where a static method is appropriate - when you want to control instantiation in the class. Singleton and factory pattern.
01-12-2021 4:05 PM
Matthew Billingham What do you mean in "Singleton and factory pattern."?
Thanks
12-28-2020 6:25 AM
One additional point I want to add as a pro argument for classes/interfaces is the better testability. Instead of blowing up your productive code with test-seams in case of functions, you can use the test double framework to create test doubles for based on interfaces.
To take advantage of that pro argument, you need to do unit testing of course, but I guess that is already a standard nowadays :-).
01-04-2021 7:06 AM
I put all my FM calls into a local class impelementing a local interface. That way, I can test without using test-seams. But yes, it's better to use classes directly. So long as implemented against an interface of course!
12-28-2020 7:34 AM
There are very good reasons why you should always use a class and Mehtods. Function modules are only appropriate if you are forced to do so for technical reasons. But that is rarely the case.
In addition to the points mentioned by 8d8214c7f9734f45be69f95cc0d5aeee and florian.pfeffer here are some more advantages of classes/methods:
To benefit from this, you need to learn and understand the concepts of Object Orientation. And you have to get involved with it. But then they have a huge advantage for ABAP developers.
Regards,
Jörg
01-03-2021 11:33 AM
Thank you for your valuable answer. It is pity that I can not mark more the on answer as Best answer
Hagit