Skip to Content
Personal Insights
Author's profile photo Joachim Rees

ABAP Know How: 3 lines of code, 2 commented out

Sometimes, “little chunks of knowledge” can be as little as a few lines of code.
And most of them are commented out.

With this blog post, I’ll give you an example for it, explain why keeping them somewhere is usual for me, and invite you to let me know if you have similar cases.

My example is this:

* data lo_report TYPE REF TO lcl_report.
* CREATE OBJECT lo_report.
DATA(lo_report) = NEW lcl_report( ).

Explanation:
Defining a reference variable to a class,
and creating an instance of that class (that is: creating an object of that class),
and assigning that object to the reference variable
used to be to be 2 lines of code.
But it now can be done just as well in one line of code.

I try to use the new version, but I’m not always sure on first try.
Like, all those don’t work, and I remember failing with a least a few of them:

*FALSE exmaples:
lo_report = new lcl_report( )
DATA(lo_report) = NEW lcl_report.
DATA(lo_report) = NEW lcl_report().
DATA(lo_report) = NEW lcl_report( )

So I think it’s clear, that we benefit from having those “3lines of code, 2 commented out” examples.

But I don’t want them in my coding!
1. I don’t want useless comments. I’ll delete them!
2. How would I find such an example again? (I know I did that somewhere, but where?)

So, this all leads to my plan: I’ll use a SAP Community blog post for those things:

1. It’s the right place
2. I don’t need screenshots, my sources can be #textOnly!
3. it still looks nice, just like in the editor, as I can put it in a code block.
4. I can add new things by editing or as a comment.
5. Maybe most important: publicly shared knowledge is good knowledge: I have a convenient place to look thinks up again and others can also benefit.

What do you think about that?
Best
Joachim

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Michelle Crapo
      Michelle Crapo

      Awesome idea!!!  You can find it again - and we can learn from it.

      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Thanks a lot for your Feedback, Michelle Crapo !

      I got another one right here:

      *While this one line does not work, the two following lines do just fine:
      *    METHODS: get_data IMPORTING it_so_act_type TYPE range of /scwm/lagps-act_type .
          TYPES: gty_act_type TYPE RANGE OF /scwm/lagps-act_type.
          METHODS: get_data IMPORTING it_so_act_type TYPE gty_act_type .
      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Already when writeing that 'I' and 'EQ' I thought: there's probably constants for that?!.

      Now I know:

          LOOP AT lt_lgtyp_param ASSIGNING FIELD-SYMBOL(<lfs_param_lgtyp>).
      *      ls_range_line-sign = 'I'.
            ls_range_line-sign = wmegc_sign_inclusive .
      *      ls_range_line-option = 'EQ'.
            ls_range_line-option = wmegc_option_eq .
            ls_range_line-low = <lfs_param_lgtyp>-par_value .
            APPEND ls_range_line TO lr_lgtyp.
          ENDLOOP.
      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Two ways to raise an exception with a MESSAGE: (one commented out)

      *      MESSAGE s002 DISPLAY LIKE 'E' WITH me->mv_lgnum 'lgtyp: todo'.
      *      RAISE EXCEPTION TYPE zclewm_cx_no_data_found USING MESSAGE.
            RAISE EXCEPTION TYPE zclewm_cx_no_data_found MESSAGE s002 WITH me->mv_lgnum 'lgtyp: todo'.
      

      The above is short, to keep the format.

      This is a little longer, to see some more context:

          INTO TABLE @DATA(lt_data_from_join).
      
          IF sy-subrc <> 0.
            "No data found (warehouse number: &1, storage types: &2 )
      *      MESSAGE s002 DISPLAY LIKE 'E' WITH me->mv_lgnum 'lgtyp: todo'.
      *      RAISE EXCEPTION TYPE zclewm_cx_no_data_found USING MESSAGE.
            RAISE EXCEPTION TYPE zclewm_cx_no_data_found MESSAGE s002 WITH me->mv_lgnum 'lgtyp: todo'.
          ENDIF.
      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Not sure if I explicitly said this already: this kinds of post often document my failures: I wrote some code and syntaxcheck said no.

      So those codeblocks show the way(s) something don't work (commented out), and the way(s) how it does work.

      create object lref_stock_mover EXPORTING iv_lgnum = iv_lgnum .
      * CREATE OBJECT lref_stock_mover( exporting iv_lgnum = iv_lgnum ) .
      lref_stock_mover = new zclewm_mon_stock_mover( iv_lgnum = iv_lgnum ).
      * lref_stock_mover = new zclewm_mon_stock_mover( exporting iv_lgnum = iv_lgnum ).
      * CREATE OBJECT lref_stock_mover( iv_lgnum = iv_lgnum ) .