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.
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?
I’d appreciate to know your thoughts!
All the best