Improving Code Quality using ABAP Test Cockpit

Objectives

After completing this lesson, you will be able to:
  • Create ATC check variants.
  • Perform static code checks with ATC.

Static Code Checks

Syntax Check - Extended Check - ABAP Test Cockpit

ABAP Development has several tools to perform static code checks.

Syntax Check

Before you can activate ABAP code, a Syntax Check is mandatory. If you do not perform the syntax check manually, the development environment performs it implicitly during activation. If there are any syntax errors, activation is not possible. Originally, the ABAP syntax check only reported syntax errors. Nowadays, the syntax check also issues syntax warnings. The main difference between errors and warnings is the fact that warnings do not prevent activation. Despite that, you should not ignore warnings lightly. Doing so can lead to serious problems, including performance issues, data inconsistencies, and even runtime errors.

Extended Check

Since quite early in the history of ABAP, optional source code checks complemented the mandatory syntax checks. The Extended Check is not related to activation and has no influence on it. Developers decide freely whether they want to invoke the extended check and how they treat its findings.

Note

In ABAP development tools (ADT), extended check is not a tool of its own. It is only available as part of the ABAP Test Cockpit.

ABAP Test Cockpit

When Extended Check was found to be too restricted and inflexible for modern ABAP development, it was replaced by the ABAP Test Cockpit (ATC). ATC offers a large variety of checks ranging from performance checks and security checks, to checks for adherence, to guidelines and programming conventions. To offer the capabilities of Extended Check in the new check tool too, it is integrated into ATC as one of the many checks.

ATC Check Variants

ATC Check Variants

Each execution of an ATC check is controlled by an ATC Check Variant. The check variant determines which checks are performed and provides values for the parameters of the checks.

When you perform an ATC check run, you can either use the global default check variant of your system or you can specify the check variant that you want to use. This can be an existing check variant or a check variant that you created yourself.

From a technical point of view, ATC check variants are ABAP development objects. They belong to development packages and you can transport them to other systems.

How to Create an ATC Check Variant

In this demonstration, we create an ATC Check variant to specify the checks we want to perform.

How to Run an ATC Check and Analyze the Result

In this demonstration, you will see how to execute an ATC check run on an ABAP class and analyze the result.

Pseudo Comments and Pragmas

Pseudo Comments and Pragmas

Not all ATC findings are critical and you might decide to ignore them. Some others might turn out to be false alarms at a closer look. An example for such a false alarm is a dynamic usage of a variable, which is invisible for the static code check.

ATC has two techniques to suppress findings:

  • Pseudo Comments

    You can suppress certain ATC findings by adding a specific comment at the end of the source code line with the finding. In the example, the pseudo comment "#EC NEEDED is used. EC stands for Extended Check in this case.

    Like any other comment, a pseudo comment is not visible for the ABAP syntax check and the ABAP compiler. We call it a pseudo comment, because it has an influence on the Extended Check and ATC.

  • Pragmas

    Other findings you can suppress by adding a certain keyword starting with ## to the ABAP statement. These keywords are called Pragmas. It is very important that pragmas are placed inside the ABAP statement, not after it. In the example, pragma ##needed is used, and it is placed before the period sign that ends the DATA statement.

Note

Because pragmas are visible to the syntax check, they are also used to suppress certain warnings from the syntax check. This is not possible with pseudo comments.

If you want to know whether you can suppress a given ATC finding, refer to the finding details on the ATC Problems view. Here, you also find the information on which technique you should use.

Copy and Understand a Template Class

You want to improve your ABAP programming skills. You prepare the ABAP development environment, create a new package and copy a template class.

Template:

  • /LRN/CL_S4D401_ATT_TEMPLATE (Global Class)

Solution:

  • none

Prerequisites

If you want to do the exercises as described in this course, you need access to a dedicated hands-on practice system. If you already booked a system for the first part of the learning journey Acquiring Core ABAP Skills, you can keep using this system. If you did not yet book a hands-on practice system, follow this link for details: https://learning.sap.com/practice-systems/intermediate-abap-programming.

In addition to the ABAP Service Instance URL, which you will receive during the booking process, you need a local installation of the Eclipse IDE with the ABAP Development Tools plug-in installed on top. Follow this link for a step-by-step instruction https://developers.sap.com/tutorials/abap-install-adt.html.

Task 1: Preparations

