ABAP Object Oriented Approach for Reports – Initial Design

By | March 25, 2013 | ABAP Objects, OO Design Patterns | 38,229 | 18

Many times, I see incorrect usage of ABAP Objects in custom development. In this two post blog series, I would show few incorrect designs and finally would show OO approach.

Incorrect Usage of Object Oriented

As part of my QA review responsibilities, many times, I see the report programs which are trying to use Object Oriented but would fail achieving any kind of object orientation. I consider these approaches as Pseudo OO Designs where the OO becomes just a vehicle to drive the procedural code. Something like this:

Incorrect Object Orientation

I would start this post with few bad examples. After them, I would take you back to procedural program – very simple program using subroutines. After that, I would show you the way I generally write Report programs using OO.

Only Static methods

Most of the times, I see developers are using only static methods in local class. Mainly the methods serve kind of wrapper to just have all the logic within it. Generally, there would GET_DATA and SHOW_DATA methods. This is not at all considered as Object Oriented design as there no objects involved only static method calls !

 
* Class definition
CLASS LCL_VALIDATION DEFINITION FINAL.
   PUBLIC SECTION.
     CLASS-METHODS :
              AUTHORITY_CHECK_TCODE,
              VALIDATE_CUSTOMER,
              VALIDATE_BILLING_TYPE.
ENDCLASS.
 

Basic problems using this approach

  • The core logic of the report is hidden beneath the local classes in the program. As I have noted in when to use local class, this limits the reuse of the core logic
  • Static methods along with other issues, lacks the basic advantage of method redefinition as we recently discussed in ABAP Static vs Instance method – Which to use when?

Wrapper methods to call Subroutines

Another flavor of what I generally face the design using the wrapper methods. You would see a method call in say START-OF-SELECTION, but that method would just call another subroutine. This subroutine would have more code modularization.

 
CLASS LCL_REPORT_PROCESS IMPLEMENTATION.
*
  METHOD GET_DATA.
     PERFORM F_GET_DATA.
  ENDMETHOD.
*
  METHOD FILL_FINAL_DATA.
     PERFORM  F_FILL_FINAL_DATA.
  ENDMETHOD.
*
  METHOD GENERATE_OUTPUT.
     PERFORM F_GENERATE_OUTPUT.
  ENDMETHOD.
*
ENDCLASS.              " LCL_REPORT_PROCESS IMPLEMENTATION
 

By simply looking at the design you would think what would be the benefit of using the methods as wrapper to the subroutines. This design has more issues than the previous one as this creates lot more confusion on which method calls which.

Use of Selection Screen Parameters & Select Options in the method implementations

All the time, whenever I see the OO ABAP used for any report, I see that developers are always using the parameters and select options directly in the method implementation. This makes the method impossible to use in any other way.

 
*
DATA: V_WERKS TYPE T001W-WERKS.
*
CLASS LCL_VALIDATION DEFINITION FINAL.
   PUBLIC SECTION.
     CLASS-METHODS :VALIDATE_PLANT.
ENDCLASS.
*
*
*...
* Validate Plant.
  METHOD VALIDATE_PLANT.
 
    IF S_WERKS IS NOT INITIAL.   " << Select Option
      SELECT WERKS UP TO 1 ROWS
             FROM   T001W
             INTO   V_WERKS
             WHERE  WERKS IN S_WERKS.
      ENDSELECT.
      IF SY-SUBRC NE 0 AND V_WERKS IS INITIAL.
"
      ENDIF.
    ENDIF.
  ENDMETHOD.                    "VALIDATE_PLANT
&#91;/abap_code&#93;
 
<h3>Global Variables instead of Instances</h3>
In this approach, developers try to use the Local Instance methods, but they still declare all the TYPES, DATA, Internal tables everything as Global Variables. Many times, these variables wont be used in more than one place.
 
In the code above, you can see that V_WERKS is not declared locally in the method. There isn't any need for doing that.
 
You would find few of these issues in the post on SCN <a href="http://scn.sap.com/community/abap/application-development/objects/blog/2012/12/15/abap-oo--an-abapers-perspective" target="_blank" rel="nofollow">OO - An ABAPer's perspective</a>. I tried to outline the similar design issues in my <a href="http://scn.sap.com/community/abap/application-development/objects/blog/2012/12/15/abap-oo--an-abapers-perspective#comment-335366" target="_blank" rel="nofollow">comments</a> to the post.
 
