Sharing some advanced features of ABAP Code Inspector

Time:2022-11-25

ABAP Code Inspector is one of the must-have code inspection tools in every ABAP developer’s toolbox. Using the standard inspection items that come with ABAP Code Inspector can help developers quickly detect potential defects in ABAP code, improve code quality, and improve code running performance.

This article shares some experience in using ABAP Code Inspector that the author has accumulated in his work in ABAP application development for more than ten years.

In the Check Variant of ABAP Code Inspector, we can flexibly choose which type of scanning action to perform on the ABAP code according to our own needs.

Sharing some advanced features of ABAP Code Inspector

The following figure is an example, which means that all places in the ABAP code that have read and written access to the database table are executedTable Names from SELECT Statements scan. For the specific behavior of the scan, you can click the white exclamation mark picture on a blue background to get the help document. The option ticked in the example in the figure below means to check whether the Technical Settings in the ABAP field of SE11 is correctly maintained in the accessed database table, such as whether the cache type of the table is set correctly.

Sharing some advanced features of ABAP Code Inspector

performance check

Select-Statement can be transformed. X% of fields used – Check the usage of inner table fields

Suppose we useSELECT *Read data from a table to the ABAP internal table, and then only use A field in the subsequent code, and the read table has a total of B fields in SE11, then the result of dividing A by B is smaller, indicating The lower the usage rate of the read out internal table fields.

Sharing some advanced features of ABAP Code Inspector

In other words, you might consider replacing SELECT * with only the fields that SELECT really needs? You only need to maintain a minimum threshold in the above settings. When Code Inspector scans the code, once it detects that the usage rate is lower than the maintenance threshold It will report an error. The 20 in the above picture means 20%.

Search DB Operations in loops across modularization units

When Jerry first joined SAP in 2007 and began to learn ABAP programming, his predecessors warned me not to use SELECT statements in LOOP, which would greatly affect the performance of the code.

Sharing some advanced features of ABAP Code Inspector

The above picture is an example scanned by Code Inspector, using SELECT to read the database table CRMD_DPP_HI_BLCK in the double LOOP cycle.

Nested Loops — Detection of nested loops

Although when the number of loops of nested loops in the application code is small, it does not have much impact on the absolute time the code runs – yet writing code with at least exponential time complexity is not a good programming practice in any context .

Sharing some advanced features of ABAP Code Inspector

This setting can help us quickly find all nested loops.

Copy current table row for LOOP AT

Sharing some advanced features of ABAP Code Inspector

Find all LOOP AT … INTO places, theoretically these places can use LOOP AT … REFERENCE INTO or ASSIGNING<fs> replace. When there are many row structure fields in the inner table, some performance improvements can be obtained by using the latter two methods.

Low-Perform. Parameter Transfers – detects where all parameter transfers use “Pass by Value”

parameter passing in programming languagepass by referenceandPass by valueThe distinction between these two methods is still one of the interview questions for many Internet companies.

Sharing some advanced features of ABAP Code Inspector

This option allows you to specify which type of parameter to scan for the parameter passing method:

Sharing some advanced features of ABAP Code Inspector

In ABAP, theoretically, the method of passing by reference is used for parameter passing, and the performance is always better than that of passing by value. How much the specific performance will be improved depends on the specific type of parameter passed, and cannot be generalized.

Security Check – Dynamic and Client-Specific accesses in SELECT – Detection of dynamic SQL statements

Dynamic SQL that conforms to the following paradigms will be scanned:

  • Dynamic table accesses: SELECT * FROM (dbtab) WHERE …
  • Dynamic WHERE conditions: SELECT * FROM dbtab WHERE (where_cond)
  • Accesses to certain tables: SELECT * FROM dbtab WHERE …
  • Client-specific accesses: SELECT * FROM dbtab FROM WA … CLIENT SPECIFIED …

This option does not prohibit you from using dynamic SQL statements—in fact, there are a large number of examples of using dynamic SQL statements in the persistence layer of SAP applications—but reminds you not to forget the preventive measures for SQL injection: once scanned, if If user input is involved in the splicing of these dynamic SQL statements, don’t forget to check whether the context uses CL_ABAP_DYN_PRG to process user input.

Search for APPEND and INSERT … INDEX in SORTED Tables

Detects all occurrences of an APPEND operation on an ordered inner table. Having this scan option can help you avoid this type of runtime error on line 13 in the image below.

Sharing some advanced features of ABAP Code Inspector

Check of SY-SUBRC Handing – detection of system variable sy-subrc after ABAP keyword call

Jerry still clearly remembers that when he took the graduate course “Advanced Programming in UNIX Environment” more than ten years ago, the teacher kept emphasizing that after making a system call, the return value must be checked and corresponding error handling should be performed. In Jerry’s view, error detection and handling is the basic quality that every programmer should have.

Sharing some advanced features of ABAP Code Inspector

Corresponding to ABAP, it means that every time the keyword of ABAP is called to complete an operation, the value of sy-subrc must be checked to confirm whether the operation is successful.

Sharing some advanced features of ABAP Code Inspector

