Scrutinizing ABAP GenAI example from SAP Sapphire keynote
Below is the code example from yesterdays SAP Sapphire Keynote,
At the point of writing, its not possible to replay the keynote, as it was taken down from YouTube, and not available on the Sapphire Virtual homepage.
But, given a prompt the above code was generated. This happened at the main keynote, the feature was announced 6+ months ago, and this being a keynote I think its okay to look into every detail of it. At a keynote SAP has full control of what to show and how, and I do assume there is a review step as to what goes into the keynote.
On top of this, SAP advocates clean core, clean code, clean everything, so what SAP shows on the keynote I assume SAP certifies as clean.
Lets go,
A: Indentation is off
Indentation in ABAP is typically 2 spaces, looking at line 2 its one space, while line 14 and 15 are actually aligned vertically its still one off. Line 19 and 20 doesn't follow the lines after it inside the IF
B: Hungarian notation mix
Using Hungarian notation or not is a big topic in ABAP. However most agree that mixing both in the same code is bad practice.
C: Parentheses
In line 20, the leftmost and right most parentheses can be removed.
D: Types
Using "TYPE p DECIMALS 2" is not something I've seen very often, and looking in the parameters indicates that the fields are "Decfloat" which is a different type.
This might be an error and worst case cause dumps, might also be okay. I don't have access to a system to verify.
领英推荐
E: lv_error is unused
The variable "lv_error" is unused
F: Comments
Like line 1, adding a comment that the DATA is declared below it, no need to. Add comments where it adds value, don't replicate whats below it
H: CORRESPONDING #
MOVE-CORRESPONDING works, but CORRESPONDING # is more modern, and considered cleaner by some developers
I: Inline declarations
In modern ABAP, variables can be declared inline, increasing readability
J: Line 11?
Line 11 jumps out, as it moves "element attributes" to "result amounts", this might be okay, I guess its used to fill some key fields in the result amounts. But it might risk moving too much depending on the types(which I dont have access to check)
K: Use "-="
The modern subtracting move "-=" can be used to reduce the amount of code
Just put the example into https://playground.abaplint.org, and it will hint the developer with many of the above issues.
So, what about something like the below, did I forget anything?
item_result_amounts = CORRESPONDING #( item_amounts ).
item_result_attributes = CORRESPONDING #( item_attributes ).
item_result_quantities = CORRESPONDING #( item_quantities ).
prcg_element_result_amounts = CORRESPONDING #( prcg_element_attributes ).
active_price_result_amounts = CORRESPONDING #( active_price_amounts ).
DATA(lv_temperature) = item_attributes-yy_temperature_pci.
" when the weather is good, people buy more ice cream,
" so we reduce the price
IF lv_temperature > 20.
prcg_element_result_amounts-conditionamount -=
( lv_temperature - 20 ) / 2 ) *
( prcg_element_attributes-conditionamount / 10 ).
ENDIF.
Experienced full-stack web developer with 20+ yrs in the field. Proficient in React, JavaScript, Node.js, PHP & MySQL. 7 yrs in Linux administration & 3.5 years in business software development, incl. 2.5 yrs in SAP.
8 个月This is in line with my experience when I use some modern LLM, which deliver very similar results. One difference is perhaps copilot, it can show sources - and that's really helpful.
SAP ABAP Developer, software architect, clean code enthusiast
8 个月SAP should be more responsible in showing any code. It started the clean code initiative, but it doesn't support it. I think each and every piece of code (not only AI-generated), that is presented on SAP official events should follow the principles.
SAP integration veteran | Member of the SAP Mentors program | SAP PRESS author | SAP community enthusiast
8 个月Nitpicking the code is a fun exercise, of course, but for discussing the value of GenAI code generation, I don't think it's that useful. A more interesting question would be: Will using the GenAI output as a starting point get the median ABAP developer to working code of sufficient quality faster than starting from scratch? I think the answer is very likely yes, but I'd really like to hear your take, Lars.
Expert SAP Developer * Chief Nerdess at Boring Enterprise Nerds * SAP Mentor Alumna * Book author * Conference speaker * The First of Her Name * Protector of The ABAP Realm
8 个月Lars Hvam Petersen Thanks for writing about this! I'm yet to watch the keynote and I guess now someone is editing this out in a hurry. :) (Conspiracy theories ensue!) I usually try very hard to be professional in the code reviews but if this was produced by a human developer, there is a big chance my whole review would be "what is this s*t?" On top of that, this example is probably confusing to the whole US SAP customer base because we measure temperature in Farenheit. And 20 F is very cold. Also, there is a reason that the amount fields have special currency type in SAP. There are a few oddball currencies (JPY is the most notable example) that don't use decimals and the amounts are actually stored using a "factor". I believe all of that complexity still exists in S/4HANA. I get it that some simplification is needed when just showing an example. But that's the result of example being very primitive to begin with.
SAP ABAP(+RAP), Fiori Elements. S4/HANA , BTP Technical Architect , ABAP Cloud , ABAP Push/Messaging Channel
8 个月Chain statements can be used instead of multiple DATA and Move-Corresponding