<h3>Procedural Report</h3>
Lets take a step back and think how this design can be improved. So, Start with this basic program. Program has few subroutines to validate the input, get the data and generate the output.
 
[abap_code slider="X" title=""]
*&---------------------------------------------------------------------*
*& Purpose - Simple procedrual report
*& Author  - Naimesh Patel
*& URL     - http://zevolving.com/?p=2037
*&---------------------------------------------------------------------*
REPORT  ztest_np_t100_report.
*
DATA: v_msgnr TYPE t100-msgnr.
DATA: v_arbgb LIKE t100-arbgb.
*
DATA: t_t100 TYPE STANDARD TABLE OF t100.
DATA: o_salv TYPE REF TO cl_salv_table.
*
SELECTION-SCREEN: BEGIN OF BLOCK blk1 WITH FRAME TITLE text-t01.
PARAMETERS:     p_arbgb TYPE t100-arbgb OBLIGATORY.
SELECT-OPTIONS: s_msgno FOR v_msgnr.
SELECTION-SCREEN: END OF BLOCK blk1.
*
AT SELECTION-SCREEN ON p_arbgb.
  PERFORM f_validate_message_class.
*
START-OF-SELECTION.
  PERFORM f_get_data.
*
END-OF-SELECTION.
  PERFORM f_show_data.
 
*
FORM f_validate_message_class.
  SELECT SINGLE arbgb
    INTO v_arbgb
    FROM t100
    WHERE arbgb = p_arbgb.
  IF sy-subrc NE 0.
    MESSAGE 'Message Class not found' TYPE 'E'.
  ENDIF.
ENDFORM.                    "f_validate_message_Class
*
*
FORM f_get_data.
  SELECT * FROM t100
    INTO TABLE t_t100
    WHERE arbgb = p_arbgb
    AND   msgnr IN s_msgno.
ENDFORM.                    "f_get_Data
*
*
FORM f_show_data.
  TRY.
      CALL METHOD cl_salv_table=>factory
        IMPORTING
          r_salv_table = o_salv
        CHANGING
          t_table      = t_t100.
 
      o_salv->display( ).
 
    CATCH cx_salv_msg .
  ENDTRY.
ENDFORM.                    "f_show_Data
 
 

Next Post

In the next post, I would cover, how you can achieve much more flexible and reusable design. The second part is now available at ABAP Object Oriented Approach for Reports – Redesign

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

