Sei sulla pagina 1di 29

Creating Efficient Code Using the Renewed ABAP

SAP NetWeaver 7.4


Disclaimer

This presentation outlines our general product direction and should not be relied on in making a
purchase decision. This presentation is not subject to your license agreement or any other agreement
with SAP. SAP has no obligation to pursue any course of business outlined in this presentation or to
develop or release any functionality mentioned in this presentation. This presentation and SAP's
strategy and possible future developments are subject to change and may be changed by SAP at any
time for any reason without notice. This document is provided without a warranty of any kind, either
express or implied, including but not limited to, the implied warranties of merchantability, fitness for a
particular purpose, or non-infringement. SAP assumes no responsibility for errors or omissions in this
document, except if such damages were caused by SAP intentionally or grossly negligent.

2013 SAP AG or an SAP affiliate company. All rights reserved. 2


Agenda

Expressions
Inline Declarations
Constructors
Internal Table Expressions

2013 SAP AG or an SAP affiliate company. All rights reserved. 3


Expressions
Expressions
Motivation

Declarative - What instead of How

Functional - less side effects

Modern catch up with trends

See:

http://en.wikipedia.org/wiki/Declarative_programming

http://en.wikipedia.org/wiki/Functional_programming

2013 SAP AG or an SAP affiliate company. All rights reserved. 5


Expressions
Inline Declarations

Inline Declarations
... DATA(var) ...
... FIELD-SYMBOL(<fs>) ...

The new declaration operators DATA and FIELD-SYMBOL allow inline declarations of variables and field
symbols with declaration expressions in declaration positions.

Declaration positions are write positions where the operand type can be determined from the context
statically.

2013 SAP AG or an SAP affiliate company. All rights reserved. 6


Expressions
Inline Declarations DATA( )

LOOP AT itab INTO DATA(wa).


... oref->meth( IMPORTING p1 = DATA(a1)
ENDLOOP. IMPORTING p2 = DATA(a2)
... ).

READ TABLE itab INTO DATA(wa) ...

DATA(ref) = class=>factory( ... ).


FIND ... IN ... MATCH COUNT DATA(cnt).

CALL TRANSFORMATION ... RESULT XML DATA(xml).

2013 SAP AG or an SAP affiliate company. All rights reserved. 7


Expressions
Inline Declarations DATA( )

DATA ixml TYPE REF TO if_ixml.


DATA stream_factory TYPE REF TO if_ixml_stream_factory.
DATA document TYPE REF TO if_ixml_document.

ixml = cl_ixml=>create( ).
stream_factory = ixml->create_stream_factory( ).
document = ixml->create_document( ).

DATA(ixml) = cl_ixml=>create( ).
DATA(stream_factory) = ixml->create_stream_factory( ).
DATA(document) = ixml->create_document( ).

2013 SAP AG or an SAP affiliate company. All rights reserved. 8


Expressions
Inline Declarations FIELD-SYMBOL( )

ASSIGN ... TO FIELD-SYMBOL(<fs>).

LOOP AT itab ASSIGNING FIELD-SYMBOL(<line>).


...
ENDLOOP.

READ TABLE itab ASSIGNING FIELD-SYMBOL(<line>) ...

2013 SAP AG or an SAP affiliate company. All rights reserved. 9


Expressions
Inline Declarations FIELD-SYMBOL( )

FIELD-SYMBOLS <line> LIKE LINE OF itab.

LOOP AT itab ASSIGNING <line>.


...
ENDLOOP.

LOOP AT itab ASSIGNING FIELD-SYMBOL(<line>).


...
ENDLOOP.

2013 SAP AG or an SAP affiliate company. All rights reserved. 10


Expressions
Constructor Expressions

Constructor Expressions
... NEW|VALUE|REF|EXACT|CONV|CAST|COND|SWITCH type( ... ) ...

The new constructor operators

NEW creates objects


VALUE creates values
REF gets references
EXACT performs lossless calculations or assignments
CONV converts values
CAST performs up or down casts
COND and SWITCH enable conditional expressions

allow to construct results of a specified type in general expression positions.

2013 SAP AG or an SAP affiliate company. All rights reserved. 11


Expressions
Constructor Expressions NEW( )

FIELD-SYMBOLS <fS> TYPE data.


DATA dref TYPE REF TO data.
DATA dref TYPE REF TO data.
CREATE DATA dref TYPE i.
dref = NEW i( 555 ).
ASSIGN dref->* TO <fs>.
<fs> = 555.

DATA(oref) = NEW class( ... ).


DATA oref TYPE REF TO class.
CREATE OBJECT oref EXPORTING ...
DATA oref TYPE REF TO class.
oref = NEW #( ... ).

2013 SAP AG or an SAP affiliate company. All rights reserved. 12


Expressions
Constructor Expressions VALUE( )

