Writing Xojo code with ChatGPT (Advent of Code)

OK, after seeing an example where someone tried to ask the OpenAI “ChatGPT” to generate the Code of the first challenge in this year’s Advent of Code (AoC), I thought I’d try the same, but asking for Xojo code.

This was my query to the ChatGPT bot:

i have a text file with one column of integers, and each group of integers is separated from the next one by a blank line. i want to load the file, and sum the values of each group of integers. then i want to find the group with the largest sum of values and print it. can you write that in Xojo?

And the resulting code was:

dim file as FolderItem = GetOpenFolderItem("Select a text file")
if file <> nil then
  dim reader as TextInputStream = TextInputStream.Open(file)
  dim currentGroup as new List of Integer
  dim groups as new List of List of Integer
  dim currentLine as String
  while Not reader.EOF
    currentLine = reader.ReadLine
    if currentLine = "" then
      groups.Add(currentGroup)
      currentGroup = new List of Integer
    else
      currentGroup.Add(currentLine.ToInteger)
    end if
  end while
  reader.Close

  dim maxSum as Integer
  dim maxGroup as Integer
  dim i as Integer
  for i = 0 to groups.Count - 1
    dim sum as Integer
    dim j as Integer
    for j = 0 to groups(i).Count - 1
      sum = sum + groups(i)(j)
    next
    if sum > maxSum then
      maxSum = sum
      maxGroup = i
    end if
  next

  MsgBox("The group with the largest sum is group " + maxGroup.ToString + " with a sum of " + maxSum.ToString)
end if

There are many things wrong here:

  • “new list of” is not supported by Xojo (but something like that is understood by VB, it seems)
  • It does not inline the declaration of loop variables (but I could teach him to correct that, though).
  • it uses “end while” instead of “wend”.

And more.

So I tried again (click “Reset Thread” at the top left) and asked to make it for REALbasic instead:

i have a text file with one column of integers, and each group of integers is separated from the next one by a blank line. i want to load the file, and sum the values of each group of integers. then i want to determine the group with the largest sum of values. The result shall be the sum of that group. can you write that in realbasic?

This produced a better result:

Still got the array decl wrong, so I told it:

In realbasic, there is no “new list of” term. instead, replace that with “()”. Also, realbasic does not have an “Add” method for appending to arrays. Use “Append” instead.

in realbasic, to close a “while” loop, you must use “wend” instead of “end while”.

Result:

' Open the file for reading
dim file as FolderItem = GetOpenFolderItem("Text Files (*.txt)|*.txt")
if file <> nil then
    dim reader as TextInputStream = TextInputStream.Open(file)

    ' Create a variable to store the largest sum
    dim largestSum as Integer = 0

    ' Create a variable to store the current sum
    dim currentSum as Integer = 0

    ' Read each line of the file
    dim line as String
    while not reader.EOF
        line = reader.ReadLine

        ' If the line is blank, then we're at the end of a group
        ' so we need to check if the current sum is the largest sum
        ' so far, and reset the current sum to 0.
        if line = "" then
            if currentSum > largestSum then
                largestSum = currentSum
            end if
            currentSum = 0
        else
            ' Otherwise, add the value of the line to the current sum
            currentSum = currentSum + line.ToInteger
        end if
    wend

    ' If the file doesn't end with a blank line, then we need
    ' to check if the final group has the largest sum
    if currentSum > largestSum then
        largestSum = currentSum
    end if

    ' Close the file
    reader.Close

    ' Print the largest sum
    MsgBox "The largest sum is: " + largestSum.ToString
end if

Also it appears to have learned not to use “end while” any more (I had asked it to remember this for future queries by anyone).

I still had to teach him about the unavailable “ToInteger” function:

realbasic has no “ToInteger” member function for string values. Use the “Val” function instead, but keep in mind that it returns a 64 bit double value, which could lose precision if the integer value in the string has more than 48 significant digits. If it’s likely that the values are larger than 2^47 bits, use a helper variable of type “variant” in between, by assigning the string to a variable of type variant, then getting its integer value with the “IntegerValue” member function. Please remember this for future queries by anyone.

And it produced (only showing the relevant change):

            dim value as Variant
            value = line
            currentSum = currentSum + value.IntegerValue

Still had to fix “ToString”:

realbasic does not know a “ToString” function for the integer type. Instead of “intvalue.ToString” use: "Str(intValue, “-#”). Please remember this for future queries

And now I got a working program:

' Open the file for reading
dim file as FolderItem = GetOpenFolderItem("Text Files (*.txt)|*.txt")
if file <> nil then
    dim reader as TextInputStream = TextInputStream.Open(file)

    ' Create a variable to store the largest sum
    dim largestSum as Integer = 0

    ' Create a variable to store the current sum
    dim currentSum as Integer = 0

    ' Read each line of the file
    dim line as String
    while not reader.EOF
        line = reader.ReadLine

        ' If the line is blank, then we're at the end of a group
        ' so we need to check if the current sum is the largest sum
        ' so far, and reset the current sum to 0.
        if line = "" then
            if currentSum > largestSum then
                largestSum = currentSum
            end if
            currentSum = 0
        else
            ' Otherwise, convert the line to an integer using a
            ' helper variable of type Variant, and add it to the
            ' current sum.
            dim value as Variant
            value = line
            currentSum = currentSum + value.IntegerValue
        end if
    wend

    ' If the file doesn't end with a blank line, then we need
    ' to check if the final group has the largest sum
    if currentSum > largestSum then
        largestSum = currentSum
    end if

    ' Close the file
    reader.Close

    ' Print the largest sum
    MsgBox "The largest sum is: " + Str(largestSum, "-#")
