top of page
  • Writer's picturelauralikespi

ChatGPT Take 4 - Code for Me

Updated: Aug 11, 2023

I’ve had a good run trying to get ChatGPT to write a website blurb for me. I’ve got a few more ideas in that space, but for now I’m taking a detour into using ChatGPT for coding. Some consider writing code to be an art form, it is slightly more scientific than writing words for a website. There is a right and wrong, although there are multiple ways to get the right, so this could potentially be a chance for ChatGPT’s clinical tone to be of use.

Incase you missed the previous blog posts:

Finding Errors

The most logical thing I can think of using ChatGPT for when it comes to coding is finding errors or bugs in the code. Everyone who has written code more than once will be familiar with the frustration of code erroring or not working as intended, only for the culprit to be a missing comma or misspelt word. I imagine ChatGPT will be able to find such things with ease. But let’s have a play, as also I’m going to start with a simple (probably not great) command as I think that’s closer to how people use tools.


Let’s start with SQL as this is a very popular language for people to learn at the minute (particularly those who don’t know how to code). As I’m trying not to copy anything sensitive from my work SQL, I’m going to try writing dummy code for now (this is code with table_name, column_name, etc instead of the actual names).

L: Why does this code not work?


FROM table_name


Maybe a silly one to start with, but the answer was still intriguing. ChatGPT tried to tell me the issues was grouping by a numeric value and not a column name in the last line.

I do really like the way this answer is presented - although it isn’t actually correct.

Next I tried a slightly more correct version


SUM(column2) AS summed_value,

FROM table_name


This time ChatGPT told me the comma at the end of the row SUM(column2) AS summed_value, was the problem. Again, this is incorrect. My confidence is kind of waiving.

L: does your answer change if I'm using Google BigQuery?

ChatGPT: No, the general principle remains the same even if you are using Google BigQuery. The issue with the trailing comma in your original code would still cause a syntax error in Google BigQuery as well.

WRONG! Trust me, I use BigQuery every day.

L: The code will still run with a comma at the end of that row

ChatGPT: Apologies for the confusion. You are correct, in Google BigQuery, having a trailing comma at the end of a column in the SELECT statement is allowed. It does not cause a syntax error in BigQuery, unlike some other database systems.


L: GROUP BY 1 is also correct syntax

ChatGPT: I apologize for the confusion. You are correct that using GROUP BY 1 is valid syntax in some database systems, including Google BigQuery. In SQL, when using GROUP BY, you can refer to the columns in the SELECT clause by their position rather than their names. The position numbering starts at 1 for the first column, 2 for the second column, and so on.

L: what type of SQL were you using when you first responded?

ChatGPT: When I first responded, I provided a general explanation based on standard SQL conventions. The syntax and principles I discussed are applicable to most relational database systems that follow the SQL standard. However, without specifying a specific database system, I couldn't provide exact details about the behaviour of that particular system. SQL syntax and features can vary slightly across different database implementations. Nonetheless, the information I provided is generally applicable to standard SQL, and the correction I suggested should work in most SQL database systems.

Okay, I can accept these responses. While they were not 100% accurate, without knowing the programme I was using, ChatGPT gave answers which would work in most programmes / versions of SQL. While I can see this being useful for people, we are letting ourselves in for a whole host of badly written code by people who don’t understand it.


Being my usual resourceful (read: lazy) self, I found some old code on my laptop for a flocking algorithm. The output of this code (and some other code) can be found on my portfolio website. The code itself can be found on Github.

Tiny technicality to help understand - I copied the code from this specific JavaScript file (flocking.js) for this to work it needs to be called (asked to do it’s thing) by other files (a website HTML file or some other programming file). Basically this file cannot magically make circles flock without some other code.

So when I first copied this code into ChatGPT and told it the code was not running, I (rightly so) got the following response

ChatGPT: It seems that the code you provided is incomplete, as some parts are missing, such as the HTML elements and event listeners for the buttons. Without the complete code and the necessary HTML structure, it is difficult to identify the exact issue or provide a solution. To help you further, could you please provide the complete code, including the HTML structure, button event listeners, and any other relevant parts? Additionally, let me know if you encounter any specific error messages or unexpected behavior when running the code. With that information, I'll be better able to assist you in resolving the issue.

