Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Encapsulation in Case Statement

Tags:

abap

While modifying an existing program's CASE statement, I had to add a second block where some logic is repeated to set NetWeaver portal settings. This is done by setting values in a local variable, then assigning that variable to a Changing parameter. I copied over the code and did a Pretty Print, expecting to compiler to complain about the unknown variable. To my surprise however, this code actually compiles just fine:

CASE i_actionid.
    WHEN 'DOMIGO'.
        DATA:   ls_portal_actions  TYPE powl_follow_up_sty.
        CLEAR ls_portal_actions.
        ls_portal_actions-bo_system = 'SAP_ECC_Common'.
        " [...]
        c_portal_actions = ls_portal_actions.
    WHEN 'EBELN'.
        ls_portal_actions-bo_system = 'SAP_ECC_Common'.
        " [...]
        C_PORTAL_ACTIONS = ls_portal_actions.
ENDCASE.

As I have seen in every other programming language, the DATA: declaration in the first WHEN statement should be encapsulated and available only inside that switch block. Does SAP ignore this encapsulation to make that value available in the entire CASE statement? Is this documented anywhere?

Note that this code compiles just fine and double-clicking the local variable in the second switch takes me to the data declaration in the first. I have however not been able to test that this code executes properly as our testing environment is down.

like image 787
Lilienthal Avatar asked Oct 30 '13 08:10

Lilienthal


2 Answers

In short you cannot do this. You will have the following scopes in an abap program within which to declare variables (from local to global):

  • Form routine: all variables between FORM and ENDFORM
  • Method: all variables between METHOD and ENDMETHOD
  • Class - all variables between CLASS and ENDCLASS but only in the CLASS DEFINITION section
  • Function module: all variables between FUNCTION and ENDFUNCTION
  • Program/global - anything not in one of the above is global in the current program including variables in PBO and PAI modules

Having the ability to define variables locally in a for loop or if is really useful but unfortunately not possible in ABAP. The closest you will come to publicly available documentation on this is on help.sap.com: Local Data in the Subroutine

As for the compile process do not assume that ABAP will optimize out any variables you do not use it won't, use the code inspector to find and remove them yourself. Since ABAP works the way it does I personally define all my variables at the start of a modularization unit and not inline with other code and have gone so far as to modify the pretty printer to move any inline definitions to the top of the current scope.

like image 180
DW8Reaper Avatar answered Oct 20 '22 20:10

DW8Reaper


Your assumption that a CASE statement defines its own scope of variables in ABAP is simply wrong (and would be wrong for a number of other programming languages as well). It's a bad idea to litter your code with variable declarations because that makes it awfully hard to read and to maintain, but it is possible. The DATA statements - as well as many other declarative statements - are only evaluated at compile time and are completely ignored at runtime. You can find more information about the scopes in the online documentation.

like image 40
vwegert Avatar answered Oct 20 '22 19:10

vwegert