Of course, you can also tell Code Inspector to check only certain types of ABAP keyword calls according to the actual situation of the project. For example, the meaning of the above figure is to only check whether the sy-subrc has been checked after the READ TABLE keyword is called.

Missing table content check before calling SELECT … FOR ALL ENTRIES IN

When using FOR ALL ENTRIES IN<itab> Before, you must first check whether the internal table itab is empty. This option scans for code that is not written according to this specification.

Programming Conventions – Naming conventions

In this interface, set the naming conventions agreed in your team for different types of variables in ABAP, and then Code Inspector can scan all places in the code that violate these naming conventions.

Sharing some advanced features of ABAP Code Inspector
Sharing some advanced features of ABAP Code Inspector

Metrics and Statistics

The settings under this inspection category are some interesting statistics.

Sharing some advanced features of ABAP Code Inspector

Or give an example. The setting of the red area in the figure below means that if the number of executable statement lines in a method of a class exceeds 150 lines, Code Inspector will report a warning message. This is to prevent everyone from writing an overly verbose method.
The setting in the blue area is to report a warning message if the amount of comments corresponding to every 100 lines of executable code is less than 10 lines. These thresholds can be modified or closed according to the actual situation.

Sharing some advanced features of ABAP Code Inspector

FAN-OUT Structural Metrics – counts the fan-out value of a method

The fan-in value and fan-out value of a method are often mentioned in the context of modular programming. This pair of concepts is not the first in the programming world, but originated from the logic circuit design in the semiconductor industry:

Sharing some advanced features of ABAP Code Inspector

The fan-out coefficient of a logic gate defines the maximum number of digital signal inputs that the gate can drive, while the fan-out value of a code module represents the number of modules directly under it.

This option enables you to count the fanout factor of your method. The fan-out coefficient is too small, which means that the method basically does not call other lower-level functions. There are two possibilities:

(1) The logic of the method itself is very simple, only two or three lines of code, such as the setter/getter method of the class;
(2) The modularization of this method is not done well, and there is room for optimization. For example, some statements written directly in the method can be refined into lower-level functions and called in the method.

Comment Language Metrics

This option counts German commented functions that appear in the code.

Sharing some advanced features of ABAP Code Inspector

OO Size Metrics

This option is also created to prevent you from inadvertently creating a monster class: once the attributes of the class you care about exceed the set threshold, such as members of the class, public/private methods of the class, etc. When the value set in the option is exceeded, Code Inspector will alarm.

Sharing some advanced features of ABAP Code Inspector

Program Complexity Test – cyclomatic complexity – a test tool for code cyclomatic complexity (cyclomatic complexity)

This is another powerful tool that can help you write Clean ABAP code.

What is the cyclomatic complexity of the code?

According to the definition of Wikipedia, we draw the execution flow of a piece of code as a directed acyclic graph, and then the cyclomatic complexity can be calculated by the following formula:

https://en.wikipedia.org/wiki…

Cyclomatic complexity = number of edges in the graph – number of nodes in the graph + 2

Sharing some advanced features of ABAP Code Inspector

This is actually Euler’s theorem learned in our graduate course “Graph Theory”.
Consider the following example:

Sharing some advanced features of ABAP Code Inspector

The above 8 lines of ABAP code, the ring complexity is 3, how to calculate it?
First draw its corresponding directed acyclic graph:

Sharing some advanced features of ABAP Code Inspector

The number of sides in this graph is 3, that is, the three thick lines in black, red and green.

The number of vertices in this graph is 2, as shown by the blue legend of the two diamonds in the graph.

The final cyclomatic complexity is 3 – 2 + 2 = 3.

Statistics show that there is a strong positive correlation between the high cyclomatic complexity of the code and the high failure rate. Many, whether it is the original developer of the code or the maintainer who took over later, it will feel dizzy to read it.

Therefore, you can use the scanning option of ABAP Code Inspector to monitor the cyclomatic complexity of your code at any time.

Sharing some advanced features of ABAP Code Inspector

Search DB Operations

List all the SQL operation keywords that appear in the code that you care about.

Sharing some advanced features of ABAP Code Inspector

Search ABAP Statement Patterns

This option is also useful to scan the ABAP code according to the regular expression you specify.

Sharing some advanced features of ABAP Code Inspector

For example, if you want to find out all the occurrences of READ TABLE XXX WITH KEY X = X in the code, you only need to fill in the corresponding regular expression in the input box above, that is, use * to represent any string:

READ TABLE * WITH KEY * = *

Then ABAP Code Inspector will scan the code according to our expected behavior:

Sharing some advanced features of ABAP Code Inspector

The ABAP report RS_ABAP_SOURCE_SCAN can also achieve exactly the same function:

Sharing some advanced features of ABAP Code Inspector

Summarize

ABAP Code Inspector is one of the must-have code inspection tools in every ABAP developer’s toolbox. Using the standard inspection items that come with ABAP Code Inspector can help developers quickly detect potential defects in ABAP code, improve code quality, and improve code running performance.

This article shares some advanced functions that the author has used ABAP Code Inspector for many years, so as to meet the quality inspection, monitoring and report output functions of the author’s team in all dimensions of the delivered product code. I hope it can be helpful to everyone’s ABAP project development. .