Why use ABAP Objects Horst Keller, Stefan Bresch ABAP Language, SAP AG SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 2 Learning Objectives As a result of this workshop, you will be able to: Explain the benefits of object oriented programming in general Explain the benefits of ABAP Objects compared to classical ABAP Understand why Unicode enabled ABAP Objects is the best language for business applications up to now
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 2
Learning Objectives
As a result of this workshop, you will be able to:
Explain the benefits of object oriented programming in generalExplain the benefits of ABAP Objects compared to classical ABAPUnderstand why Unicode enabled ABAP Objects is the best language for business applications up to now
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 31
Reuse – Example ABAP/4 (1)
Function Group
FUNCTION withdraw.
*"--------------------------------------------------------------*" IMPORTING*" REFERENCE(ID) TYPE ACCOUNTS-ID*" REFERENCE(KIND) TYPE C DEFAULT 'C‚*" REFERENCE(AMOUNT) TYPE ACCOUNTS-AMOUNT*" RAISING*" CX_NEGATIVE_AMOUNT*" CX_UNKNOWN_ACCOUNT_TYPE*"--------------------------------------------------------------
CASE kind.WHEN 'C'. „Checking accountPERFORM withdraw_from_checking_account USING id amount.
WHEN 'S'. "Savings accountPERFORM withdraw_from_saving_account USING id amount.
WHEN OTHERS.RAISE EXCEPTION TYPE cx_unknown_account_type.
ENDCASE.ENDFUNCTION.
Steering parameters
Additional exception
Procedure calls with parameter passing
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 32
Reuse – Example ABAP/4 (2)
Function Group
FORM withdraw_from_checking_accountUSING l_id TYPE accounts-id
l_amount TYPE accounts-amount.DATA account_wa TYPE accounts.READ TABLE account_tab INTO account_wa WITH TABLE KEY id = l_id.account_wa-amount = account_wa-amount - l_amount.MODIFY TABLE account_tab FROM account_wa.IF account_wa-amount < 0.... " handle debit balance
ENDIF.ENDFORM.
FORM withdraw_from_savings_account USING l_id TYPE accounts-id
l_amount TYPE accounts-amountRAISING cx_negative_amount.
DATA account_wa TYPE accounts.READ TABLE account_tab INTO account_wa WITH TABLE KEY id = l_id.IF account_wa-amount > l_amount.account_wa-amount = account_wa-amount - l_amount.MODIFY TABLE account_tab FROM account_wa.
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 63
Next Topic
Background
Principal Features of ABAP ObjectsEncapsulationInstantiationReuse via InheritanceInterfacingEvents
Benefits of ABAP ObjectsSimplicityExplicitnessMaintainabilityPurified ABAPFuture Orientation
Conclusion
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 64
Purified ABAP - Syntax (1)
ABAP/4 has developed over a long period of timelarge number of obsolete statements overlapping conceptshighly specialized conceptssurprising implicit behaviordifficult to learn
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 65
Purified ABAP - Syntax (2)
ABAP Objects reduces the complexity of ABAPmany obsolete statements and additions are forbiddenmany implicit syntax completions must be done explicitwrong handling of data is restricted
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 66
Purified ABAP – Syntax Examples (1)
Clean-Up in ABAP ObjectsNo implicit work areas
No internal tables with header lines explicit specification of work areas
No TABLES work areas no data sharing between programs, explicit specification of work areas in Open SQL , ...
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 69
Purified ABAP - Semantics
Unicode-enabled ABAPStatic type checks specified more preciselyByte and character strings processed separatelyStructures handled appropriately for their type using structural equivalence rulesUncontrolled memory manipulation no longer permitted
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 70
Purified Semantics – Example (1)
Unicode-enabled ABAPStructures are assigned fragment by fragment
DATA: BEGIN OF struc1,col1(2) TYPE c VALUE 'AB',col2(2) TYPE c VALUE 'CD',
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 71
Purified Semantics – Example (2)
Unicode-enabled ABAPNo access to memory outside a data objectDATA: text1(10) TYPE c,
text2(10) TYPE c VALUE 'ABCDEFGHIJ',off TYPE i.
FIELD-SYMBOLS <fs> TYPE c.
DO 15 TIMES.off = sy-index - 1.ASSIGN text1+off(1) TO <fs>.IF <fs> IS ASSIGNED.<fs> = 'X'.
ENDIF.ENDDO.
XXXXXFGHIJ text2 in non-Unicode-enabled ABAP:
Memoryoverwriting,
even for references!
ABCDEFGHIJtext2 in Unicode-enabled ABAP:
Memory protection
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 72
Purified Syntax + Purified Semantics
Unicode-enabled ABAP Objects!Unicode-enabled ABAP Objects is the best ABAPavailable up to now!If you use Unicode-enabled ABAP Objects, you program automatically according to the rules of purified syntax and semanticsEven if you do not exploit the real “object oriented”features, programs written in Unicode-enabled ABAP Objects are
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 75
Next Topic
Background
Principal Features of ABAP ObjectsEncapsulationInstantiationReuse via InheritanceInterfacingEvents
Benefits of ABAP ObjectsSimplicityExplicitnessMaintainabilityPurified ABAPFuture Orientation
Conclusion
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 76
Conclusion - Summary
ABAP Objects offersbetter encapsulationbetter interfacesbetter techniques for reusemore static type securitysupport for multiple instantiationbetter support for dynamic programming
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 77
Conclusion - Recommendation
Everybody programming in ABAP should use ABAP Objects for new and ongoing projects
take advantage of object oriented featuresuse methods as far as possible, even if you stay within the procedural programming modeldo not use subroutines any moreuse function modules only when technically necessary (RFC, encapsulation of screens etc.) disentangle classical ABAP from ABAP Objects
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 78
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 79
Appendix A – Stricter Syntax in ABAP Objects (1/2)
Notation: No special characters in names, no length specifications<= zero, no multi-line literalsDeclarations: LIKE references to data objects only; no implicit lengths or decimal places in TYPES; no length specifications for data types i, f, d, or t; no operational statements in structure definitions; FIELDS, RANGES, INFOTYPES, TABLES, NODES, COMMON PART, OCCURS, NON-LOCAL not permittedForbidden operations: CLEAR … WITH NULL, PACK, MOVE ... PERCENTAGE, ADD-CORRESPONDING, DIVIDE-CORRESPONDING, SUBTRACT-CORRESPONDING, MULTIPLY-CORRESPONDING, ADD THEN ... UNTIL ..., ADD FROM ... TO ..., CONVERT {DATE|INVERTED DATE}String processing: Not permitted on numeric data objectsField symbols: No implicit types; FIELD-SYMBOLS … STRUCTURE, ASSIGN ... TYPE, ASSIGN LOCAL COPY OF, ASSIGN TABLE FIELD not permittedLogic expressions: ><, =<, => not permitted, table used with INmust be a selection tableControl structures: No operational statements between CASE and WHEN, ON-ENDON not permitted
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 80
Appendix A – Stricter Syntax in ABAP Objects (2/2)
Internal tables: No headers, no implicit work areas, no redundant key specifications, compatible work areas where necessary, obsolete READ variants and COLLECT ... SORTED BY, WRITE TO itab, PROVIDE (short form) not permittedProcedures (methods): No implicit type assignment, compatibleinitial values only, passing sy-subrc not permitted, raising undefined exceptions not permittedProgram calls: No joint use of USING and SKIP FIRST SCREEN when calling transactions, passing formal parameters implicitly in CALL DIALOG not permittedDatabase accesses: No implicit work areas, no *-work areas, READ, LOOP, REFRESH FROM on database tables not permitted, VERSIONaddition to DELETE and MODIFY not permitted, no PERFORMING additionin Native SQLData cluster: No implicit identifiers, passing parameters explicitly not permitted, no implicit work areas, MAJOR-ID and MINOR-ID not permittedLists: DETAIL, SUMMARY, INPUT, MAXIMUM, MINIMUM, SUMMING, MARK, NEW-SECTION and obsolete print parameters not permitted
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 81
Appendix B – Stricter Syntax in Unicode ProgramsOffset/length accesses: Only performed on character-type or byte-type ranges, and only on flat character-type initial parts of structuresMemory accesses: No access to memory outside a data objectSeparation of byte string and character string processing: Explicit specification with IN BYTE MODE or IN CHARACTER MODE ; appropriate types expected – for character strings this means only c, d, n, t, string, and flat structures with purely character-type componentsStructures: When assigning and comparing you must take the Unicode fragment view into considerationFile interface: Implicitly opening files not permitted; access, storage, and coding type must be specified explicitly; no write access to read-only filesConversions: TRANSLATE ... CODE PAGE ..., TRANSLATE ... NUMBER
FORMAT ... not permittedOPEN SQL: Stricter conditions for work areasType assignment using STRUCTURE: Stricter checks on assignments to field symbols and formal parameters typed using STRUCTURE
Function module calls: A specified formal parameter of a function module must be available
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 82
Appendix C – Encapsulation of Screens (1)
Encapsulation of screens in function groups
Function Group
FUNCTION-POOL flight_screens.
SELECTION-SCREEN BEGIN OF SCREEN 100 AS WINDOW.PARAMETERS: p_carrid TYPE sflight-carrid,
p_connid TYPE sflight-connid,p_fldate TYPE sflight-fldate.
SELECTION-SCREEN END OF SCREEN 100.
TABLES sflight.
Selection Screen
FUNCTION get_flight_parameters.CALL SELECTION-SCREEN 100 STARTING AT 10 10.... " checkscarrid = p_carrid.connid = p_connid.fldate = p_fldate.
ENDFUNCTION.
Interface data for dynpro
FUNCTION get_plane_type.sflight-planetype = plane_type.... " preparationCALL SCREEN 200 STARTING AT 10 10.... " checks plane_type = sflight-planetype.
SAP AG 2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 87
No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice.
Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors.
Microsoft®, WINDOWS®, NT®, EXCEL®, Word®, PowerPoint® and SQL Server® are registered trademarks of Microsoft Corporation.
IBM®, DB2®, DB2 Universal Database, OS/2®, Parallel Sysplex®, MVS/ESA, AIX®, S/390®, AS/400®, OS/390®, OS/400®, iSeries, pSeries, xSeries, zSeries, z/OS, AFP, Intelligent Miner, WebSphere®, Netfinity®, Tivoli®, Informix and Informix® Dynamic ServerTM are trademarks of IBM Corporation in USA and/or other countries.
ORACLE® is a registered trademark of ORACLE Corporation.
UNIX®, X/Open®, OSF/1®, and Motif® are registered trademarks of the Open Group.
Citrix®, the Citrix logo, ICA®, Program Neighborhood®, MetaFrame®, WinFrame®, VideoFrame®, MultiWin® and other Citrix product names referenced herein are trademarks of Citrix Systems, Inc.
HTML, DHTML, XML, XHTML are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology.
JAVA® is a registered trademark of Sun Microsystems, Inc.
JAVASCRIPT® is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape.
MarketSet and Enterprise Buyer are jointly owned trademarks of SAP AG and Commerce One.
SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and in several other countries all over the world. All other product and service names mentioned are the trademarks of their respective companies.