Typically, I wanted to name this post as part II of our previous post “Are you an ABAP coder or a programmer?“. But later I felt, ‘Back to Basics‘ made more sense.

At the end of this post, there is a 2 liner code with a quiz. If you could spare one minute to leave a comment with your answer, it would highly motivate us. (Optional, just a request. You need not leave the answer, just enjoy the post).. 🙂

There are bunch of new areas budding in the field of SAP ABAP. Fiori, UI5, ABAP on HANA etc are knocking and virtually trying to make ABAPers old schoolers. But in this age of competition and evolution, do we really have our basics correct?

[adToAppearHere]

I would not be writing anything new in this post. But I want to jolt our young ABAPers with some examples and try to put some points. Below examples are readily available in SAP’s Documentation but I am still quoting them here because, these days we believe in asking Google more than searching in SAP’s Documentations. Hope, some day this post would line up in the first page of the Google search and help the needful readers. 🙂

Point 1

1 2 3 4 5 6 7 8 9 * Declaration 1 DATA bound _ 1 TYPE p LENGTH 6 DECIMALS 2. DATA bound _ 2 TYPE p LENGTH 6 DECIMALS 2. * Declaration 2 TYPES num _ type TYPE p LENGTH 6 DECIMALS 2. DATA : num _ 1 TYPE num _ type , num _ 2 TYPE num _ type .

Question 1: Do you know what type of data object are those in Declaration 1 & 2?

Question 2: Which one is the recommended way and why?

Answer 1:

Bound_1 and bound_2 are BOUND data types. It is created if a data object is defined in the DATA statement. Num_type is STANDALONE data type. They are defined using the TYPES statement .

We have been using it for years, did you know these formal terms? 🙂

Answer 2:

Most of us know, second way is the better way. Use standalone data types instead of constructing bound data types when declaring data objects. Why?

a) A bound data type only exists as a property of that single data object. If this type is needed in several different places, it has to be defined separately for each place where it is used.

b) The declaration of a standalone data type allows multiple data objects (or interface parameters or field symbols) to use a type without the need to always redefine this type.

c) Even if only one data object of this type is required initially, it is very likely that further data objects will be added during the course of the development. If the type needs to be adapted later on, you can do this centrally.

d) Declaring a standalone type and using it to declare a data object means following the SoC (Separation of Concerns) principle.

Point 2

In the above example we stressed on reusability of the STANDALONE Type. Does the same rule apply for the newly released Inline declarations? Lets check.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 * Declaration 1 DATA : i _ tab1 TYPE STANDARD TABLE OF string , i _ tab2 TYPE STANDARD TABLE OF string . LOOP AT i _ tab1 ASSIGNING FIELD - SYMBOL ( < fs > ) . * Do something ENDLOOP . LOOP AT i _ tab2 ASSIGNING < fs > . * Do something ENDLOOP . * Declaration 2 DATA : i _ tab1 TYPE STANDARD TABLE OF string , i _ tab2 TYPE STANDARD TABLE OF string . LOOP AT i _ tab1 ASSIGNING FIELD - SYMBOL ( < fs1 > ) . * Do something ENDLOOP . LOOP AT i _ tab2 ASSIGNING FIELD - SYMBOL ( < fs2 > ) . * Do something ENDLOOP .

Going by the reusabilty fundamentals, our logical mind says, Declaration 1 is better as we have only one data to take care of two tables. But SAP says, Declaration 2 is better. Why?

Answer: The rule dictating that no global program variables and field symbols are to be declared also applies to inline declarations, without restrictions. The declaration operators can be used to make inline declarations in writer positions. In this way, declarations are made in operational statements rather than in declaration statements. The declaration operators are compiled only in executable statements.

SAP suggests to use declaration operators as if they were local declarations in the current statement block. The fact that they are valid in the whole method is ignored, for the sake of simplicity. If the field symbol and the variables are only to be declared once for both loops, they should be declared at the start of the method using declaration statements (and not as inline declaration operators). Using one inline operator in two different logical processing blocks negate the very purpose of inline operators.

Point 3

Should you be contented with technically correct statements? Or are the semantically correct statements better?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 * Declaration 1 DATA is _ initial TYPE SYST _ MSGTY . IF is _ initial IS INITIAL . * Do something ENDIF . * Declaration 2 DATA is _ initial TYPE abap _ bool . IF is _ initial EQ abap _ false . * Do something ENDIF .

Technically both the above declarations are correct and do not have any performance impact. But declaration 2 is better because SAP recommends to use semantically appropriate data types only. After all SYST_MSGTY in first declaration is for system field sy-msgty which indicates the message type but the declared variable IS_INITIAL, whose name and use clearly indicate that it is used for a truth value.

The technical properties of a type alone do not justify its use in a specific context, as this impedes the readability of the program. For this reason, we must use only data types whose semantics match the usage.

Point 4

Use the Data Type abap_bool for Logic Values. Using the type abap_bool and the constants abap_true and abap_false makes it clear that logic values are being used.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 * Declaration 1 DATA is _ found TYPE c LENGTH 1. is _ found = 'X' . IF is _ found IS NOT INITIAL . * Do something ENDIF . * Declaration 2 DATA is _ found TYPE abap _ bool . is _ found = abap _ true . IF is _ found = abap _ true . * Do something ENDIF

Declaration 2 and usage makes it semantically and logically legible.

Point 5

1 2 3 4 5 6 7 8 * Usage of literal 1 v _ circumference = 2 * '3.141592653589793238462643383279503' * v _ radius . * Usage of literal 2 CONSTANTS pi TYPE decfloat34 VALUE '3.141592653589793238462643383279503' . v _ circumference = 2 * pi * v _ radius .

Avoid using literals in operand positions. Period!!

Trivia:

Text field literals are enclosed in single speech marks (‘). The data type is c.

String literals are enclosed in back quotes (`). The data type is string.

Point 6

Did you know that ‘Strings’ are dynamic data objects of variable length. There are text strings of the string data type and byte strings of the xstring data type.

The speciality of String data type is that length of strings automatically adapts to the content where as the text and byte fields of a fixed length (c, x data types) are fixed.

What does it mean?

It means, fixed length text and byte fields of type (c, x) consume the fixed memory. Strings are more flexible than fields of a fixed length and usually help us save memory space, because no unnecessary space is occupied by blanks or zeros.

exception: If it is obvious that a certain length is never exceeded, you can also use short fields of a fixed length.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 * Declaration 1 TYPES html _ line TYPE c LENGTH 255. DATA html _ table TYPE TABLE OF html _ line . APPEND '<HTML>' TO html _ table . APPEND '<BODY>' TO html _ table . APPEND '</BODY>' TO html _ table . APPEND '</HTML>' TO html _ table . * Declaration 2 DATA html _ table TYPE TABLE OF string . APPEND `<HTML>` TO html _ table . APPEND `<BODY>` TO html _ table . APPEND `</BODY>` TO html _ table . APPEND `</HTML>` TO html _ table .

Instead of fixed 255 characters, dynamic string is always a better alternative.

Point 7.

Food for thought. What would be the output of the below write statement?

1 2 3 CONSTANTS v _ time TYPE t VALUE 003661. write : / v _ time .

May I request you to please write what you think would be the output in the comment section of this post and then put these two lines of code in your system and know the correct answer. Please note: We are not trying to test any ones’ knowledge. It is just for fun. 🙂

I am sure, you would be blown away with the output. Please share your thoughts on what happened and why for the above 2 lines of code.

If you want to get notification about the newest posts, please subscribe. Your email is safe with us.

If you liked it, please share it! Thanks!

Image source: via Free Stock Photos foter.com