4 experiences with FINAL in ABAP

Published on

in

Reading time:

2–3 minutes

With the declaration operator FINAL, you can create immutable variables in ABAP. This means that a variable is initialized at the same time as it is declared, but it is given a value that cannot be changed elsewhere in the source code. This has advantages, but can also be confusing. Below are four experiences with the FINAL operator.

#1: Significantly less cognitive load

Every developer reads vast amounts of source code every day and needs to understand what it is about as quickly and easily as possible. FINAL demonstrates its full strength here, because when using the operator, it becomes clear that the variable created by it only receives a value at this exact location. The value cannot be changed afterwards.

This is in stark contrast to the declaration operator DATA or the ABAP statement DATA. Variables created in this way can change their values at many locations. Accordingly, such variables require a completely different level of attention when reading source code than variables created with FINAL.

In this context, less attention equates to less cognitive load. Accordingly, we recommend using FINAL whenever possible.

#2: Limitations in the Debugger

Those who use FINAL when writing their source code (design time) may experience a surprise in the debugger (run time). Changing variables created with FINAL is not possible in the debugger. This is in contrast to variables created with DATA, which can be changed in the debugger, provided they have the appropriate permissions.

#3: Be careful when working with objects

In ABAP, objects are accessed via object reference variables. Such a variable can also be created with FINAL. However, this does not mean that the state of the object (if it has one) accessed through the variable cannot change. It is therefore not an immutable object.

The object reference variable only contains the address of the object, it is a pointer. If this pointer is created with FINAL, the address cannot change. The object accessible via the pointer follows a different lifecycle during runtime.

#4: Confusion with Loops

FINAL can also be used in loops. An example might look like this:

LOOP AT items INTO FINAL(item).  
..
ENDLOOP.

The example clearly shows that in each loop iteration, the variable “item” is filled with the contents of the current row of the internal table “items.” “Item” is immutable.

At first glance, this usage seems to contradict the functionality of FINAL. Repeatedly using the same immutable variable “item” with different contents seems incorrect. To resolve this contradiction, one must take a closer look at the help for FINAL.

According to the help, the position at which FINAL creates and initializes a variable is essential for FINAL. This position is always the same in the loop example, which is why the example ultimately fulfills the basic requirement of FINAL.

Summary

FINAL is a helpful enhancement in the ABAP programming language. Its use makes source code more understandable even when reading it, which is entirely in keeping with the spirit of CleanABAP. At the same time, its use has several consequences that can be easily assessed through experience. This allows you to quickly determine whether FINAL or DATA is better.


Thank you for reading. If you enjoyed the post, please share the article with your community. Thanks in advance.

Michael