18 Comments

  • Hi Naimesh,

    You are touching on a popular ‘pet peace’ I have with object oriented programming in SAP. I see this type of OO programming being used quite a lot. Classes being created when all they do is wrap procedural code and can’t really be reused at all.

    Really looking forward to the next post to continue from the topic.

    Keep up the good.

    Pete

  • steve oldner

    Ouch! How did you get my code? Very good series, I am sending this to my co-workers.

  • Hello Pete & Steve,

    Glad you liked the subject. Yes, usage of Pseudo OO is very much wide spread ..

    Thanks,
    Naimesh Patel

  • Naimesh,

    You pointed out a very good topic for object-oriented ABAP.
    These all happen because of ABAP is kind of mix of procedural and OO mixed. But if you are developing OO, please do OO. Also changing procedural habits and changing minds for OO are not easy!

    I am looking forward to seeing your next post.
    Good job, thank you.

  • Robson Soares

    I’m also looking forward to seeing your next post, especially because I still have not achieved OO programming. Seeing this change of paradigm through code comparison will be very interesting.

    Great job!

    Thank you.

  • Hello Tuncay,

    Thanks for your point. I agree with you. If you are developing in OO, you should try to leverage Object Oriented as much as possible.

    Regards,
    Naimesh Patel

  • Hello Robson,

    I hope that I would exceed the expectation here πŸ™‚

    Regards,
    Naimesh Patel

  • Miguel Silva

    Hi Naimesh,

    Thank you very much for your articles. I have been watch your blog for long time and it is very helpful on my job.

    Can you please write a post about how to use the parameters and selection screens parameters and select options on reports with abap oo (local classes on reports).

    Thank you in advance.
    Miguel Silva

  • Tenorio

    Excellent friend, good topic.

    ABAP OO is a great way to work, but is rarely used.
    I would like to know more about ABAP OO could suggest some study material.

    Really your site is the best I’ve seen on this topic.

    Excellent job!

  • NorthernGuy

    HI Naimesh,

    looking forward to read year next blog in this series.

    I try to design all my reports in an OO approach, still facing some issues where you have “global” Objects like the selection-screen or messsage handler. I’m still undecided what’s the best approach. Make it “globally” available with singleton design pattern or pass it to all report layers and methods?

    Just one question, is S_WERKS really globally available in your method VALIDATE_PLANT in your example above? I’m not sure because in general I’m not using local classes. I think in a global class it’s not available.

    Best regards,
    Tapio

  • Hello Migual,

    Glad that my work helps you in day-to-day activities.
    The next article as mentioned in this post would talk about that and hopefully helps to clear few doubts.

    Thanks,
    Naimesh Patel

  • Hello Tenorio,

    Great that you are interested in knowing more about OO ABAP. But, why do you think that Object Oriented is not much used?

    I have wrote quite a few articles on basic on OO Concepts. Once you are familiar with them, you can read about Design Patterns. Also find the Case Studies where I mention the usage of OO ABAP.

    Thanks,
    Naimesh Patel

  • Hello Tapio,

    I hope my next post would clear some doubts on handling the selection screen related object. I would be glad to hear form you again after reading the next post on your thoughts.

    Since all the classes in the above examples are defined locally in the report, all selection screen fields are available in the methods, similarly they are in Subroutines. I was actually referring to the V_WERKS which was declared globally – outside the class and used within the method implementation.

    Regards,
    Naimesh Patel

  • Hello All,

    Just to close the loop: ABAP Object Oriented Approach for Reports – Redesign – 2nd part is now available.

    Thanks,
    Naimesh Patel

  • Suhas Saha

    Hello Naimesh,

    I understand the limitations of static methods but in a simple report where you fetch some data and display it do you really need to declare the methods as instance (just for the sake of it)? I have been having this dilemma ever since i have started coding in OO.

    However due to my affinity towards K.I.S.S, i tend to define the methods as static when i realize the only purpose they are gonna serve is modularise πŸ˜‰ But obviously using subroutines inside method definitions is a huge turn-off!

    Although in case of complex reports the decision is fairly simple – MVC! I have been bitching about the inability to integrate UI in class definitions, owing to which it’s difficult to design a “picture-perfect” MVC for reports/module pool. I have tried and it only adds to the complexity πŸ™

    Let me know your thoughts

    Cheers,
    Suhas

  • Hello Suhas,

    For simple reports, if you are going to declare local class, it wont matter of which to use – Static Vs Instance. Since its a local, buried in the program, you wont be able to reuse it for sure, if needed anywhere else.

    In the same report, if you want to redefine something, you wont be able to achieve if you don’t have instances. E.g. a report to generate two views – Summary & Detail ALV. If you abstract and modular your code, almost the ALV logic would be similar. In case if you go with Static methods, what you would end up is a procedural code where you create a generic method which would be called within the specific method of Summary vs Detail.

    Agree that in-capabilities of SAP like UI within Class, or Push notification makes it incomplete usage of MVC. Think like a stock watch app where the data is keep getting refreshed whenever model sends a new notification. In SAP, we can achieve similar functionality by constant checking for new updates – a different from pure MVC.

    Please check the second part of the series ABAP Object Oriented Approach for Reports – Redesign on decoupling the things as much as possible.

    Thanks,
    Naimesh Patel

  • Sameer

    Hi Naimesh,

    Its a really helpful post, keep up the good work. I want to know that is it ok to use function module instead of methods, if we are developing our report using OOPs ? Because sometimes we dont get method name for a particular task and we easily get the function module for the same, so in that case shall we use function module in OOPs? Please tell me any way to find the method against a function module or against a particular task we want to perform.

    Regards
    Sameer

  • Hello Sameer,

    If you have already established logic which is working as expected, you can call that FM in a wrapper method e.g. method GUI_UPLOAD of CL_GUI_FRONTEND_SERVICES uses the FM GUI_UPLOAD to do the upload.

    But, this should not be the excuse when you are in a new development. If you want to start from a scratch and create a FM in a wrapper method, you wouldn’t able to leverage full features of what Object Oriented is offering.

    SAP is creating more and more Object oriented. I guess the only way to get know if there is any method, is to find for it.

    Regards,
    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.