‎2008 Aug 14 3:28 PM
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.
‎2008 Aug 14 3:32 PM
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
‎2008 Aug 14 3:32 PM
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
‎2008 Aug 14 3:36 PM
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 .
‎2008 Aug 14 3:50 PM
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
‎2008 Aug 14 3:56 PM
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.
‎2008 Aug 14 4:01 PM
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
‎2008 Aug 14 4:39 PM
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.
‎2008 Aug 14 4:43 PM
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