Back to Basics

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

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.

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?

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.

Declaration 2 and usage makes it semantically and logically legible.

Point 5

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.

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?

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

Share on Facebook22Share on LinkedIn3Tweet about this on TwitterShare on Google+3Email this to someonePin on Pinterest0Share on Reddit8
Isn't it Fair to Share??

14 Comments on "Back to Basics"

  1. Soumyadeep Basu | June 29, 2015 at 12:25 pm |

    Really blown away by the output….never used type t in my long career and thought I have worked on many things :-)…..frankly speaking I thought the input is wrong as 61 cannot be a second value 🙂 good blog!!

    • Thanks Soumyadeep!!

      Glad that you liked it. True, SAP is an ocean.. The deeper you dive, the more you are amazed.. 🙂

      Regards,
      Raju.

  2. Steve Oldner | June 29, 2015 at 2:32 pm |

    Never used t. Didn’t expect the result! I’m going to pass this one around!

    Thanks!

  3. This is very well structured document. Thanks Raju

  4. Tuhin Bhaduri | June 30, 2015 at 5:40 pm |

    This is an excellent and unique SAP ABAP blog I have ever seen. Raju I liked it very much.

  5. Saket Suman | July 25, 2015 at 8:15 am |

    Wow!! What an article… Revisited the basics with a new outlook.
    Keep up the good work, would certainly look forward for more of them.

    • Dear Saket – Thank you so much for your appreciation. Such words keep us motivated.
      Please keep visiting for everyday useful ABAP tweaks and tricks.

      Regards,
      SAPYard.

  6. krishnamraju | July 27, 2015 at 11:24 am |

    That’s really awesome but the output is 010101 what does mean it ……. explain it…
    what ever…..
    Try to conduct more quizzes with radio buttons…..with new interview questions….for abapers…that will useful to us

    • Dear Krishnamraju – Thank you so much for leaving your feedback. We will definitely work on radio button quizzes and tricky questions..

      Regarding 010101 output..
      1 hour = 60 minute = 3600 second
      1 minute = 60 second
      and 1 sec
      so when u add 3600 + 60 + 1 = 3661 = 01:01:01 = 1 second past 1 mintute past 1 hour.. 1 AM 1 Min 1 Sec..
      Hope this is clear.

      Regards,
      SAPYard.

  7. Mutero william | August 14, 2015 at 2:04 pm |

    Thank you so much. I like it.

Comments are closed.