Back to basics !!
Embed Size (px)
Transcript of Back to basics !!
Back to BasicsTOPICS: BOUND Data Type SAP ABAP SoC
POSTED BY: SAP YARD JUNE 26, 2015
Typically, I wanted to name this post as part II of ourprevious post Are you an ABAP coder or aprogrammer?. But later I felt, Back to Basics mademore sense.
At the end of this post, there is a 2 liner code with aquiz. If you could spare one minute to leave acomment with your answer, it would highly motivateus. (Optional, just a request. You need not leave theanswer, just enjoy the post)..
There are bunch of new areas budding in the field ofSAP ABAP. Fiori, UI5, ABAP on HANA etc areknocking and virtually trying to makeABAPers old
DELETING rows of theinternal table within theLOOP. Is it a Taboo? A bigNO NO?Quick Reference for VistexTechnicalOffshore DevelopmentModel in 10 Steps
SAP YARDYOUR BACKYARD FOR SAP TECHNICAL TIPS AND SOLUTIONS
HOME SEE ALL POSTS ASK YOUR QUESTIONS ABOUT ME CONTACT ME
schoolers. But in this age of competition andevolution, do we really have our basics correct?
I would not be writing anything new in this post. But Iwant to jolt our young ABAPers with some examplesand try to put some points.Below examples arereadily available in SAPs Documentation but I amstill quoting them here because, these days we believein asking Google more than searching in SAPsDocumentations. Hope, some day this post would lineup in the first page of the Google search and help theneedful readers.
Question 1: Do you know what type of data object arethose in Declaration 1 & 2? Question 2: Which one is the recommended way andwhy?
Answer 1: Bound_1 and bound_2 are BOUND data types. It iscreated if a data object is defined in the DATAstatement.Num_type is STANDALONE data type.They are defined using the TYPES statement .
We have been using it for years, did you know theseformal terms?
Answer 2:Most of us know, second way is the better way. Usestandalone data types instead of constructing bounddata types when declaring dataobjects.Why?a) A bound data type only exists as a property of that
Ready Reckoner for SAPDevelopersJust a key and two clicks forALV consistency check
* Declaration 1DATA bound_1 TYPE p LENGTH 6 DECIMALS 2. DATA bound_2 TYPE p LENGTH 6 DECIMALS 2.* Declaration 2TYPES num_type TYPE p LENGTH 6 DECIMALS 2. DATA: num_1 TYPE num_type, num_2 TYPE num_type.
single data object. If this type is needed in severaldifferent places, it has to be defined separately foreach place where it is used.b) The declaration of a standalone data type allowsmultiple data objects (or interface parameters or fieldsymbols) to use a type without the need to alwaysredefine this type.c) Even if only one data object of this type is requiredinitially, it is very likely that further data objects willbe added during the course of the development. If thetype needs to be adapted later on, you can do thiscentrally.d) Declaring a standalone type and using it to declarea data object means following the SoC (Separation ofConcerns) principle.
Point 2In the above example we stressed on reusability of theSTANDALONE Type. Does the same rule apply for thenewly released Inline declarations? Lets check.
Going by the reusabilty fundamentals, our logicalmind says, Declaration 1 is better as we have only onedata to take care of two tables. But SAP says,
* Declaration 1DATA:i_tab1 TYPE STANDARD TABLE OF string,i_tab2 TYPE STANDARD TABLE OF string.LOOP AT i_tab1 ASSIGNING FIELD-SYMBOL().* Do somethingENDLOOP.LOOP AT i_tab2 ASSIGNING .* Do somethingENDLOOP.* Declaration 2DATA:i_tab1 TYPE STANDARD TABLE OF string,i_tab2 TYPE STANDARD TABLE OF string.LOOP AT i_tab1 ASSIGNING FIELD-SYMBOL().* Do somethingENDLOOP.LOOP AT i_tab2 ASSIGNING FIELD-SYMBOL().* Do somethingENDLOOP.
Declaration 2 is better. Why?
Answer: The rule dictating that no global programvariables and field symbols are to be declared alsoapplies to inline declarations, without restrictions.The declaration operators can be used to make inlinedeclarations in writer positions. In this way,declarations are made in operational statementsrather than in declaration statements. The declarationoperators are compiled only in executable statements.
SAP suggests to use declaration operators as if theywere local declarations in the current statementblock. The fact that they are valid in the whole method isignored, for the sake of simplicity. If the field symboland the variables are only to be declared once for bothloops, they should be declared at the start of themethod using declaration statements (and not asinline declaration operators). Using one inline operatorin two different logical processing blocks negate the verypurpose of inline operators.
Point 3Should you be contented with technically correctstatements? Or are the semantically correct statementsbetter?
Technically both the above declarations are correctand do not have any performance impact. Butdeclaration 2 is better because SAPrecommends touse semantically appropriate data types only. After all
* Declaration 1DATA is_initial TYPE SYST_MSGTY.IF is_initial IS INITIAL.* Do somethingENDIF.* Declaration 2DATA is_initial TYPE abap_bool.IF is_initial EQ abap_false.* Do somethingENDIF.
SYST_MSGTY in first declaration is for system fieldsy-msgtywhich indicates the message type but thedeclared variable IS_INITIAL, whose name and useclearly indicate that it is used for a truth value.
The technical properties of a type alone do not justifyits use in a specific context, as this impedes thereadability of the program. For this reason, we mustuse only data types whose semantics match the usage.
Point 4Use the Data Type abap_bool for Logic Values. Usingthe type abap_bool and the constants abap_true andabap_false makes it clear that logic values are beingused.
Declaration 2 and usage makes it semantically andlogically legible.
Avoid using literals in operand positions. Period!!
Trivia:Text field literals are enclosed in single speech marks
* Declaration 1DATA is_found TYPE c LENGTH 1. is_found = 'X'.IF is_found IS NOT INITIAL.* Do somethingENDIF.* Declaration 2DATA is_found TYPE abap_bool.is_found = abap_true.IF is_found = abap_true.* Do somethingENDIF
* Usage of literal 1v_circumference = 2 * '3.141592653589793238462643383279503'* Usage of literal 2CONSTANTS pi TYPE decfloat34 VALUE '3.141592653589793238462643383279503'v_circumference = 2 * pi * v_radius.
(). The data type is c.String literals are enclosed in back quotes (`). Thedata type is string.
Point 6Did you know that Strings are dynamic data objects ofvariable length. There are text strings of the string datatype and byte strings of the xstring data type.
The speciality of String data type is that length ofstrings automatically adapts to the content where asthe text and byte fields of a fixed length (c, x datatypes) are fixed.
What does it mean?It means, fixed length text and byte fields of type (c,x) consume the fixed memory. Strings are moreflexible than fields of a fixed length and usually helpus save memory space, because no unnecessary spaceis occupied by blanks or zeros.
exception: If it is obvious that a certain length is neverexceeded, you can also use short fields of a fixed length.
Instead of fixed 255 characters, dynamic string is alwaysa better alternative.
* Declaration 1TYPES html_line TYPE c LENGTH 255. DATA html_table TYPE TABLE OF html_line. APPEND '' TO html_table.APPEND '' TO html_table.APPEND '' TO html_table. APPEND '' TO html_table.* Declaration 2DATA html_table TYPE TABLE OF string. APPEND `` TO html_table.APPEND `` TO html_table.APPEND `` TO html_table. APPEND `` TO html_table.