GPT4 Helps me Code

Spent some time yesterday giving some of my crappy code to GPT4 and asking it to make it better.

I gave it this: and asked “Can you simplify this code”

def form_to_data(form):
    form_data = []
    for name in form.fields:
        field_data = dict()
        field = form.fields[name]
        field_data['name'] = name
        field_data['type'] = field.widget.input_type
        field_data['label'] = field.label
        field_data['help'] = field.help_text
        form_data.append(field_data)
    return form_data

The response was pretty good.

def form_to_data(form):
    return [
        {
            'name': name,
            'type': field.widget.input_type,
            'label': field.label,
            'help': field.help_text
        }
        for name, field in form.fields.items()
    ]

Then I asked “Can you add type annotations?”

And got back:

from typing import List, Dict, Any
from some_module import Form, Field  # Replace 'some_module' with the actual module where Form and Field are defined

def form_to_data(form: Form) -> List[Dict[str, Any]]:
    return [
        {
            'name': name,
            'type': field.widget.input_type,
            'label': field.label,
            'help': field.help_text
        }
        for name, field in form.fields.items()
    ]

It also added this note:

Note:

  • Replace some_module with the actual module where Form and Field are defined.
  • The exact type of Form and Field depends on the specific library or framework you are using.
  • The Any type is used for the values in the dictionary because they can be of various types depending on the form field. If you have more specific types, you can replace Any with those.

The trace is in this session.

Pretty impressive. It seems to do a great job with common transformations on code that is pretty straightforward. How would it do with something that is messy or ugly?

Like this:

def idnum(a):
    b = "Number"
    if a == 3:
        b = "Three"
    if a > 8:
        b = "MoreThan8"
    if a >= 21:
        b = "MoreThan21"
    if a < 4:
        b = "LessThan4"
    return b

This code is bad and it doesn’t do anything useful or common, but its not too hard to follow if you are a human coder.

Let’s ask GPT to simplify:

def idnum(a):
    if a < 4:
        return "LessThan4"
    if a == 3:
        return "Three"
    if a > 8:
        return "MoreThan8"
    if a >= 21:
        return "MoreThan21"
    return "Number"

Now that code is simpler but it is also not going to get the same result. For a=100 this will return MoreThan8 but the original would return MoreThan21.

Let’s ask GPT to think about it

I asked: Will both versions of the code yield the same result?

It replied:

No, the two versions of the code will not always yield the same results due to the order and logic of the conditional statements. In the original code, the last condition checked (if a < 4) can override previous conditions. For example:

  • If a is 3, the original code returns "LessThan4", not "Three" because the if a < 4 condition comes last and overrides the if a == 3 condition.
  • For values greater than or equal to 21, the original code returns "MoreThan21", because the if a >= 21 condition comes before the if a < 4 condition.

In the simplified version, the first matching condition is returned immediately, and no further conditions are checked. This changes the behavior for some values. For example:

  • If a is 3, the simplified code will return "LessThan4", which is consistent with the original code.
  • However, for values greater than or equal to 21, the simplified code will return "MoreThan8" because the if a > 8 condition is checked before the if a >= 21 condition, and the function returns immediately after a condition is met.

To ensure both versions yield the same results, you need to carefully order the conditions and consider which ones should take precedence.

That’s a long explanation but it seems to get it right? So let’s ask it to try again:

Me: Can you write a simplified version which gets the same results as the original?

def idnum(a):
    if a >= 21:
        return "MoreThan21"
    if a > 8:
        return "MoreThan8"
    if a == 3:
        return "Three"
    if a < 4:
        return "LessThan4"
    return "Number"

This is just wrong in a new way. For a=3 this will return “Three” while the original returned LessThanFour. Again, this code is bad and contrived but just above it GPT seemed to “understand” but then goes on to make the same mistake.

What’s fun is you can ask GPT to run the code for its version and the original and get it to compare. Its still hard to get it to give a good result without a whole lot of coaching.

The full trace is in this session.

I tried to get it to write tests in another session.


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *