My Journey with Code and ChatGPT

My Journey with Code and ChatGPT

Hello, my name is Andrew Probert. I am the Chief Data Scientist at a data science consultancy company called Harmonic Analytics in New Zealand. Recently, I had the chance to use ChatGPT with GPT-4 (thanks to OpenAI) to generate a sequence of code examples. These examples are to be used as part of training for clients in good code review. Here I will take you through my voyage of discovery. I will give you my realizations and discoveries and possible pearls of wisdom :-).


First we needed an initial segment of code as a "seed". I took two approaches to this. First, I used an actual seed. I taught a block of code (which I cannot show you sorry) to ChatGPT. This code was in Python and used objects and classes. ChatGPT gave me a good summary of what the code was designed to do. I then asked ChatGPT to rewrite the code using no objects but instead functions. I was impressed with the result.


My second approach was to ask ChatGPT to write a program in python to add three pairs of numbers without using functions. ChatGPT performed perfectly. 



# Pair 1

a1 = 5

b1 = 10


# Pair 2

a2 = 7

b2 = 3


# Pair 3

a3 = 2

b3 = 8


# Perform addition

sum1 = a1 + b1

sum2 = a2 + b2

sum3 = a3 + b3


# Calculate total sum

total_sum = sum1 + sum2 + sum3


# Print the results

print("Sum 1:", sum1)

print("Sum 2:", sum2)

print("Sum 3:", sum3)

print("Total Sum:", total_sum)



I then asked it to rewrite this code using functions. Again great code. 


def add_numbers(a, b):

  return a + b


# Pair 1

a1 = 5

b1 = 10


# Pair 2

a2 = 7

b2 = 3


# Pair 3

a3 = 2

b3 = 8


# Perform addition

sum1 = add_numbers(a1, b1)

sum2 = add_numbers(a2, b2)

sum3 = add_numbers(a3, b3)


# Calculate total sum

total_sum = add_numbers(add_numbers(sum1, sum2), sum3)


# Print the results

print("Sum 1:", sum1)

print("Sum 2:", sum2)

print("Sum 3:", sum3)

print("Total Sum:", total_sum)



Finally, I asked ChatGPT to rewrite using meaningless variable and function names. ChatGPT understood I wanted to obfuscate and said so. The result was gibberish but technically functionable as requested.


def f(a, b):

  return a + b


# Pair 1

x1 = 5

y1 = 10


# Pair 2

x2 = 7

y2 = 3


# Pair 3

x3 = 2

y3 = 8


# Perform addition

r1 = f(x1, y1)

r2 = f(x2, y2)

r3 = f(x3, y3)


# Calculate total sum

t_sum = f(f(r1, r2), r3)


# Print the results

print("Result 1:", r1)

print("Result 2:", r2)

print("Result 3:", r3)

print("Total Result:", t_sum)



I then asked ChatGPT to rewrite this code using defensive coding (meaningful variable and function names, error handling etc). Again very impressive.


def add_numbers(a, b):

  """

  Add two numbers.


  Args:

    a (int or float): The first number.

    b (int or float): The second number.


  Returns:

    int or float: The sum of the two numbers.

  """

  if not isinstance(a, (int, float)):

    raise TypeError("Invalid input: 'a' must be an integer or float.")

  if not isinstance(b, (int, float)):

    raise TypeError("Invalid input: 'b' must be an integer or float.")

  return a + b


# Pair 1

a1 = 5

b1 = 10


# Pair 2

a2 = 7

b2 = 3


# Pair 3

a3 = 2

b3 = 8


try:

  # Perform addition

  sum1 = add_numbers(a1, b1)

  sum2 = add_numbers(a2, b2)

  sum3 = add_numbers(a3, b3)


  # Calculate total sum

  total_sum = add_numbers(add_numbers(sum1, sum2), sum3)


  # Print the results

  print("Sum 1:", sum1)

  print("Sum 2:", sum2)

  print("Sum 3:", sum3)

  print("Total Sum:", total_sum)


except TypeError as e:

print(f"Error: {str(e)}")



In summary I am impressed. I would make the following comments. I got better results when I wrote clearer and more detailed prompts. To show that one can create non-trivial code using ChatGPT try the prompt

write a program that takes as input a data set of movies and a data set of actors and generates a graph or network of how they relate using networkx. Then draw the graph.

Try generating this - you will be happily surprised. And bear in mind this is not using the Code Interpreter plugin which is apparently even more impressive.

Secondly, I am not sure I would trust the results of ChatGPT (the completion) without review first. There is a lot of discussion now about a role that might be called Prompt Engineering or Prompt Development (when creating code). I would add to this a needed role I call Completion Reviewer. This would use many of the same techniques as a document or code reviewer. An interesting and ironic realization given the purpose of this exercise for me was to generate code samples for teaching code review.

I conclude that this truly is revolutionary technology. Particularly for today's software engineers and data scientists. We need to learn the new tools and that will require new ways of thinking. Writing a good prompt is not at all the same as writing a block of code. It is more like writing a document. The future is here. Are we ready to take advantage of it or will we be left behind?

To view or add a comment, sign in

More articles by Andrew P.

Others also viewed

Explore content categories