With ABAP 740, a new feature was introduced called “Constructor expressions”. First I was sceptical about their usefulness, and it seemed to me that they make the coding more complicated and more difficult to read. But since I am always very curious about new possibilities in programming, I decided to try them out, and I must admit that meanwhile I am a fan of them. In this post, I’d like to focus on VALUE, which I use frequently.
Coding examples
Look at these two examples from a mapping routine, taken out of a productive program of mine:
Where’s the difference?
One could say “where’s the difference? No fewer coding lines, no saving of variable declaration!”, which is all true. But there are two functional differences and one aesthetical.
The first functional difference is, that the target structure is initialized before assigning the values to it. If you ever forgot a “clear” statement before filling data structures, you will appreciate this effect.
The second comes out in debugging. The whole mapping of this structure will be done in the debugger with one single step. Of course, pressing F5 will bring you into the method determine_req_date, which is called by the expression, but the rest is done with one step. Do you remember, how often you were hammering F5 to step over a large mapping section, having to stop on every single assignment?
The second difference is about beauty and readability of the code. Using the constructor expression, you get one block for each structured element you populate, which leads to good readable coding even if the single module is quite long. Look at this example:
Do you get the Idea?
Conclusion
I’d appreciate to know your thoughts!
All the best
Jörg
Hi Jörg,
I agree VALUE #( ) helps to reduce the boilerplate.
1) to take it further, couldn’t you avoid APPEND INITIAL LINE using
APPEND VALUE #( … ) TO mt_out ?
2) I note the 2 versions of your code are not equivalent.
Is req_date ever filled properly in the second version ?
<list> = VALUE #( req_date = determine_req_date( <list> ) ),
best regards,
JNN
Two times right! Thank you for the hint. i will correct this shortly.
Why not directly append to the internal table with
mt_out = VALUE #( (…) )?
Hi Suhas,
this woul be wrong: We are in a loop here. Your suggestion would elimintate previously appended lines.
Sorry, i didn’t know the context in which you are filling the internal table.
Not if you use the BASE addition with VALUE 😎 AFAIK, available from ABAP 740 SP08.
The expression should look like this:
That’s cool, I didn’t know this.