Skip to Content
Technical Articles
Author's profile photo Prasanth Padmanabhan Menon

A curious case of Chat GPT and ABAP

Introduction

I recently came across an interesting and excellent blog series written by Kallol Chakraborty , https://blogs.sap.com/2022/12/20/easy-way-to-write-algorithms-in-abap-series-01/ wherein he is discussing about writing algorithms in ABAP.

Now that Chat GPT is out, we have already seen a lot of use cases where many developers have used Chat GPT to generate code from one language to another. I thought, why not use Chat GPT and see how efficiently it could convert a piece of code to ABAP.

The Prompt

I logged in to leetcode and searched for an algorithm to test this out.

I took the example of grouping anagrams as follows and then, gave the following, as input to Chat GPT for converting it to ABAP

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        if (strs.length == 0) return new ArrayList();
        Map<String, List> ans = new HashMap<String, List>();
        int[] count = new int[26];
        for (String s : strs) {
            Arrays.fill(count, 0);
            for (char c : s.toCharArray()) count[c - 'a']++;

            StringBuilder sb = new StringBuilder("");
            for (int i = 0; i < 26; i++) {
                sb.append('#');
                sb.append(count[i]);
            }
            String key = sb.toString();
            if (!ans.containsKey(key)) ans.put(key, new ArrayList());
            ans.get(key).add(s);
        }
        return new ArrayList(ans.values());
    }
}

The following is the output in ABAP, that I got as a response from Chat GPT.

You can find the video of the prompt I gave, given below.

Now, the generated output is not that far from being accurate with some minor syntax errors which can be fixed by a skilled developer. But what surprised me is the fact that it was able to identify similar syntaxes in ABAP and write that down.

Conclusion

This opens a huge avenue of opportunities where code can be swapped across multiple languages. The possibilities are endless, and I am excited for what the future holds for the ABAP and the developer community as a whole.

