Skip to Content
Author's profile photo Jörg Krause

On programming style: the use of constructor expressions – VALUE

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:

  1. Without using VALUE:

  2. Using VALUE:

    /wp-content/uploads/2016/08/img1_1014994.png

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

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jacques Nomssi
      Jacques Nomssi

      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

      Author's profile photo Jörg Krause
      Jörg Krause
      Blog Post Author

      Two times right! Thank you for the hint. i will correct this shortly.

      Author's profile photo Former Member
      Former Member

      1) to take it further, couldn't you avoid APPEND INITIAL LINE using

      APPEND VALUE #( ... ) TO mt_out ?

      Why not directly append to the internal table with

      mt_out = VALUE #( (...) )?

      Author's profile photo Jörg Krause
      Jörg Krause
      Blog Post Author

      Hi Suhas,

      this woul be wrong: We are in a loop here. Your suggestion would elimintate previously appended lines.

      Author's profile photo Former Member
      Former Member

      Sorry, i didn't know the context in which you are filling the internal table.

      Jörg Krause wrote:

      this would be wrong: We are in a loop here. Your suggestion would elimintate previously appended lines.

      Not if you use the BASE addition with VALUE 😎 AFAIK, available from ABAP 740 SP08.


      The expression should look like this:

      mt_out = VALUE #( BASE mt_out (...) )

      Author's profile photo Jörg Krause
      Jörg Krause
      Blog Post Author

      That's cool, I didn't know this.