Skip to Content

Technology Blog

Technology Blog

AI Adventures with GitHub Copilot

Recently updated on

A while back I wrote a blog post about my experiences using ChatGPT to help me upgrade a website.  As my regular readers will recall, the AI did help me with the project, but ChatGPT only works when given a prompt, and the more specific the prompt, the better.

I have also started using a second AI, "GitHub Copilot."   This AI can be directly integrated into the user's text editor, making the code itself the prompt.  When I start to type (and sometimes before!) Copilot will try to predict what I'm trying to do and offer me a suggestion, which I can accept using tab completion.

The function is best illustrated with some examples:

Example One

I was debugging some JavaScript and wanted to print some values to the error console.

The original code had the line:

    return el.getBoundingClientRect().height -
      parseInt(style.getPropertyValue('padding-top'), 10) -
      parseInt(style.getPropertyValue('padding-bottom'), 10);

But I wanted to know what those interior values were before returning them. So I added some local variables before printing them out:

   var bcr = el.getBoundingClientRect();
   var bcrh = el.getBoundingClientRect().height;

By the time I started to add the next line, Copilot had picked up the pattern, and offered:

    var bcrw = el.getBoundingClientRect().width;

which of course, was what I was going to type.

Now I wanted to print those values to the javascript console.  When I started to type console.log(... Copilot anticipated what I was going for by offering:

    console.log(bcr);

But I didn't want to try to print the rectangle object itself.  I just wanted the dimensions, *AND* I wanted to label it, so I overrode Copilot's suggestion with:

    console.log("BCRH");
    console.log(bcr.height);

After that, Copilot was familiar with my preference.  It offered:

    console.log("BCRW");
    console.log(bcr.width);

Even more impressively, when I later starting adding similar 'console.log' statements to other parts of the code, Copilot remembered that I liked the label first:

    console.log("SOME OTHER VALUE");
    console.log(some_other_value);

Then I decided the output was too noisy, and I wanted everything on one line.  I only had to add a colon to my initial print statement like this:

    console.log("SOME OTHER VALUE:

and Copilot filled the rest in for me:

    console.log("SOME OTHER VALUE: " + some_other_value);

Thus Copilot can save a lot of time otherwise spent typing or correcting fat-fingered typos.

Example Two

If you need to use a Python function that you're not super familiar with, chances are that Copilot knows it better than you do.  I wanted a quick and clean way to know the last day in a given month.  That is, I needed something that would know that March has 31 days, but that April only has 30; and that February, 2023 had 28 days, but that February, 2024 had 29 days.  That kind of thing.

It turns out there's a handy built-in python module called "calendar" for this.  I needed to put `import calendar` at the top of my module, but after that, Copilot knew just what to do.  It knew that the `monthrange()` method returns a two-tuple, and that the second element of that tuple was what I needed. It even knew to convert my year, which was a string, into an integer for use with the calendar module, and then to convert the result from `calendar` back into a string again. I just had to type:

    end_day =

and Copilot filled in the rest:

    end_day = str(calendar.monthrange(int(year), 12)[1])

Example Three

Another time I had to add "address", "city", and "state" fields to a form that already had "name" and "zip" fields.

In "models.py" I started to create the "address" field under the "name" field.  I had only typed the first few characters:

    name = models.CharField(max_length=175)
    addr                                   <-- typing here...
    zip = models.CharField(max_length=15)

before Copilot figured out what I was doing, and supplied the rest of the line, presumably pulling the value for 'max_length' from the line above.

    name = models.CharField(max_length=175)
    address = models.CharField(max_length=175)
    zip = models.CharField(max_length=15)

As for the "city" and "state" fields, I didn't have to type anything at all.  I just proceeded to the next line down, and Copilot handed it to me:

    name = models.CharField(max_length=175)
    address = models.CharField(max_length=175)
    city = models.CharField(max_length=75)
    state = models.CharField(max_length=2)

It was even smart enough to give the "state" field a "max_length" of 2, assuming (correctly) that I would be using state abbreviations for that field.

Those are some examples of how Copilot can be helpful.  But it is not all-knowing, and sometimes the things it can't figure out are surprising.  For example, after I added the "address", "city", and "state" fields to the model above, I opened the "forms.py" module in a new Emacs buffer, intending to add the new fields to the corresponding model form.  The list of fields already included "name" and "zip", since those fields already existed on the model, but starting to add an "address" field yielded nothing from Copilot.

On another occasion, I created a model in "models.py" with "email_address" and "created_at" as the only fields.  I then opened "admin.py" to add the fields to the model admin's "list_display" attribute.  Copilot was unable to recommend the names of the fields already defined on the model, and in fact suggested "peronsal_information" (which it came up with entirely on its own) as a candidate for the list display.

These latter examples show that Copilot doesn't always know best, especially when it needs information from a module other than the one you are currently working on.  It will also occasionally make suggestions that are outright wrong.  This can lead to new problems if you get into the habit of accepting Copilot's suggestions without carefully looking them over.

Example Four

As one final example, I was trying to test if the template file I was editing was really being used to render the page. (Sometimes this can be surprisingly tricky to determine).  Frustrated, I replaced the entire contents of the template file with the word "AARRRRGGHHH!!"  I wasn't even looking for suggestions from Copilot at that point, but it proposed adding a third exclamation point to round out my first two.  It didn't stop there, though.  It went on to suggest that I follow my outburst with the line:

 I have a 2000 323i and I have the same problem. I have been told that it is the fuel pump. I have not replaced it yet, but I will let you know if that fixes the problem.

In conclusion, you still have to keep your eye on Copilot, since you never really know what it's going to do. However, its suggestions frequently predict what I am thinking, sometimes spookily so.  It has been a helpful and time-saving tool, and I look forward to seeing it continue to improve.
 


Share , ,
If you're getting even a smidge of value from this post, would you please take a sec and share it? It really does help.