Personal Insights
ABAP Know How: 3 lines of code, 2 commented out
Sometimes, “little chunks of knowledge” can be as little as a few lines of code.
And most of them are commented out.
With this blog post, I’ll give you an example for it, explain why keeping them somewhere is useful for me, and invite you to let me know if you have similar cases.
My example is this:
* data lo_report TYPE REF TO lcl_report.
* CREATE OBJECT lo_report.
DATA(lo_report) = NEW lcl_report( ).
Explanation:
Defining a reference variable to a class,
and creating an instance of that class (that is: creating an object of that class),
and assigning that object to the reference variable
used to be 2 lines of code.
But it now can be done just as well in one line of code.
I try to use the new version, but I’m not always sure on first try.
Like, all those don’t work, and I remember failing with a least a few of them:
*FALSE examples:
lo_report = new lcl_report( )
DATA(lo_report) = NEW lcl_report.
DATA(lo_report) = NEW lcl_report().
DATA(lo_report) = NEW lcl_report( )
So I think it’s clear, that we benefit from having those “3lines of code, 2 commented out” examples.
But I don’t want them in my coding!
1. I don’t want useless comments. I’ll delete them!
2. How would I find such an example again? (I know I did that somewhere, but where?)
So, this all leads to my plan: I’ll use a SAP Community blog post for those things:
1. It’s the right place
2. I don’t need screenshots, my sources can be #textOnly!
3. it still looks nice, just like in the editor, as I can put it in a code block.
4. I can add new things by editing or as a comment.
5. Maybe most important: publicly shared knowledge is good knowledge: I have a convenient place to look things up again and others can also benefit.
What do you think about that?
Best
Joachim
Awesome idea!!! You can find it again - and we can learn from it.
Thanks a lot for your Feedback, Michelle Crapo !
I got another one right here:
Already when writeing that 'I' and 'EQ' I thought: there's probably constants for that?!.
Now I know:
I am afraid these constants are not globally available in whole system?
Anyway here is one line version for you
And here also replacing LOOP with FOR statement:
Those constants are defined in TYPE-POOL wmegc. - to me they seem to be globally available, but I'm not sure. (I noticed, code completion in AdT does not work for them).
Also, thanks for the alternate versions Tomas Buryanek , I appreciate those a lot!
Two ways to raise an exception with a MESSAGE: (one commented out)
The above is short, to keep the format.
This is a little longer, to see some more context:
Not sure if I explicitly said this already: this kinds of post often document my failures: I wrote some code and syntaxcheck said no.
So those codeblocks show the way(s) something don't work (commented out), and the way(s) how it does work.
Also should work: (as the lref_stock_mover is defined + type already)
Or define and type in one go:
Problem:
"not translated: (SLIN)
Solution:
Oh no, actually another Problem:
Syntax-Check:
"'va'(003)" is not type-compatible with formal parameter "IV_WHAT_IS_IT".
(Parameter if of type string).
Solution(?) with CONV :
Syntax-Check:
The text ID is not three characters long.
Solution: Add a space and we are fine:
Conclusion: Things (ATC/SLIN, Syntax-Check) are fine now, but it looks a lot less clean, compared to the initial state, with all the brackets.... )
Q: How to get a sub-set of an internal table?
A1: with VALUE FOR:
A2: with FILTER (needs a KEY on the field being filtered for) :