2

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?

2 Answers 2

6

There are a variety of techniques - I prefer to code almost everything except for the basic selection screen handling in a separate controller class. The report simply defers to that class and calls its methods. Other than that - it's just a warning that you can ignore if you know what you're doing. Writing a program without any global variable at all will certainly not be practical - however, you should think at least twice before using global variables or attributes in a place where a method parameter would be more appropriate.

1
  • Thank you. Since this I've been using ABAP Objects wherever possible. Also, thanks for getting IS INSTANCE OF implemented in 7.50! :)
    – gkubed
    Nov 4, 2015 at 17:28
4

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).

4
  • Thank you. You're certainly right about too few ABAPers caring - but someone must lead by example. I have a copy of Introduction to ABAP Programming for SAP, Third Edition by Cengage Learning in front of me, and the example program ZCHECK on page 294 contains only global variables. Do you know of any good resources to helpful to an ABAPer trying to adhere to programming standards?
    – gkubed
    Jul 22, 2015 at 12:33
  • 2
    I think you're on the right track using the Extended Program Check (SLIN). If you're looking for something to read, Official ABAP Programming Guidelines is an excellent reference.
    – mjturner
    Jul 22, 2015 at 13:07
  • 2
    @mjturner I read in documentation that subroutines are obsolete. Jul 23, 2015 at 14:34
  • 2
    @Christian Yes, you're right - I believe that as of 7.02 FORM is considered an obsolete modularisation technique. I'll update my answer to expand on that a little bit.
    – mjturner
    Jul 23, 2015 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.