In Eclipse, create an ABAP Cloud project and add package /LRN/S4D401_EXERCISE to the list of favorite packages. In the ABAP Cloud project, create a new package under the superpackage ZSTUDENTS (suggested name: ZS4D401_##, where ## is your group number) and add it also to the favorite packages.

Steps

  1. Open Eclipse, switch to the ABAP perspective and create an ABAP Cloud project.

    Note

    If you just finished the Learning Basic ABAP Programming course and already have your ABAP Cloud project open in Eclipse, you can skip this step.
    1. Open Eclipse and close all tabs.

    2. Choose WindowPerspectiveOpen PerspectiveOther....

    3. In the dialog box, double-click ABAP.

    4. Choose FileNewABAP Cloud Project.

    5. Proceed as follows on the New ABAP Cloud Project dialog that appears:

      If you have been given the ABAP Service Instance URL:

      1. Copy and paste this URL in the corresponding field on the dialog.
      2. Choose Next.

      If you have been given a file containing the Service Key:

      1. Select the Extract link on the dialog.
      2. Choose Import... and select the file with the Service Key.
      3. After importing click Copy to Clipboard to copy the Service Instance URL.
      4. Choose Close.
      5. In the top of the popup box in the ABAP Service Instance URL:* field, paste the ABAP Service Instance URL with the key combination Ctrl + V.
      6. Choose Next.
    6. Choose Open Logon Page in Browser.

    7. You will be prompted to logon to the system.

      Enter the user and password that you used to register to SAP Learning Hub.

    8. When you see the message, You have been successfully logged on, close the browser window and return to Eclipse.

    9. To finish creating the project, choose Finish.

  2. Add package /LRN/S4D401_EXERCISE to the list of Favorite Packages.

    1. In the Project Explorer on the left, expand your ABAP Cloud project.

    2. Right-click Favorite Packages and choose Add Package....

    3. In the search field, enter /LRN/S4D401.

    4. From the list of matching items, select /LRN/S4D401_EXERCISE, then choose OK.

  3. In your ABAP Cloud project, create a new package with the following attributes. When you are prompted for a transport choose the transport request in which you are involved. If no transport request is listed, create a new request.

    AttributeValue
    NameZS4D401_##, where ## is your group number
    DescriptionIntermediate ABAP Programming
    Add to favorite packagesChecked
    SuperpackageZSTUDENTS
    1. In the Project Explorer, right-click on your ABAP Cloud project and choose NewABAP Package.

    2. Enter the package name ZS4D401_## where ## is your group number.

    3. Enter the description Intermediate ABAP Programming.

    4. Mark the checkbox Add to favorite packages.

    5. Enter the superpackage ZSTUDENTS.

    6. Choose Next.

    7. Choose Next.

    8. Check if there is a transport request listed under option Choose from requests in which I am involved. If this is the case, choose this option. If the list is empty, choose the option Create a new request and enter a request description, for example, Exercises S4D401 - Student ## (where ## is your group number).

    9. Choose Finish.

Task 2: Copy and Analyze Template

Create a copy of global class /LRN/CL_S4D401_ATT_TEMPLATE. Name the copy ZCL_##_SOLUTION and add it to your package ZS4D401_##.

Hint

When copying the template class, the original description, which is Template - Starting Point, is also copied. If you want to change this description for the copied class, you can do so via the Properties view. To do this, select the General tab in this view and adjust the content of the Description field accordingly. Don't forget to save your changes.

Steps

  1. Copy the class /LRN/CL_S4D401_ATT_TEMPLATE to a class in your own package (suggested name: ZCL_##_SOLUTION, where ## stands for your group number).

    1. Open the source code of the global class /LRN/CL_S4D401_ATT_TEMPLATE.

    2. Link the Project Explorer view with the editor.

    3. In the Project Explorer view, right-click the class /LRN/CL_S4D401_ATT_TEMPLATE to open the context menu.

    4. From the context menu, choose Duplicate....

    5. Enter the name of your package in the Package field. In the Name field, enter ZCL_##_SOLUTION, where ## stands for your group number.

    6. Choose Next.

    7. Confirm the transport request and choose Finish.

  2. Analyze the code.

    Hint

    • Use the ABAP Language Help (F1)and the Code Element Information (F2) to get information on statements and code elements.
    • Navigate to the definition of a code element, if necessary (F3 or Strg + Left-click).
  3. Activate and execute the class as a console app.

    Hint

    Please be patient, the application needs some time to run. (Note the green progress indicator in the bottom right corner of the eclipse window.) Performance is one of the things we are going to improve during this course.
    1. Press Ctrl + F3 to activate the class.

    2. Press F9 to execute the class as console app.

Perform a Code Analysis Using the ABAP Test Cockpit

You notice that something is not right with the output of your code. In particular, the console displays the same number for maximum seats and occupied seats although the number of free seats is larger than zero. You create a check variant for the ABAP Test Cockpit and use it to analyze your code.

Template:

  • /LRN/CL_S4D401_ATT_TEMPLATE (Global Class)

Solution:

  • /LRN/S4D401_ATS_VARIANT (Check Variant)
  • /LRN/CL_S4D401_ATS_CHECKED (Global Class)

Task 1: Create a Check Variant

In your package, create a new check variant (suggested name: Z##_VARIANT, where ## is your group number). Configure the check variant to perform all checks of category Extended Program Check (SLIN).

Hint

By default, Extended Program Check (SLIN) only performs a subset of the available checks.

Steps

  1. Create a new ATC check variant.

    1. In the Project Explorer on the left, right click your package and choose NewOther ABAP Repository Object.

    2. In the filter field, type atc.

    3. Select ATC Check Variant and choose Next.

    4. Enter Z##_VARIANT for Name and Extended Program Check for Description, and choose Next.

    5. Confirm your transport request and choose Finish.

  2. Include the ATC check Extended Program Check (SLIN) in your ATC check variant.

    1. In the ATC Checks section on the left of your ATC check variant, expand node Syntax and activation.

    2. Select Extended Program Check (SLIN).

  3. Edit the parameters of the ATC check Extended Program Check (SLIN). Set the value of parameter StandardOrCustom to 2 and ensure all other parameters have the value true.

    1. Left-click on Extended Program Check (SLIN) to open this ATC check in the ATC Check Details section on the right.

    2. In the ATC Check Details section, choose the Parameters tab to display the parameters for this check.

    3. Change the value of the StandardOrCustom parameter from 1 to 2.

    4. Scroll down the list, and, where necessary, change the value of the remaining parameters from false to true.

  4. Activate your check variant.

    1. Press Ctrl + F3 to activate the check variant.

Task 2: Improve Code

Using your check variant Z##_VARIANT, perform a static code check of class ZCL_##_SOLUTION, which you created in the previous exercise. Analyze the findings and make corrections to the code where necessary.

Steps

  1. Perform a check of class ZCL_##_SOLUTION using your check variant Z##_VARIANT.

    1. Right-click anywhere in the source code of your class, and choose Run AsABAP Test Cockpit With....

    2. Enter the name of your check variant (Z##_VARIANT) and choose OK.

      Note

      The check is performed asynchronously. Wait for the green bar in the lower right corner to disappear.
    3. Choose the ATC Problems view below the ABAP editor to display the check result.

  2. Analyze the result and fix the warning related to the SEATS_OCC field.

    Hint

    You know that if the code was correct, the attributes SEATS_MAX, SEATS_OCC, and SEATS_FREE would be used in the same place.
    1. Find the warning No reads performed on field SEATS_OCC.

    2. Double-click on the warning to navigate to the source code.

    3. Place the cursor on attribute SEATS_MAX and choose Get Where-Used List from the toolbar. Alternatively, you can press Ctrl + Shift + G.

    4. From the where-used list, you can see that one of the APPEND statements falsely uses the SEATS_MAX attribute instead of the SEATS_OCC attribute.

    5. Double-click the match APPEND |Occupied Seats: { seats_max }| TO r_result. in the where-used list to navigate to the corresponding statement.

    6. Replace SEATS_MAX with SEATS_OCC in the APPEND statement to fix the issue.

  3. Fix the finding related to the CURRENCY_CODE field.

    Hint

    You know that the CURRENCY_CODE attribute is not used yet, but you want to keep it for future developments. Therefore, you hide this warning using a pragma.
    1. Return to the ATC Problems view.

    2. Find the warning No reads performed on field CURRENCY_CODE.

    3. Choose the warning to display the warning details.

    4. In the Details section on the right, find the sentence starting with Finding can be suppressed with....

    5. Double-click the warning to navigate to the source code.

    6. Adjust the DATA statement as follows:

      Code Snippet
      1
      DATA currency_code TYPE /dmo/currency_code ##NEEDED.
  4. Activate your class and repeat the code check to verify that the two warnings have gone.

    Note

    For the moment, we will ignore the other findings. They will be covered in later sections of this course.
    1. Press Ctrl + F3 to activate the class.

    2. Return to the ATC Problems view.

    3. Right-click any of the findings to open the context menu.

    4. From the context menu, choose Recheck.