Back to basics !!

Click here to load reader

  • date post

    15-Apr-2017
  • Category

    Technology

  • view

    194
  • download

    0

Embed Size (px)

Transcript of Back to basics !!

  • 8/11/2015 BacktoBasics|SAPYard

    http://www.sapyard.com/backtobasics/ 1/12

    Back to BasicsTOPICS: BOUND Data Type SAP ABAP SoC

    XSTRING

    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

    Enteremail

    Subscribe

    RECENT POSTS

    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

    Youand92otherfriendslikethis

    SAPYard168likes

    Liked

    SEARCH

    8

    8

    6

    1

    https://www.facebook.com/sapyardjavascript:void(0);https://www.facebook.com/raju.shrestha.315http://www.sapyard.com/tag/sap-abap/http://www.sapyard.com/deleting-rows-of-the-internal-table-within-the-loop-is-it-a-taboo-a-big-no-no/http://www.sapyard.com/tag/soc/javascript:void(0);https://www.facebook.com/rajnarayan.karmakarhttp://www.sapyard.com/javascript:void(0);https://www.facebook.com/vasudeva.saladihttps://www.facebook.com/sapyardhttp://www.sapyard.com/Abouthttp://www.sapyard.com/quick-reference-for-vistex-technical/http://www.sapyard.com/offshore-development-model-in-10-steps/http://www.sapyard.com/https://www.facebook.com/sharer/sharer.php?app_id=776730922422337&u=https%3A%2F%2Fwww.facebook.com%2Fsapyard&display=popup&ref=plugin&src=pagehttps://www.facebook.com/profile.php?id=100005754648662https://www.facebook.com/sapyardjavascript:void(0);https://www.facebook.com/prasanta.kar.5268http://www.sapyard.com/are-you-an-abap-coder-or-a-programmer/javascript:void(0);https://www.facebook.com/jayanta.chakraborty.969http://www.sapyard.com/contacthttp://www.sapyard.com/archives/http://www.sapyard.com/tag/xstring/http://www.sapyard.com/ask/javascript:void(0);http://www.sapyard.com/tag/bound-data-type/javascript:void(0);http://www.sapyard.com/author/raju/

  • 8/11/2015 BacktoBasics|SAPYard

    http://www.sapyard.com/backtobasics/ 2/12

    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.

    Point 1

    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.

    http://www.sapyard.com/just-a-key-and-two-clicks-for-alv-consistency-check/http://www.sapyard.com/ready-reckoner-for-sap-developers/

  • 8/11/2015 BacktoBasics|SAPYard

    http://www.sapyard.com/backtobasics/ 3/12

    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.

  • 8/11/2015 BacktoBasics|SAPYard

    http://www.sapyard.com/backtobasics/ 4/12

    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.

  • 8/11/2015 BacktoBasics|SAPYard

    http://www.sapyard.com/backtobasics/ 5/12

    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.

    Point 5

    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.

  • 8/11/2015 BacktoBasics|SAPYard

    http://www.sapyard.com/backtobasics/ 6/12

    (). 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.

    Point 7.

    * 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.