Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean code: should we replace macros with methods, and why? #844

Closed
sandraros opened this issue Oct 9, 2021 · 10 comments · Fixed by #860
Closed

Clean code: should we replace macros with methods, and why? #844

sandraros opened this issue Oct 9, 2021 · 10 comments · Fixed by #860

Comments

@sandraros
Copy link
Collaborator

sandraros commented Oct 9, 2021

Recently, there was some little refactoring in a few abap2xlsx programs or classes, to replace macros (DEFINE ... END-OF-DEFINITION) with methods:

My question:

  • Is that a best practice in ABAP community? Discussion links?
  • Could it be decided once for all and write the decision in the abap2xlsx contribution guidelines, so that to have coherent code?

Thanks!

This issue can be used as support to collect future modifications. These are the macros left in abap2xlsx:

@larshp
Copy link
Member

larshp commented Oct 9, 2021

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abenmacros_guidl.htm

@larshp
Copy link
Member

larshp commented Oct 9, 2021

plus the linter and transpiler doesnt work very well with macros, and I'm too lazy to fix it

the linter and transpiler I see as a prerequisite for unit testing, unit testing is a prerequisite for keeping abap2xls stable and adding more features

@sandraros
Copy link
Collaborator Author

I'm not the devil's advocate, just to say, here's an example of "exceptional case" (c.f. macro guidelines), which is about avoiding a huge number of lines, in standard method GET_METADATA of CL_SHM_RIS_METADATA (https://launchpad.support.sap.com/#/corrins/3006480/41), with 15,000 lines of below kind of code, combined with the context which requires the code to be compatible with ABAP < 7.40, i.e. VALUE expressions are not possible (which is currently the case of abap2xlsx):

    CLEAR ls_md.
    insert_risdata '1' 'PROG' '' 'P' ls_md-where_used.
    insert_srchgrp 'STANDARD' '' 'TEXT-S01'.
    insert_srchgrp 'EINSTELLUNG' '' ''.
    insert_srchelm 'TITLE-PROGNAME' 'STANDARD' '1' '' '' '0' 'KEY1X' '' 'PA'.
    INSERT ls_search_elements_fb INTO TABLE ls_md-search_elements.
    insert_srchelm 'TITLE-PROGNAME' 'STANDARD' '2' '' '' '0' 'KEY1' '' 'SO'.
    INSERT ls_search_elements_fb INTO TABLE ls_md-search_elements.
    ...
    insert_ris_md 'PROG' '' 'PZ'.

the 4 macros are:

    DEFINE insert_risdata.
      CLEAR ls_risdata.
      ls_risdata-group            = to_upper( &1 ).
      ls_risdata-trobjtype        = to_upper( &2 ).
      ls_risdata-subtype          = to_upper( &3 ).
      ls_risdata-legacy_type      = to_upper( &4 ).
      INSERT ls_risdata INTO TABLE &5.
    END-OF-DEFINITION.

    DEFINE insert_srchgrp.
      CLEAR ls_search_group_fb.
      ls_search_group_fb-name            = &1 .
      ls_search_group_fb-parent_group    = &2 .
      ls_search_group_fb-text_ref        = &3 .
      INSERT ls_search_group_fb INTO TABLE ls_md-search_groups.
    END-OF-DEFINITION.

    DEFINE insert_srchelm.
      CLEAR ls_search_elements_fb.
      ls_search_elements_fb-for         = &1 .
      ls_search_elements_fb-group       = &2 .
      ls_search_elements_fb-index       = &3 .
      ls_search_elements_fb-label_for   = &4 .
      ls_search_elements_fb-label_ref   = &5 .
      ls_search_elements_fb-line        = &6 .
      ls_search_elements_fb-name        = &7 .
      ls_search_elements_fb-rb_group    = &8 .
      ls_search_elements_fb-type        = &9 .
    END-OF-DEFINITION.

    DEFINE insert_ris_md.
      ls_md-trobjtype        = to_upper( &1 ).
      ls_md-subtype         = to_upper( &2 ).
      ls_md-legacy_type     = to_upper( &3 ).
      INSERT ls_md INTO TABLE lt_metadata_fb.
    END-OF-DEFINITION.

@larshp
Copy link
Member

larshp commented Oct 9, 2021

yea

this is INSERT VALUE #( something something ) INTO TABLE lt_foobar.

which we cant use in 702 syntax

on long term I propose to develop abap2xlsx in 740 syntax, and then automatically downport it to 702, a prerequsite for this is using abaplint, which requires the macros to be refactored. After refactoring the macros, we can use the upport rules in abaplint to upport it to 740. But again, this is long term

sandraros pushed a commit that referenced this issue Oct 9, 2021
@sandraros
Copy link
Collaborator Author

Another point which makes a macro more easy than a method is IF &1 IS SUPPLIED. You may have a look at the draft PR I have just proposed, all macros are removed (NB: I didn't test at all so it's a draft), which shows an example with SUPPLIED. I think the macros in zcl_excel_worksheet are more easy to read than the methods. If you are okay to keep the methods, I'm fine with it.

sandraros pushed a commit that referenced this issue Oct 24, 2021
fix #844 to remove all macros
AndreaBorgia-Abo added a commit that referenced this issue Nov 1, 2021
* fix #844 to remove all macros 

* abaplint.json avoid_use/define false -> true

Co-authored-by: sandraros <sandra.rossi@gmail.com>
Co-authored-by: Abo <andrea@borgia.bo.it>
@AndreaBorgia-Abo
Copy link
Member

Reopened, then closed again after double-checking they're all gone, sorry for the noise.

@sandraros
Copy link
Collaborator Author

Is it a problem to reopen when needed? (and keep "fix" as text of a PR if it's a fix)

@AndreaBorgia-Abo
Copy link
Member

Is it a problem to reopen when needed? (and keep "fix" as text of a PR if it's a fix)

It's more of an inconvenience than a real problem, especially with long-running cleanups like the DDIC, since we can always reopen as needed.

More to the point, "fix" gives the idea that that specific commit actually solves the issue, whereas "WIP" clearly marks this as a partial solution toward the goal.

@sandraros
Copy link
Collaborator Author

Okay, fine, I'll keep it in mind. Agreed for "WIP".

Another solution is to always have a dedicated issue/enhancement per PR, even if its goal is to facilitate the fix of another issue. This way, "fix" can be kept. For instance, enhancement #524 is difficult to do without first refactoring change_cell_style with a dedicated PR, so I would create another enhancement to support that.

@AndreaBorgia-Abo
Copy link
Member

Yup, I agree that having a dedicated intermediate issue as stepping stone, where possible, is a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants