Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How much ABAP740 (read: functional ABAP programming) is too much?

SuhaSaha
Advisor
Advisor
0 Kudos

Hello SDNers,

Now that i am finally developing productiv programs in ABAP740, i am often faced with the following dilemma -


How much ABAP740 (read: functional ABAP programming) is too much?

When i saw program Z_REDUCE_FOR_FUN in the blog discussion , the same thought came to my mind.

When i see the demo programs from , i am inclined to use as much expressions as possible. Although i add inline comments explaining what the hell is going on, but still sometimes it seems a lil' too much

This construct is definitely not going to make me new friends.

Where do you draw the line? Do you have any techniques to make the expression-based ABAP programming more easier to understand?

BR,

Suhas

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Now, that's why I answered in the blog discussion ABAP News for 7.40, SP08 - FOR Expressions , with the question if expressions really make sense in that case ...


The answer to the above question "how much functional ABAP is too much" should be:


You use functional ABAP for two reasons:


  • you can code easier
  • your programs become better readable


In the moment, one of the above breaks, you don't use functional ABAP, but split into several statements

Of course this is also a question of taste, your skills and your human reader's skills.


I'm using functional ABAP productively, but I don't overdo it (as in some examples demonstrating the capabilities).


A typical all day example goes as follwos. What looks better?



DATA language TYPE string.

IF sy-langu = 'D'.

   language = 'DE'.

ELSE.

   language = 'EN'.

ENDIF.

cl_abap_docu_external=>get_abap_docu_for_adt(

    EXPORTING

      language language

   IMPORTING

     html      =     DATA(html) ).


or




cl_abap_docu_external=>get_abap_docu_for_adt(

    EXPORTING

      language COND #( WHEN sy-langu = 'D' THEN `DE` ELSE `EN` )

   IMPORTING

     html      =     DATA(html) ).


Clearly the second, I got rid of helper variable language and the IF block. It was much easier for me to write that down because I coded the condition where I need it and the code is much better readable than in the old style.


Other typical example for such conditionals:



DATA text TYPE string.

IF sy-langu = 'D'.

   text = `Hallo`.

ELSE.

   text = `Hello`.

ENDIF.

DATA(html) `<html><body>` &&

               text &&

`</body></html>`.


vs.



DATA(html) `<html><body>` &&

               COND #( WHEN sy-langu = 'D' THEN 'Hallo' ELSE 'Hello' ) &&

`</body></html>`.


KISS!


Its the little things that count ...

13 REPLIES 13

ŁukaszPęgiel
Contributor
0 Kudos

Hello Suhas,

this will not bring you new friends for sure unless other will want to learn and use new syntax. If the code is written with old syntax or new it doesn't matter as long as it's optimized. I saw for example people ( and I also) really like to access the field in an itab directly for example:


matnr = mara[ 1 ]-mantr

and in the next line(s)


matkl = mara[ 1 ]-matkl.

mtart = mara[ 1 ]-mtart.

instead of :


assign mara[ 1 ] to field-symbol(<mara>).

if <mara> is assigned.

     matnr = <mara>-matnr.

     matkl = <mara>-matkl.

     mtart = <mara>-mtart.

    

endif.

But in my own opinion there is never too much of 740 syntax

Cheers

Łukasz

0 Kudos

matkl = mara[ 1 ]-matkl.

mtart = mara[ 1 ]-mtart.

But does it not read the internal table MARA twice? For index access maybe the performance hit won't be too much, but i still wouldn't like to read the same table with the same key more than once

0 Kudos

Yes it does access table many times, that's why I said syntax itself is nice, but the use must be optimized, like in the assign.. example.

0 Kudos

Łukasz Pęgiel wrote:

Yes it does access table many times, that's why I said syntax itself is nice, but the use must be optimized, like in the assign.. example.

Now i get it, we are on the same page then

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Now, that's why I answered in the blog discussion ABAP News for 7.40, SP08 - FOR Expressions , with the question if expressions really make sense in that case ...


The answer to the above question "how much functional ABAP is too much" should be:


You use functional ABAP for two reasons:


  • you can code easier
  • your programs become better readable


In the moment, one of the above breaks, you don't use functional ABAP, but split into several statements

Of course this is also a question of taste, your skills and your human reader's skills.


I'm using functional ABAP productively, but I don't overdo it (as in some examples demonstrating the capabilities).


A typical all day example goes as follwos. What looks better?



DATA language TYPE string.

IF sy-langu = 'D'.

   language = 'DE'.

ELSE.

   language = 'EN'.

ENDIF.

