04-05-2016 8:41 AM
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
04-05-2016 9:12 AM
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:
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 ...
04-05-2016 8:57 AM
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
04-05-2016 9:30 AM
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
04-05-2016 9:33 AM
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.
04-05-2016 9:38 AM
Ł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
04-05-2016 9:12 AM
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:
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 ...
04-05-2016 9:36 AM
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.
04-05-2016 9:53 AM
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?
04-05-2016 9:59 AM
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
04-05-2016 10:05 AM
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
04-05-2016 6:02 PM
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.
04-05-2016 6:41 PM
You have to use a non-generic table type instead of "my_ztable". Then it should work.
Regards Christian
04-05-2016 7:14 PM
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
04-07-2016 6:59 AM
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...