ABAP 740

ABAP 740: Declaring and Creating variables

One of the differences with ABAP and Other Programming language is in ABAP we have been taught to declare the variables on top of the program or within a top include while other programs declare them just before they are being used.

However the developers tend to declare the data before using it in the program in order to give the better readability. The Objective here is to have a less data declaration at the top.

1.1 Omitting Declaration Using Type Pool

Type pools are required for the purpose of reusing the constants and global variables. Let’s say you create a huge program and you declare a bunch of global variables and constants and then you realize these variables declarations will be  used somewhere else. The only possible way to reuse the declared variables earlier was adding them in a Type Pool.

With Version 6.40 SAP introduced the possibility to create the global types in ABAP Dictionary with which we can declare global variables. But still type pools was required to support programming construct such as abap_true and abap_false statements.

With release 7.02 SAP Added these type pools as a global and now you no longer has to use the type pools declaration to have access to this variable.

But the General practice today is that you avoid using the type pool declarations or constructing global structures in ABAP Dictionary (SE11). Instead you can build a constant class(SE24) and within that class you can declare the global variables and constants with visibility as public. This way it can be used to declare variable by referencing the class attributes.

e.g Create a class zcl_demo_constants and declare internal table type tt_demo_table with visibility public. This can be used to create variables wherever required as below.

Now there is no need to bloat the repository objects with z types.

1.2 Omitting Data Type Declarations

The purpose of having to declare the data at the top or anywhere in the program is to let the compiler know what type of data it contains in order to perform the syntax check.

So if it is important the compiler knows the data type to perform its task, why not let the compiler decide instead of declaring it yourself, this will save a lot of Codes.

In this case, the data type for the variable is determined as String as the string value is assigned.

This case declares a variable lv_lines of type integer as the outcome of lines(lt_table) is an integer.

1.3 Create Object using new

ABAP run time environment has been adapting to quite a lot of features from Eclipse, in Same way the ABAP programming language has been adapting to programming features with that of JAVA.

In JAVA/JAVASCRIPT we have a keyword called new which is used for the purpose of declaring the objects.

Now we can simplify the object creation using the keyword new.

This can be re-written as below.

Quite simple, isn’t it. Not just the object, you can also use it for the data declarations as well.

data: lv_matnr type matnr.  “matnr is a data element referenced for declaring variable. e.g

This creates a variable of type matnr and assigns value 0 to it.

1.4 Filling structures and internal tables while creating using value keyword.

You should be familiar with the below statement.

The value keyword assigns value to the lv_name variable at the time of declaration. So far we have been only able to assign initial value to elementary data type.

With 7.4, value keyword has come of age and now you can use it for the purpose of initializing your structures as well as internal tables as well.

1.5 Filling internal tables from other tables using For

In previous section we read about the for statement, where we can use the value statement to fill the internal table. e.g

This fills up three rows of value in the lt_mara table. But ideally when you are writing code you don’t always hard code the values from the internal table, you either load it from database table or you fill it from another internal table. The possibility that we have while moving data from one internal table to another internal table is to have the same structure.e.g

With ABAP 7.4, we have the FOR Command, which can move the data between two internal tables in an elegant way, even with different structures. We can also restrict the data that is being moved.

Let’s assume i have an internal table, lt_all_content which has all the records. I only want to move a limited record to another internal table.

1.6. Creating short lived variables.

In 7.40 version, SAP has introduced a keyword called LET which is used for the purpose of declaring the variables which is short lived. For those who are familiar with javascript can easily understand what this is.

These variables are short lived while the variable created using the keyword data live throughout your program lifetime. This variable can be used only with the constructor expression. For example, value keyword discussed above is an constructor expression.

The variable a exists only within this value statement.

Comment here