cl_abap_docu_external=>get_abap_docu_for_adt(

    EXPORTING

      language language

   IMPORTING

     html      =     DATA(html) ).


or




cl_abap_docu_external=>get_abap_docu_for_adt(

    EXPORTING

      language COND #( WHEN sy-langu = 'D' THEN `DE` ELSE `EN` )

   IMPORTING

     html      =     DATA(html) ).


Clearly the second, I got rid of helper variable language and the IF block. It was much easier for me to write that down because I coded the condition where I need it and the code is much better readable than in the old style.


Other typical example for such conditionals:



DATA text TYPE string.

IF sy-langu = 'D'.

   text = `Hallo`.

ELSE.

   text = `Hello`.

ENDIF.

DATA(html) `<html><body>` &&

               text &&

`</body></html>`.


vs.



DATA(html) `<html><body>` &&

               COND #( WHEN sy-langu = 'D' THEN 'Hallo' ELSE 'Hello' ) &&

`</body></html>`.


KISS!


Its the little things that count ...

0 Kudos

Thanks for the words of wisdom Horst!

Do you think the code snippet i have attached adheres to the KISS principle? I was inclined to declare a helper variable, use the REDUCE construct there & pass the helper variable in the VALUE construct for the internal table

Of course this is also a question of taste, your skills and your human reader's skills.

But as Lukas has rightly pointed out, if the other ABAPers do not want to learn/use the new syntax it is going to be difficult to explain the code to them.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

For me, your code snippet is readable and adheres to the KISS principle. Functional ABAP is used straightforward and not too deeply nested.

The main problem is the skill set. Human readers aquaint to LET, REDUCE or COND shouldn't have problems to understand nested expressions if the nesting level is not too deep. (About the same rules should hold as for nesting control structures).

I rather see the problem, that many ABAP programmers are simply not willing to get engaged with functional ABAP (it might be those still using FORMs instead of methods ). But should we refain from using it for that reason?

nomssi
Active Contributor
0 Kudos

Hello Suhas,

let me rewrite your task as follow: retrieve the first entry of a filtered internal table and change it.

my advice: separate the imperative part from the pure, side effect free functions that can nicely be written as expressions. In a functional language, you FILTER your data and TAKE the first entry with pure function, In ABAP that could look like e.g.

1) DATA(filter) = VALUE table_of_flights( FOR f IN flights WHERE ( fldate <= sy-datum ) ( f ) ).

2)

  TRY.

  *  do something with filter[ 1 ]

     CATCH cx_sy_itab_error.

   ENDTRY.

This passes Horst's test. Now if you worry about performance, use SQL / a CDS view to push the code to the database

    SELECT * FROM sflight UP TO 1 ROWS

      WHERE fldate <= @sy-datum

      INTO TABLE @DATA(flights).


regards,

JNN

0 Kudos

Hello JNN,

In a functional language, you FILTER your data and TAKE the first entry with pure function

That's a novel idea and i like it

BR,

Suhas

0 Kudos

And there are people who do not like the I.D. (Inline Declarations), developed using these instructions is the best thing that has.

Taking advantage, was in doubt as his above statement tried to replicate it, but to me it says: "There are no components with the name" FOR ".". Is it true that in value should be the table name?

Example: DATE (filter) = VALUE my_ztable (FOR f IN itab WHERE (date <= sy-datum) (f)).


I'm using SAP_ABA 740 SP12


Best regards,


Raphael Pacheco.

0 Kudos

You have to use a non-generic table type instead of "my_ztable". Then it should work.

Regards Christian

ChristianGünter
Contributor
0 Kudos

Great to see that my little toy program ignited such a great discussion

In my opinion the latest functional extensions to the ABAP programming languages give us new means of abstraction. As before things like ABAP Objects did too. It's always hard for people to grasp new concepts but once you get accustomed to them they'll leverage your power. But if don't get used to them you sooner or later have a problem. But keep in mind with great power comes great responsibility. In the end as already said by others it's all a matter of experience, skill and knowledge.

As a rule of thumb, if I look at a expression after a long mental break and can immediately understand them again they seem ok to me.

Btw. I don't think that the recent extensions are the end of that development. Anyone said first class functions, closures or tail recursion?

Christian

matt
Active Contributor
0 Kudos

Trade readability against functionality and robustness. You can write fairly unreadable ABAP code even in pre 46c. New language constructs can always be overused - often the examples supplied aren't ones you'd ever use in the real worlds, as they're designed to showcase what the construct does.

Even using RESUMABLE will lose you a few friends! Or ABAP Objects...