Is ChatGPT a game change for Software Engineering?

William Kimeria
3 min readDec 20, 2022

--

A couple of weeks ago, a friend said he would miss my puns if and when I left Twitter but he would subscribe to my puns if I ever wrote a service for it. Since I am re-learning GoLang (after using it for a tiny bit 4+ years ago), I decided just for fun to see what it would take to write a PAAS (Puns as a Service) in Go (and host on Heroku, which for me is the fastest way to get things up and running). I remembered I had been playing around with ChatGPT a few days ago. I wondered; how good is ChatGTP’s code generation (I had asked it to implement a number of algorithms in specific languages).

For example, I asked it to write code to find the first 1 million prime numbers using the sieve of Eratosthenes in a number of different programming languages (GoLang, C++ and even Assembly). For example, here is the prompt for C++ and GoLang (also note that I mis-spelled Eratosthenes in my prompt)

(side note, this will wreak havoc with recruiters and companies who insist on algorithmic coding challenges)

All good and fun. What if I asked ChatGPT to write a more complicated program?

Here is my prompt (the whole answer is longer than will fit into a screenshot so I copied my prompt and ChatGPTs answers)

Build me a REST API service written in GoLang where the base url is “puns”. It reads the puns from a PostgreSQL database and returns a random one. Also, when it fetches a pun, it caches it in Redis by id with a TTL of 1 hour.

And ChatGPT’s response (note the conversational aspect as well as the code generated)

  1. First, you’ll need to install the necessary dependencies. You’ll need the Go PostgreSQL driver to connect to the PostgreSQL database and the Go Redis client to connect to Redis. You can install these dependencies by running the following commands:
go get github.com/lib/pq 
go get github.com/go-redis/redis/v7

2. Next, you’ll need to import the necessary packages and define the base URL for the API.

package main

import (
"database/sql"
"fmt"
"log"
"math/rand"
"net/http"
"time"

"github.com/go-redis/redis/v7"
_ "github.com/lib/pq"
)

const baseURL = "puns"

func main() {
// Connect to the PostgreSQL database and Redis cache
db, err := sql.Open("postgres", "user=postgres password=postgres dbname=puns sslmode=disable")
if err != nil {
log.Fatal(err)
}
defer db.Close()

cache := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})

// Define the routes for the API
http.HandleFunc("/"+baseURL+"/random", func(w http.ResponseWriter, r *http.Request) {
handleRandomPun(w, r, db, cache)
})

// Start the server
log.Fatal(http.ListenAndServe(":8080", nil))
}

3. Next, you’ll need to define a function to handle the /random route of the API. This function will fetch a random pun from the PostgreSQL database, cache it in Redis, and return it to the client.

func handleRandomPun(w http.ResponseWriter, r *http.Request, db *sql.DB, cache *redis.Client) {
// Query the database for a random pun
var id int
var pun string
err := db.QueryRow("SELECT id, pun FROM puns ORDER BY RANDOM() LIMIT 1").Scan(&id, &pun)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Cache the pun in Redis with a TTL of 1 hour
err = cache.Set(fmt.Sprintf("pun:%d", id), pun, time.Hour).Err()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Return the pun to the client
fmt.Fprintf(w, "%s", pun)
}

Wow!

The code ChatGPT wrote, as well as the explanations, gets me 90% of the way there. The only thing missing is inferring that for the fetch, I would like to get a random pun id, first check the Redis Cache and only if it does not exist fetch it from the Postgres database. But that is something that I did not explicitly specify.

Sure, this is a toy example, but I can see the utility of this for rapid prototyping as well as learning. I would gladly pay a subscription fee to ChatGPT to have access to it for rapid prototyping as well as writing code snippets.

--

--

William Kimeria
William Kimeria

Written by William Kimeria

Software Engineer, cyclist, bad pun fan, amateur photographer/artist/astronomer

No responses yet