Back to basics !!

12
Back to Basics TOPICS: 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 our previous post “Are you an ABAP coder or a programmer?“. But later I felt, Back to Basicsmade 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 Enter email Subscribe RECENT POSTS DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO? Quick Reference for Vistex Technical Offshore Development Model in 10 Steps SAP YARD YOUR BACKYARD FOR SAP TECHNICAL TIPS AND SOLUTIONS HOME SEE ALL POSTS ASK YOUR QUESTIONS ABOUT ME CONTACT ME You and 92 other friends like this SAP Yard 168 likes Liked SEARCH … 8 8 6 1

Transcript of Back to basics !!

8/11/2015 Back to Basics | SAP Yard

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 make ABAPers old

Enter email

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

You and 92 other friends like this

SAP Yard168 likes

Liked

SEARCH …

8

8

6

1

8/11/2015 Back to Basics | SAP Yard

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 SAP’s Documentation but I amstill quoting them here because, these days we believein asking Google more than searching in SAP’sDocumentations. 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 data objects. 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.

8/11/2015 Back to Basics | SAP Yard

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(<fs>).* Do somethingENDLOOP. LOOP AT i_tab2 ASSIGNING <fs>.* 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(<fs1>).* Do somethingENDLOOP. LOOP AT i_tab2 ASSIGNING FIELD-SYMBOL(<fs2>).* Do somethingENDLOOP.

8/11/2015 Back to Basics | SAP Yard

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 SAP recommends 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 Back to Basics | SAP Yard

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

SYST_MSGTY in first declaration is for system fieldsy-msgty which 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 Back to Basics | SAP Yard

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 '<HTML>' TO html_table. APPEND '<BODY>' TO html_table. APPEND '</BODY>' TO html_table. APPEND '</HTML>' TO html_table. * Declaration 2DATA 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.

8/11/2015 Back to Basics | SAP Yard

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

12 COMMENTS ON "BACK TO BASICS"

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

May I request you to please write what you thinkwould be the output in the comment section of thispost and then put these two lines of code in yoursystem and know the correct answer. Please note: Weare not trying to test any ones’ knowledge. It is just forfun.

I am sure, you would be blown away with theoutput. Please share your thoughts on what happenedand 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

Previous post Next post

Really blown away by the output….never usedtype t in my long career and thought I haveworked on many things :-)…..frankly speakingI thought the input is wrong as 61 cannot be a

Soumyadeep Basu | June 29, 2015 at12:25 pm | Reply

 CONSTANTS v_time TYPE t VALUE 003661.write:/ v_time.

8/11/2015 Back to Basics | SAP Yard

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

second value good blog!!

Thanks Soumyadeep!!

Glad that you liked it. True, SAP is anocean.. The deeper you dive, the moreyou are amazed..

Regards,Raju.

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

Thanks!

Thank you so much Steve.Appreciate it..

Regards,Raju.

SAP Yard | June 29, 2015 at1:23 pm | Reply

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

SAP Yard | June 29, 2015 at2:37 pm | Reply

8/11/2015 Back to Basics | SAP Yard

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

This is very well structureddocument. Thanks Raju

Thanks Apan!! Glad youliked it..

Raju

This is an excellent and unique SAP ABAPblog I have ever seen. Raju I liked it verymuch.

Thank you so much Tuhin.Appreciate you visited it.

Regards,Raju.

Apan | June 30, 2015 at 2:15 pm | Reply

SAP Yard | June 30, 2015 at2:16 pm | Reply

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

SAP Yard | June 30, 2015 at6:16 pm | Reply

8/11/2015 Back to Basics | SAP Yard

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

Wow!! What an article… Revisited the basicswith a new outlook.Keep up the good work, would certainly lookforward for more of them.

Dear Saket – Thank you somuch for your appreciation. Such wordskeep us motivated.Please keep visiting for everyday usefulABAP tweaks and tricks.

Regards,SAPYard.

That’s really awesome but the output is010101 what does mean it ……. explain it…what ever…..Try to conduct more quizzes with radiobuttons…..with new interview questions….forabapers…that will useful to us

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

SAP Yard | July 25, 2015 at6:46 pm | Reply

krishnamraju | July 27, 2015 at 11:24am | Reply

8/11/2015 Back to Basics | SAP Yard

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

Dear Krishnamraju –Thank you so much for leaving yourfeedback. We will definitely work onradio button quizzes and trickyquestions..

Regarding 010101 output..1 hour = 60 minute = 3600 second1 minute = 60 secondand 1 secso when u add 3600 + 60 + 1 = 3661 =01:01:01 = 1 second past 1 mintute past1 hour.. 1 AM 1 Min 1 Sec..Hope this is clear.

Regards,SAPYard.

Leave a commentYour email address will not be published.

Name *Raju

Email *[email protected]

Website

Comment

SAP Yard | July 27, 2015 at 1:39pm | Reply

8/11/2015 Back to Basics | SAP Yard

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

Post Comment

COPYRIGHT 2015 | SAPYARD BY WWW.SAPYARD.COMALL PRODUCT NAMES ARE TRADEMARKS OF THEIR RESPECTIVE COMPANIES. SAPYARD.COM IS NOT AFFILIATED TO SAP AG.