Search code examples
global-variablesabap

Localizing global variables


When using the Extended Program Check, I get the following warning:

Do not declare fields and field symbols (variable name) globally.

This is from declaring global data before the selection screen. The obvious solution is that they should be declared locally in a subroutine.

If I decide to do this, the data will now be out of scope for the other subroutines, so I would end up creating something to the effect of a main() function from C or Java. This sounds like a good idea - however, events such as INITIALIZATION are not allowed to be inside of subroutines, meaning that it forces a break in scope.

Observe the sample program below:

REPORT Z_EXAMPLE.
SELECTION-SCREEN BEGIN OF BLOCK upload WITH FRAME TITLE text-H01.
  PARAMETERS: p_infile TYPE rlgrap-filename LOWER CASE OBLIGATORY.
SELECTION-SCREEN END OF BLOCK upload.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_infile.
  PERFORM main1 CHANGING p_infile.
INITIALIZATION.
  PERFORM main2.
TOP-OF-PAGE.
  PERFORM main3.
...

main1, main2, and main3 cannot to my knowledge pass any data to one another without global declaration. If the data is parsed from the uploaded file p_infile in main1, it cannot be accessed in main2 or main3. Aside from omitting events all together, is there any way to abide by the warning but let data be passed over events?


Solution

  • As @vwegert so rightly said, it's almost impossible to write an ABAP program that doesn't have at least a few global variables (the selection screen and events enforce that, unfortunately).

    One approach is to use a controller class, another is to have a main subroutine and have it call other subroutines as required, passing values as required. I tend to favour the latter approach in a lot of cases, if only because it's easier to split the subroutines into logical groupings in separate includes (doing so with classes can sometimes be a little ugly). It really is a matter of approach though, but the key thing is reducing global variables to a minimum - unfortunately too few ABAP developers that I've encountered care about such issues.

    Update

    @Christian has reminded me that as of ABAP AS 7.02, subroutines are considered obsolete:

    Subroutines should no longer be created in new programs for the following reasons:

    • The parameter interface has clear weaknesses when compared with the parameter interface of methods, such as:

      • positional parameters instead of keyword parameters
      • no genuine input parameters in pass by reference
      • typing is optional
      • no optional parameters
    • Every subroutine implicitly belongs to the public interface of its program. Generally this is not desirable.

    • Calling subroutines externally is critical with regard to the assignment of the container program to a program group in the internal session. This assignment cannot generally be defined as static.

    Those are all valid points and I think in light of that, using classes for modularisation is definitely the preferred approach (and from a purely aesthetic point of view, they also "fit" better with the syntax enhancements in 7.02 and later).