DATA(itab) = VALUE t_itab(


( col1 = 1 col2 = 2 )
( col1 = 3 col2 = 4 ) ).
DATA itab TYPE t_itab.
DATA wa LIKE LINE OF itab.
method( itab ).
wa-col1 = 1. wa-col2 = 2.
APPEND wa TO itab.
wa-col1 = 3. wa-col2 = 4.
APPEND wa TO itab.
method( itab ).
method( VALUE t_itab(
( col1 = 1 col2 = 2 )
( col1 = 3 col2 = 4 ) ) ).

2013 SAP AG or an SAP affiliate company. All rights reserved. 13


Expressions
Constructor Expressions REF( )
DATA dref TYPE REF TO string.
GET REFERENCE OF para INTO dref.

DATA(ptab) =
VALUE abap_parmbind_tab(
( name = name
kind = cl_abap_objectdescr=>exporting
value = dref ) ).

CALL METHOD (class)=>(meth) PARAMETER-TABLE ptab.

DATA(ptab) =
VALUE abap_parmbind_tab(
( name = name
kind = cl_abap_objectdescr=>exporting
value = REF #( para ) ) ).

CALL METHOD (class)=>(meth) PARAMETER-TABLE ptab.

2013 SAP AG or an SAP affiliate company. All rights reserved. 14


Expressions
Constructor Expressions EXACT( )

TYPES numtext TYPE n LENGTH 255.


DATA number TYPE numtext.
TRY.
MOVE EXACT '4 Apples + 3 Oranges' TO number.
CATCH cx_sy_conversion_error INTO DATA(exc).
...
ENDTRY.

TYPES numtext TYPE n LENGTH 255.


TRY.
DATA(number) = EXACT numtext( '4 Apples + 3 Oranges' ).
CATCH cx_sy_conversion_error INTO DATA(exc).
...
ENDTRY.

2013 SAP AG or an SAP affiliate company. All rights reserved. 15


Expressions
Constructor Expressions CONV( )

DATA text TYPE c LENGTH 255.


DATA text TYPE c LENGTH 255. DATA(xstr) = cl_abap_codepage=>convert_to(
DATA helper TYPE string. source = CONV #( text ) ).
helper = text.
DATA(xstr) = cl_abap_codepage=>convert_to(
source = helper ).

IF CONV decfloat34( 1 / 3 ) > 0.


IF 1 / 3 > 0. ...
... ENDIF.
ENDIF.

IF ' ' = CONV char1( ` ` ).


IF ' ' = ` `. ...
... ENDIF.
ENDIF.

2013 SAP AG or an SAP affiliate company. All rights reserved. 16


Expressions
Constructor Expressions CAST( )

DATA structdescr TYPE REF TO cl_abap_structdescr.


structdescr ?= cl_abap_typedescr=>describe_by_name( 'T100' ).
DATA(components) = structdescr->components.

DATA(components) =
CAST cl_abap_structdescr(
cl_abap_typedescr=>describe_by_name( 'T100' )
)->components.

2013 SAP AG or an SAP affiliate company. All rights reserved. 17


Expressions
Constructor Expressions COND( )

DATA time TYPE string.


IF sy-timlo < '120000'.
time = |{ sy-timlo TIME = ISO } AM|.
ELSEIF sy-timlo > '120000'.
time = |{ CONV t( sy-timlo - 12 * 3600 )
TIME = ISO } PM|.
ELSEIF sy-timlo = '120000'. DATA(time) =
time = |High Noon|. COND string(
ELSE. WHEN sy-timlo < '120000' THEN
RAISE EXCEPTION TYPE cx_cant_be. |{ sy-timlo TIME = ISO } AM|
ENDIF. WHEN sy-timlo > '120000' THEN
|{ CONV t( sy-timlo - 12 * 3600 )
TIME = ISO } PM|
WHEN sy-timlo = '120000' THEN
|High Noon|
ELSE
THROW cx_cant_be( ) ).

2013 SAP AG or an SAP affiliate company. All rights reserved. 18


Expressions
Constructor Expressions SWITCH( )

DATA number TYPE string.


CASE sy-index.
WHEN 1.
number = 'one'.
WHEN 2.
number = 'two'.
WHEN 3.
number = 'three'.
WHEN OTHERS.
RAISE EXCEPTION TYPE cx_overflow.
ENDCASE. DATA(number) =
SWITCH string( sy-index
WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
WHEN 3 THEN 'three'
ELSE THROW cx_overflow( ) ).

2013 SAP AG or an SAP affiliate company. All rights reserved. 19


Expressions
Table Expressions

Table Expressions
... itab[ ... ] ...

The new table expressions itab[ ] enable read access to internal tables at operand positions.

The operand positions can be read positions and also some write positions

2013 SAP AG or an SAP affiliate company. All rights reserved. 20


Expressions
Table Expressions table line

wa = itab[ idx ].
READ TABLE itab INDEX idx INTO wa.

READ TABLE itab INDEX idx wa = itab[ KEY key INDEX idx ].
USING KEY key INTO wa.

wa = itab[col1 = ... col2 = ... ].


READ TABLE flights WITH KEY
col1 = ... col2 = ... INTO wa.
wa = itab[ KEY key COMPONENTS
col1 = ... col2 = ... ].
READ TABLE flights WITH TABLE KEY key
COMPONENTS col1 = ... col2 = ...
INTO wa. wa = itab[ KEY key
col1 = ... col2 = ... ].

2013 SAP AG or an SAP affiliate company. All rights reserved. 21


Expressions
Table Expressions result

... itab[ ... ] ... READ TABLE ... ASSIGNING ...

Performance
considerations!
... VALUE type( itab[ ... ] ) ... READ TABLE ... INTO ...

... REF type( itab[ ... ] ) ... READ TABLE ... REFERENCE INTO ...

2013 SAP AG or an SAP affiliate company. All rights reserved. 22


Expressions
Table Expressions chaining

2 3 READ TABLE itab INTO DATA(wa1) INDEX 2.


READ TABLE wa1-col2 INTO DATA(wa2) INDEX 1.
4 5
1 READ TABLE wa2 INTO DATA(wa3) INDEX 2.
6 7
DATA(num) = wa3-col1.
8 9

11 12

13 14
10
15 16
DATA(num) = itab[ 2 ]-col2[ 1 ][ 2 ]-col1.
17 18

2013 SAP AG or an SAP affiliate company. All rights reserved. 23


Expressions
Internal Tables Built-in Functions

READ TABLE itab WITH ... TRANSPORTING NO FIELDS. DATA(idx) =


DATA(idx) = sy-tabix. line_index( itab[ ... ] ).

READ TABLE itab WITH ... TRANSPORTING NO FIELDS. IF line_exists( itab[ ... ] ).
IF sy-subrc = 0. ...
... ENDIF.
ENDIF.

2013 SAP AG or an SAP affiliate company. All rights reserved. 24


Expressions
ABAP is Extensively Expression Enabled

Skip Old Fashioned Style!

MOVE source TO target.

COMPUTE result = anything.

CALL METHOD meth EXPORTING ...


RECEIVING ...
LeftHandSide = RightHandSide.

2013 SAP AG or an SAP affiliate company. All rights reserved. 25


Expressions
ABAP is Extensively Expression Enabled Example I
DATA:
itab TYPE tt, DATA(itab) = VALUE tt( ( c1 = 7 c2 = 9 )
wa LIKE LINE OF itab. ( c1 = 3 c2 = 5 ) ).
FIELD-SYMBOLS:
<wa1> TYPE ts, itab[ 1 ]-c2 = itab[ c2 = 5 ]-c1.
<wa2> TYPE ts.

wa-c1 = 7.
wa-c2 = 9.
INSERT wa INTO TABLE itab.
wa-c1 = 3.
wa-c2 = 5.
INERT wa INTO TABLE itab.

READ TABLE itab INDEX 1 ASSIGNING <wa1>.


READ TABLE itab WITH KEY c2 = 5
ASSIGNING <wa2>.
<wa1>-c2 = <wa2>-c1.

2013 SAP AG or an SAP affiliate company. All rights reserved. 26


Expressions
ABAP is Extensively Expression Enabled Example II

DATA(factory) = NEW cl_factory( ).


factory->service( CONV #( id ) ).
CONSTANTS id TYPE i VALUE 17.

DATA:
factory TYPE REF TO cl_factory,
id_string TYPE string.

CREATE OBJECT factory.


Even
id_string = id.
factory->service( id_string ).
NEW cl_factory( )->service( CONV #( id ) ).

2013 SAP AG or an SAP affiliate company. All rights reserved. 27


Expressions
ABAP is Extensively Expression Enabled Pitfalls

Obfuscating Performance
itab1[ 1 ]-a = itab2[ 1 ]-x.
lcl=>cm_ii( itab1[ 1 ]-b = itab2[ 1 ]-y.
lcl=>factory2( itab1[ 1 ]-c = itab2[ 1 ]-z.
f_in1 = lcl=>factory0( )->im_ii( i )
f_in2 = lcl=>cm_ii( 2 ** i ) - 3 +
itab[ 2 ]-s-xtab[ x = 'abc' ]
)->m_ii(
lcl=>factory1( f_in = itab[ a = 2
b = 4711 / 2
]-x
)->m_lif_i( lcl=>cm_ii( 5 ) )->im_ii( 6 )
)
).

2013 SAP AG or an SAP affiliate company. All rights reserved. 28


2013 SAP AG or an SAP affiliate company. All rights reserved.

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.

National product specifications may vary.

These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and
SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth
in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

SAP 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
other countries.

Please see http://www.sap.com/corporate-en/legal/copyright/index.epx#trademark for additional trademark information and notices.

2013 SAP AG or an SAP affiliate company. All rights reserved. 29

Potrebbero piacerti anche