Assigned Tags

      11 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      This is the code from the screenshot above, so that ABAP developers can check by themselves:

      CLASS solution DEFINITION.
        PUBLIC SECTION.
          METHODS: group_anagrams IMPORTING strs TYPE string_table
                             RETURNING VALUE(rv_anagrams) TYPE t_t_string_table.
      ENDCLASS.
      
      CLASS solution IMPLEMENTATION.
        METHOD group_anagrams.
          DATA: ans TYPE t_t_string_table,
                ca TYPE string,
                key TYPE string.
          IF lines( strs ) = 0.
            rv_anagrams = VALUE #( ).
            RETURN.
          ENDIF.
          CREATE DATA ans TYPE HASHED TABLE OF t_string_table
                            WITH UNIQUE KEY string.
          LOOP AT strs INTO ca.
            SORT ca BY character.
            key = ca.
            IF NOT ans IS INITIAL.
              INSERT VALUE #( ca ) INTO TABLE ans-values INDEX key.
            ELSE.
              INSERT VALUE #( ( ca ) ) INTO TABLE ans INDEX key.
            ENDIF.
          ENDLOOP.
          rv_anagrams = ans-values.
        ENDMETHOD.
      ENDCLASS.

      That's the test class:

      CLASS ltc_main DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
        PRIVATE SECTION.
          METHODS test FOR TESTING.
      ENDCLASS.
      CLASS ltc_main IMPLEMENTATION.
        METHOD test.
          DATA(words) = VALUE string_table( 
              ( `are` ) ( `bat` ) ( `ear` ) ( `code` ) ( `tab` ) ( `era` ) ).
          DATA(answer) = NEW solution( )->group_anagrams( words ).
          cl_abap_unit_assert=>assert_equals( act = answer exp = VALUE solution=>t_t_string_table(
              ( VALUE #( ( `are` ) ( `ear` ) ( `era` ) ) )
              ( VALUE #( ( `bat` ) ( `tab` ) ) )
              ( VALUE #( ( `code` ) ) ) ) ).
        ENDMETHOD.
      ENDCLASS.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      Below what I would expect from a developer who would convert the java algorithm line by line. That's a code which works, each line is more or less equivalent to the java code:

      CLASS solution DEFINITION.
        PUBLIC SECTION.
          TYPES: t_t_string_table TYPE STANDARD TABLE OF string_table WITH DEFAULT KEY.
          METHODS: group_anagrams IMPORTING strs               TYPE string_table
                                  RETURNING VALUE(rv_anagrams) TYPE t_t_string_table.
      ENDCLASS.
      
      CLASS solution IMPLEMENTATION.
        METHOD group_anagrams.
          TYPES: ty_count TYPE STANDARD TABLE OF i WITH EMPTY KEY,
                 BEGIN OF ty_ans_line,
                   mapkey   TYPE string,
                   mapvalue TYPE string_table,
                 END OF ty_ans_line,
                 ty_ans TYPE HASHED TABLE OF ty_ans_line WITH UNIQUE KEY mapkey.
          IF lines( strs ) = 0. rv_anagrams = VALUE #( ). RETURN. ENDIF.
          DATA(ans) = VALUE ty_ans( ).
          DATA(count) = VALUE ty_count( FOR x = 0 WHILE x < 26 ( ) ).
          LOOP AT strs INTO DATA(s).
            DO lines( count ) TIMES. count[ sy-index ] = 0. ENDDO.
            LOOP AT VALUE string_table( FOR off = 0 WHILE off < strlen( s ) ( substring( val = s off = off len = 1 ) ) ) INTO DATA(c).
              DATA(index) = cl_abap_conv_out_ce=>uccpi( c ) - cl_abap_conv_out_ce=>uccpi( 'a' ) + 1.
              count[ index ] = count[ index ] + 1.
            ENDLOOP.
            DATA(sb) = VALUE string_table( ).
            DO 26 TIMES.
              APPEND `#` TO sb.
              APPEND |{ count[ sy-index ] }| TO sb.
            ENDDO.
            DATA(key) = concat_lines_of( table = sb ).
            IF NOT line_exists( ans[ mapkey = key ] ). INSERT VALUE #( mapkey = key ) INTO TABLE ans. ENDIF.
            ASSIGN ans[ mapkey = key ]-mapvalue TO FIELD-SYMBOL(<mapvalue>).
            INSERT s INTO TABLE <mapvalue>.
          ENDLOOP.
          rv_anagrams = VALUE #( FOR <ans_line> IN ans ( <ans_line>-mapvalue ) ).
        ENDMETHOD.
      ENDCLASS.
      Author's profile photo Prasanth Padmanabhan Menon
      Prasanth Padmanabhan Menon
      Blog Post Author

      Hi Sandra,

       

      Thank you for this. The human touch is clearly evident here.

       

      Best Regards,

      Prasanth

      Author's profile photo Gagan Jangir
      Gagan Jangir

      Thanks a lot Prasanth Padmanabhan Menon for the encouragement and Discussion.

      Author's profile photo Sandra Rossi
      Sandra Rossi

      I think you're a little too bit optimistic when you say "the generated output is not that far from being accurate with some minor syntax errors which can be fixed by a skilled developer."

      Here is the version of ChatGPT corrected by me with the fewest changes possible:

      CLASS solution DEFINITION.
        PUBLIC SECTION.
          TYPES: t_t_string_table TYPE STANDARD TABLE OF string_table WITH DEFAULT KEY.
          METHODS: group_anagrams IMPORTING strs TYPE string_table
                             RETURNING VALUE(rv_anagrams) TYPE t_t_string_table.
      ENDCLASS.
      
      CLASS solution IMPLEMENTATION.
        METHOD group_anagrams.
          TYPES: BEGIN OF t_string_table,
                   string   TYPE string,
                   mapvalue TYPE string_table,
                 END OF t_string_table,
                 t_string_table_2 TYPE HASHED TABLE OF t_string_table WITH UNIQUE KEY string.
          DATA: ans TYPE REF TO t_string_table_2,
                key TYPE string.
          IF lines( strs ) = 0.
            rv_anagrams = VALUE #( ).
            RETURN.
          ENDIF.
          CREATE DATA ans TYPE HASHED TABLE OF t_string_table
                            WITH UNIQUE KEY string.
          LOOP AT strs INTO DATA(s).
            DATA(ca) = VALUE string_table( FOR off = 0 WHILE off < strlen( s ) ( substring( val = s off = off len = 1 ) ) ).
            SORT ca BY table_line.
            key = concat_lines_of( table = ca ).
            IF NOT line_exists( ans->*[ string = key ] ).
              INSERT VALUE #( string = key ) INTO TABLE ans->*.
            ENDIF.
            ASSIGN ans->*[ string = key ]-mapvalue TO FIELD-SYMBOL(<mapvalue>).
            INSERT s INTO TABLE <mapvalue>.
          ENDLOOP.
          rv_anagrams = VALUE #( FOR <ans_line> IN ans->* ( <ans_line>-mapvalue ) ).
        ENDMETHOD.
      ENDCLASS.

      What the differences are:

      To do these fixes, I had to understand the algorithm (from the java code for instance). It was impossible for me to FIRST understand the crazy code generated by ChatGPT.

      After that, I had to understand which parts of ChatGPT code correspond to which part of the algorithm, and I could find the minimal changes.

      Conclusion: it took me 3x more time to find how to fix the program with minimal changes rather than writing the algorithm from scratch.

      But I guess ChatGPT could be trained with ABAP code equivalent to java code and maybe that would result in more reusable code in the future...

      Author's profile photo Prasanth Padmanabhan Menon
      Prasanth Padmanabhan Menon
      Blog Post Author

      Hi Sandra,

      You are right that I was a being a bit too optimistic and the minimal changes weren't actually that minimal as you have demonstrated here(The screenshot you have given for the code comparison really puts things in perspective).

      But I do believe that the model will definitely become more capable by reading equivalent ABAP code.

      Best Regards.

      Prasanth

      Author's profile photo Julie Plummer
      Julie Plummer

      Thank you for your service, Sandra Rossi !

      Your experience is pretty much identical to mine as a technical translator, correcting machine translations 20-ish years ago - much slower and more frustrating than just translating from scratch.

      (In fact, the manager who gave us the task was so embarrassed by our workload, he threw a party in the Walldorf Party Room in Building 5, with a free bar - and served the drinks himself!)

      Machine translation has improved massively ofc, and has its uses providing rough-and-ready information from languages I don't speak.

      But it's still:

      1. Very domain-specific, and reliant on huge training data sets
      2. 80/20 - still needs 20% human post-processing

      Given that software, unlike human texts (eg newspapers), can be mission-critical, I'm pretty dubious about this - at present.

      Just a personal view.

      Best wishes,
      Julie.

      Author's profile photo Matthew Billingham
      Matthew Billingham

      https://www.theregister.com/2022/12/05/stack_overflow_bans_chatgpt/

      Stack Overflow bans ChatGPT as 'substantially harmful' for coding issues

      OpenAI's question-answering bot, ChatGPT, isn't smart enough for the team at Stack Overflow, who today announced a temporary ban on answers generated by the AI bot because of how frequently it's wrong.

       

      If you rely on ChatGPT, then you're going to have very buggy code. Probably best avoided. On the plus side, stupid-criminals are trying to use it as an attack vector.

      Frankly, even getting humans to produce good code is a challenge. Having an AI do it, based on a million examples of not very code isn't going to do any better. As Sandra Rossi  points out - it requires analysis. ChatGPT doesn't do analysis. It chats in a (scarily) believable way!

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Thanks for that very interesting article + Stack Overflow policy about GPT.

      There's something I don't see clearly in this article and in Stack Overflow:

      I fear that in a very near futuret the questions asked in the forums by beginners would contain stupid code produced by GPT and asking "what is to be fixed". That will make people less and less trying to think, understand and learn, it's why it's important in our answers to not give directly the final code but make people learn how to find by themselves.

      I hope these forums would sanction the people who are asking (repeatedly) questions about generated GPT code.

      NB: in case you don't find the source easily from The Register article (link at the word "said"):

       

      Author's profile photo Lakshmi Sankaran
      Lakshmi Sankaran

      From my perspective any AI algorithms anyway need human analysis and this is not a surprise if you spend time analysis of your own bugs or the others. In all ways it is good move AI like ChatGPT just started to do the revolution here. Fingers crossed as more such models are in store including ones to defend security threats on such tools. So await cool future ahead of us as for analysis professionals than coders who do work from scratch ? Happy ABAP joined this wagon.

      Author's profile photo Kallol Chakraborty
      Kallol Chakraborty

      Dear @Prasanth Padmanabhan Menon,

      Thanks for checking out my blogpost. 🙂

      Yes, I have tried ChatGPT with ABAP. I can say, it's quite good. But I am curious about passing the test cases.

      Regards,

      Kallol