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

best practice for function module development

Former Member
0 Likes
1,430

When designing a function module, what is the best practice. Should we develop it so that most of the extraction is done within the function module, or should we develop it such that prior to calling the function module the extraction should be done in the main calling program and the function module would have a huge interface of many importing parameters?

For e.g. lets say for some business logic I need MATNR MTART MEINS BSTME and so on. Now by knowing the matnr I can get all the other fields. Should I create a function module with just matnr as importing parameter and do the Select on MARA in the FM or should I create the function module with all the fields as required importing parameters. What would be a best practise in such a scenario.

Thank you.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
998

The whole thing with function modules is the idea of encapsulation and abstraction, you want to make the interface as simple as possible for the caller. So in your example, you of course would simply pass the MATNR. You can then get the rest of the required data internally in your FM. This way other developers that may call your function, will not have to do the work of getting all that data before calling, and in most cases, they would all do it in a different way.

Regards,

Rich Heilman

7 REPLIES 7
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
999

The whole thing with function modules is the idea of encapsulation and abstraction, you want to make the interface as simple as possible for the caller. So in your example, you of course would simply pass the MATNR. You can then get the rest of the required data internally in your FM. This way other developers that may call your function, will not have to do the work of getting all that data before calling, and in most cases, they would all do it in a different way.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
998

The other thing you need to think about is how often the FM would be called. If you wanted 100 records from MARA then one select would be more efficient than calling the FM 100 times .

Read only

ThomasZloch
Active Contributor
0 Likes
998

If your function gets called frequently for the same MATNR you might also want to buffer the MARA data in the global memory of the function group, to avoid redundant database accesses.

Thomas

Read only

Former Member
0 Likes
998

Having the caller pass any few values as possible is a good idea. But the thing that I am concerned about is what happens if in the flow of the entire program many function modules are used, all of them similar to this example, that would mean that the Select on MARA would occur numerous times. Thats a performance issue.

Read only

0 Likes
998

Now we are getting into the ecapsulation part of function groups. Did you know that when you run your program, and it executes a FM, that ALL function modules in the function group are loaded into memory? And that global data inside the function group can be shared across functino module calls? This is how you would relieve your concern. Lets say that all of your function modules belong to the same group, And all of them need to use a record from MARA, and you don't want to do a SELECT in all of these functions. Basically, you just create a global structure variable in the TOP include of the function group. In the first FM, you simply do the select and put that data into the global variable, then during the next FM call(the FM must be in the same group) you can get the data from the global variable directly without having to go to the database.

Regards,

Rich Heilman

Read only

0 Likes
998

Ah, excellent. Very similar to OOPS but makes a lot more sense now. Thanks Rich. Your book on Next Generation ABAP Development is good reading by the way.

Read only

0 Likes
998

yes, that's right, the concepts are similar, in fact, here is some documentation.

http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5954f411d194a60000e8353423/frameset.htm

Thanks for you comment about the book.

Regards,

Rich Heilman