I had tried removing some semi-colons to break the code (forgetting JavaScript can actually add these for you, a very useful feature).

When I renamed one of the parts of the code - from Boid to Boidd - and copied the code in again (saying it was not working), ChatGPT still could not tell me what was wrong. It did ask for the error message:

ChatGPT: Could you please specify the error you're encountering or provide more details about the issue you're facing? With more information, I'll be better able to assist you in resolving the problem.

L: something about Boidd() not existing

ChatGPT: I apologize for the confusion. It appears that there is a typo in the code. The correct class name is Boid, not Boidd. Please replace Boidd with Boid in the code.

Here's the corrected line of code:

boids[i] = new Boid(); // Instead of new Boidd();

Once you make this change, the code should work as expected. Let me know if you have any further questions!

Well done ChatGPT, you found the error! This could be useful for someone trying to debug their code.

Side note - understanding code:

While I was trying to get ChatGPT to find errors in the code, I accidentally hit enter too early and just sent the code itself. The response from ChatGPT was to summarise what the code did.

ChatGPT: It seems that the code you provided is a simulation of a flocking behavior, where boid objects move and interact with each other based on certain rules. Here's a breakdown of the code and its functionality:

1. The code sets up various variables such as the number of boids (n), width (w) and height (h) of the simulation area.

2. The `Boid` class is defined, which represents an individual boid with its coordinates (x and y) and a direction angle (theta).

3. The `for` loop initializes an array called `boids` with `n` instances of the `Boid` class, randomly setting their coordinates and direction.

4. The `plotBoids` function extracts the x and y coordinates of all boids and returns them as separate arrays.

5. The `distance_matrix` function calculates the Euclidean distances between boids and stores them in a matrix.

6. The `neighbourhood` function determines the neighbors of each boid based on a given radius. It calculates the alignment, cohesion, and separation forces for each boid and updates their positions accordingly.

7. The `cohesion`, `separation`, and `alignment` functions calculate the corresponding forces based on the neighboring boids and return the magnitude and angle of those forces.

8. The code uses the Plotly library to visualize the boids' positions in a scatter plot. The `graph` variable represents the plot, and the `updatePlot` function updates the plot with the current boid positions.

9. The `updateSim` function is called repeatedly to update the simulation by invoking the `neighbourhood` and `updatePlot` functions.

10. The `updating` variable is a timer that repeatedly calls the `updateSim` function every 50 milliseconds.

11. Event handlers for the start, stop, and restart buttons are defined. The `startSim` function starts the simulation, `stopSim` stops it, and `restartSim` restarts it with new parameters.

12. The start button is initially disabled, and the stop and restart buttons are enabled. When the simulation is started or stopped, the corresponding button states are updated.

That's a brief overview of the code. If you have any specific questions or need further clarification, feel free to ask!

This has been the thing which has impressed me most about ChatGPT so far - I can see several uses for this: 1. A junior or non technical person trying to understand some code

2. Helping create a blueprint (or rushed) technical documentation

Writing Code

This blog post was inspired by a colleague of mine who is a Product Manager. [They have kindly allowed me to share this story, and sent some prompts and screenshots to include in this blog post]

They tagged the data team with a question about some data in a Google Sheet. The Google Sheet contained some SQL code which I was not expecting, but was very impressed. However, the SQL code looked weird and being the wonderfully helpful data person, always trying to help people learn (read: a pain in the ass), I reached out with some tips only to be told ChatGPT wrote the code!

The SQL code in question (which some redaction to protect our work data tables):

  FORMAT_TIMESTAMP('%H:00:00', transaction_created_timestamp) AS hour_of_day,
  SUM(amount) AS total_losses,
  count(distinct transaction_id) AS count_txns,

FROM table_name

WHERE transaction_id IS NOT NULL
  and transaction_created_timestamp > "2023-01-01 12:51:31.142000 UTC"

GROUP BY hour_of_day

The lines which stuck out to me were the ones related to timestamps (a notoriously difficult aspect of any SQL coding).

Before we correct them, let’s take a look how my colleague got this code:

PM: Edit this sql so it groups the timestamps by hour of day (the date is irrelevant)


FROM `my-table`

where transaction_id is not null
group by transaction_created_timestamp

Let’s take a minute to appreciate how great this is - rather than having to contact the data team, or spend ages looking through documentation, Google or StackOverflow, people can use an existing SQL query in ChatGPT to get what they need.