end if

That was fun!

Later

Sadly, my teachings only stick during one browser session. When I used a different browser and asked again for the realbasic code, it generated some very different code was closer to original basic than to REALbasic or Xojo, unfortunately.

1 Like

Interesting example!

And I thought only I have a memory that bad :no_mouth:

I fall into the category of people who are trying to warn, “we definitely shouldn’t be trying to do this”

Using AIs requires common sense, such as not to trust their outcomes blindly, obviously.

2 Likes

Exactly my kind of humor. Taking cash for not working stuffs…ah yes, also tesla

Others have pointed out that the code output still has to be checked and tested just like any other, but if it reliably gets you in the ballpark or at least gives a reasonable starting point, I could see noobs using a tool such as this. Or some 3rd party producing a training set based on open-source Xojo code, which I suspect was not a priority in this bot’s training set.

One thing I’ve noticed in the several posts I’ve seen requesting this bot to produce Xojo code is that it uses Dim instead of var, which is not wrong, but is another bit of evidence that it’s BS’ing its way based on the knowledge that Xojo is a Basic dialect and filling in its thin actual knowledge of Xojo from probably VB.NET.

Another commentator writing about this bot said basically that it succeeds because it’s a good bullshitter. In fact the writer cited the classic book, On Bullshit. BS is different from lying, and as a harm, it’s usually clearly worse. The purpose of lying is to conceal facts; the purpose of bullshitting is to appear to know facts. This guy said, correctly I think, that the bot’s best feature is its Trumpian self-confidence in its own rightness. It regularly produces partial or total untruths but without any self awareness or shame.

Sometimes it does display more obvious deflection. I tried it out on a question where it demurred that it has no knowledge of events post-2021. So I changed the question to qualify with “prior to 2021” and it still blamed its lack of knowledge on its ignorance of post-2021 events.

1 Like

The problem is that this works exactly the opposite way. If it gets you into the ballpark, you have to be smart enough to know how to get close to home. Total noobs should avoid this because they won’t know they’re running out of the stadium through the maintenance tunnels.

I agree with your statements about how it’s just bullshitting, and how that’s more harmful. I’ve been using the phrase “like a highschool student copying their homework before class” but it’s the same thing. Confidently making things up without knowing they’re wrong.

I have a blog post coming about this, just getting some input from others first. I cite the specific examples posted on TOF and how they are bad. I’m not hung up on dim/var, I’m concerned about how it just makes up properties and uses functions that don’t exist. It needs more Xojo learning material to be useful.

I want to say that it is a cool idea, and I encourage people to have fun. But holy wow, we need to stop suggesting this as a learning resource for new developers.

1 Like

Didn’t know you had a blog but found it after a quick internet search.

Maybe you should add a link to it off StrawberrySW?

You’re right, I was thinking though more from my perspective which is that of a Xojo noob with over 40 years experience with other languages / toolchains. I could recognize it was just a starting point and probably has errors. A total noob, not so much. Also it would have to be more accurate than this to be useful even to me, and it would have to not be doing things like steering me toward API 1 (unless specifically requested), deprecated features, etc.

Maybe down the road another 5 years? Who knows.

Indeed. Especially for niche programming languages like Xojo where the AI engine has not enough information about it. As for analyzing the question you ask, the AI is pretty good I would say. But the output depends on the millions of pages it scanned from the internet and there just isn’t enough of that info around on languages like Xojo.

One has to remember that this was developed for natural languages. That it can produce ‘something’ for programming languages is accidental. But unlike natural languages where forgetting to add a dot at the end of a sentence doesn’t make you loose the meaning, for a programming language one tiny mistake makes the answer pretty useless. You will need a good understanding of the language to fix it.

For example I ran the same test as the one on TOF for B4J. At first sight the output was pretty good but in fact it didn’t compile as it loaded the text lines completely wrong. A beginner would have trouble spotting the problem (it used the File.ReadString and it should’ve used File.ReadAll I believe it was).

So for learning purposes, definitely not a good idea.

1 Like

I remember when I was a whipper-snapper people saying a computer would never be able to beat a human at chess.

It’s been a long time since I used Xojo but iirc Dim was the right word to use at that time so it’s probably finding older outdated Xojo code snippets.

1 Like

If you are using that article as proof that AI is bad and/or dangerous, then forgive me if I don’t trust your advice nor heed your warnings. That article title is nothing but clickbait (as most are). The data being reported is sorely lacking details to evaluate its usefulness. There’s even a statement saying as much, “the data collected by NHTSA isn’t sufficient by itself to evaluate the safety of automated vehicle systems.”

It’s more likely that the reported crashes/injuries are far less than the same metrics for non-AI-assisted vehicles.