Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

When should I write a class instead of a function module?

hagit
Active Participant
0 Likes
5,748

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

1 ACCEPTED SOLUTION
Read only

geert-janklaps
SAP Mentor
SAP Mentor
4,517

Hi,

In my personal opinion there are mainly 3 reasons to still use function modules, these cases are:

  • You need an RFC / Update task enabled logic (e.g. to call it from an external system)
  • You need to use screens (using screens is not possible in classes directly)
  • You need to write logic which is to be hooked into certain frameworks which are not ABAP OO-enabled

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:

  • Core logic (class, in which you never use function modules which use screens)
  • Screen logic (function modules)
  • Application logic (class, which calls the screen logic function modules)

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

13 REPLIES 13
Read only

geert-janklaps
SAP Mentor
SAP Mentor
4,518

Hi,

In my personal opinion there are mainly 3 reasons to still use function modules, these cases are:

  • You need an RFC / Update task enabled logic (e.g. to call it from an external system)
  • You need to use screens (using screens is not possible in classes directly)
  • You need to write logic which is to be hooked into certain frameworks which are not ABAP OO-enabled

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:

  • Core logic (class, in which you never use function modules which use screens)
  • Screen logic (function modules)
  • Application logic (class, which calls the screen logic function modules)

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

Read only

0 Likes
4,517

thanks for your answer.

Why do you think that a static method in a class is better than a function module?

Read only

0 Likes
4,517

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

Read only

matt
Active Contributor
4,517

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.

Read only

0 Likes
4,517

if you start using Class, forget the static method. There is a place for Abap developper who use it ... next to Hungarian notation

Read only

matt
Active Contributor
4,517

data(my_instance) = my_class=>get_instance( ).

Otherwise I agree.

Read only

0 Likes
4,517

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

Read only

matt
Active Contributor
4,517

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.

Read only

0 Likes
4,517

Matthew Billingham What do you mean in "Singleton and factory pattern."?

Thanks

Read only

pfefferf
Active Contributor
4,517

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

Read only

matt
Active Contributor
0 Likes
4,517

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!

Read only

Jrg_Brandeis
Contributor
4,517

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:

  • Type checking at design time for method calls. In contrast, function modules are checked at runtime, leading to dumps in case of incompatibilities. This is the most important advantage of static methods!
  • Better control over the reusability or visibility of methods with the keywords PUBLIC, PROTECTED and PRIVATE.
  • In general: All the advantages of Object Oriented Programming over Functional Programming. For example, encapsulation of data, inheritance, polymorphism and interfaces.

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

Read only

hagit
Active Participant
4,517

Thank you for your valuable answer. It is pity that I can not mark more the on answer as Best answer

Hagit