ChatGPT usefully returned some code (with some explainers). Based on our previous examples, do we think this is going to work?

Joke - How you can tell this example was actually the PM and not me? Dark mode is turned off.

Shockingly it did not, and my colleague was able to copy the error message back in (and cleverly included the programme, BigQuery, they were using).

PM: in bigquery I get Function not found: DATE_FORMAT at [5:5]

ChatGPT again output some code and some more explanations.

This continues until the SQL code above was output. This code does work, but if you ever write SQL code, you would notice how strange the lines related to timestamps work.

Rather than writing the very long timestamp out (transaction_created_timestamp > "2023-01-01 12:51:31.142000 UTC"), we can use a date function to get: DATE(transaction_created_timestamp) >= "2023-01-01"

Similarly, instead of a format function (FORMAT_TIMESTAMP('%H:00:00', transaction_created_timestamp) AS hour_of_day,), we can use an extract function:

EXTRACT(HOUR FROM transaction_created_timestamp) AS hour_of_day

These functions allow the code to be a bit more flexible, easier to edit and reduce the potential for human errors.

Aside from these small peculiarities, I can really see how this aspect of ChatGPT can be used in businesses to allow people get the data they need (while freeing up data people).

Testing Code

Because I am always talking about ChatGPT at the minute, people are always telling me how they use it. At a Meetup, some mentioned using ChatGPT to write tests for their code. Let’s try it out, going back to the chat with the Flocking code.

L: can you write tests for this code?

ChatGPT: Certainly! Here are some sample test cases you can use to verify the functionality of the provided code:

These test cases cover various functions in the code and help verify if they return the expected results. Feel free to modify them or add additional tests as needed.

Another opportunity for ChatGPT to augment human intelligence and help developers speed up their work by writing tests (to be checked and implemented by actual humans).

Let’s Ask the SuperBrain

If you’ve been following my previous blog posts, you’ll know I’ve missed my own top advice - ask ChatGPT how to use it. So let’s try that out -

L: How can I best use you to help with coding?

ChatGPT: As an AI language model, I can assist you with various aspects of coding. Here are some ways you can make the most of my capabilities to help you with coding:

1. Syntax and language understanding: I can provide information on programming languages, explain syntax rules, and help you understand how to structure your code.

2. Code examples and snippets: If you're stuck on a specific problem or need a code snippet for a particular task, describe your requirements, and I can generate example code for you.

3. Troubleshooting and debugging: If you encounter errors or issues in your code, you can share the relevant code snippet and the error message with me. I can try to help you identify the problem and suggest potential solutions.

4. Algorithm and data structure explanations: If you're working on algorithms or data structures, I can explain the concepts behind them and provide insights into their implementation.

5. Design patterns and best practices: I can discuss software design patterns and coding best practices to help you write clean, efficient, and maintainable code.

6. Project guidance: If you're working on a larger coding project, I can provide guidance on project structure, organization, and recommended tools or frameworks.

7. API documentation and usage: If you're integrating with an API or working with a specific library, I can help you understand the documentation and assist with code examples for interacting with the API.

8. Learning resources: If you're looking for coding tutorials, documentation, or learning resources on a particular topic, I can recommend relevant websites, books, or online courses.

Remember that while I can provide assistance, it's important to validate and test the code yourself, as I don't have real-time access to the latest libraries, frameworks, or programming environments.

Feel free to ask specific coding questions or provide code snippets, and I'll do my best to assist you!

I also asked

L: Any specific tips for using you for SQL coding?

And ChatGPT got stuck in the thinking phase. I think I broke it (read: there was a small technical error and I’m impatient), so let’s leave this blog post here.


Please note, this was my first play around (although I will admit I am impressed) - I will try again with advice from ChatGPT itself. For now, here are my thoughts:

  1. It’s not a data analyst - I’m not quaking in my boots about ChatGPT taking my data job just yet (not only could it not write correct SQL code off the bat). There is a lot of context needed in any data and coding work - human and business context that I cannot see ChatGPT (or other algorithms) exhibiting.

  2. Potential uses - understand code someone else has written, write documentation and tests.

  3. ChatGPT can write you working code - whether it is the best or easy to understand or if it can teach someone to code, I remain unconvinced.

48 views0 comments


bottom of page