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

Template:

  • /LRN/CL_S4D401_ATT_TEMPLATE (Global Class)

Solution:

  • none

Prerequisites

If you want to do the exercises as described in this learning journey, you need access to a dedicated hands-on practice system. If you already booked a system for the first part of the learning journey, 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 service key, 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 ZLOCAL (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 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. Select Use a Service Key and choose Next.

    6. Choose Import and select the file containing the service key that you have been given.

    7. Choose Open Logon Page in Browser.

    8. On the browser page that opens, choose directaccess.accounts.ondemand.com and log on with the user and password that you have been given.

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

    10. 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 and then sub node Favorite Packages.

    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
    SuperpackageZLOCAL
    Package TypeDevelopment
    Software ComponentZLOCAL
    Application ComponentLeave this field blank
    Transport LayerLeave this field blank
    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 ZLOCAL.

    6. Ensure that the Package Type is set to Development.

    7. Choose Next.

    8. Ensure the Software component is set to ZLOCAL.

    9. Ensure that Application component is empty.

    10. Ensure that Transport layer is empty.

    11. Choose Next.

    12. 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, ABAP Exercises.

    13. 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_##.

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 the name ZCL_##_SOLUTION, where ## stands for your group number.

    6. Adjust the description and choose Next.

    7. Confirm the transport request and choose Finish.

  2. Analyze the code.

    1. Use the ABAP Language Help (F1)and the Code Element Information (F2) to get information on statements and code elements.

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

    1. Press Ctrl + F3 to activate the class.

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

      Hint

      Be patient, it takes the application several seconds to run. (Note the green progress indicator in the lower right corner of the eclipse window). Performance is one of the things we are going to improve during this course.

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 selected 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. Choose 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. Choose check Extended Program Check (SLIN).

    1. In the Checks view on the left, expand node Syntax and activation.

    2. Set check Extended Program Check (SLIN) as selected.

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

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

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

    3. Edit the source code on the Parameters tab, and change the value of the StandardOrCustom parameter from 1 to 2.

    4. Scroll down the list, and set the value of all other parameters to true.

  4. Activate the 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 during 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 tab below the editor view to display the check result.

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

    Hint

    You know that if the code was correct, attributes SEATS_MAX, SEATS_OCC, and SEATS_FREE would be used in the same places.
    1. Find the warning that reads 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 + Space + 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. On the where-used list, double-click the APPEND statement that includes literal text Occupied Seats: to navigate to the statement.

    6. In this append statement, replace SEATS_MAX with SEATS_OCC to fix the issue.

  3. Analyze the result and fix the warning related to 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 that reads No reads performed on field CURRENCY_CODE.

    3. Choose the warning to display the warning details.

    4. On the Details area 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. Place the cursor on the CURRENCY_CODE attribute and choose Get Where-Used List from the toolbar. Alternatively, you can press Ctrl + Space + G.

    7. Adjust the DATA statement as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      1
      DATA currency_code TYPE /dmo/currency_code .
  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.

Log in to track your progress & complete quizzes