All articles

Software engineering & AI

The Future of Software Engineering: Exploring the Capabilities of Chat GPT and Beyond — Part 1

Andrii ·

Originally published on Medium on March 1, 2023. Read the original article.

The Language of Machines

As software engineers, we have come a long way since the early days when we used to punch holes in paper cards to write programs. Since then, we have built programs that help us to build programs, such as IDEs, new programming languages, code analyzers, interpreters, and compilers. All of this is done to narrow the gap between our natural languages and coding instructions that computers can understand.

New programming languages have naturally evolved towards those that are easy to understand and are similar to human languages. This makes sense since humans are the medium where languages spread, and languages like Brainfuck would meet more resistance, while languages like Scratch would face the least.

Although Chat GPT is not technically a programming language or an IDE, you can still consider it one. Chat GPT allows you to create code that computers can understand. It is still in a primitive form, but the writing is on the wall: this is how we will be coding in 5–10 years.

So what’s different this time? After all, we had visual, “drag and drop with your mouse,” human-like computer languages before. What is different is that it is not the usual “garbage in — garbage out.” With Chat GPT, it is quite possible to achieve “garbage in — some golden nuggets out.” Primarily because it can detect (I won’t use the word “understand” since it is controversial) the intent of what is being asked of it. Chat GPT uses a neural network that encodes nearly all human knowledge available in text form on the internet to detect intent

The Chatbot Challenge: Tackling a 2D Maze with Chat GPT

Let’s try a simple test to see the capabilities of Chat GPT, and to understand how software engineers can employ them today.

For my first experiment, I opted to tackle a straightforward problem — determining the shortest path between two points in a 2D maze. While I had no particular plan of how to approach it, I did intend to utilize the Python programming language for the task since I read somewhere that Chat GPT has been trained on massive amounts of open-source Python.

On the Internet you can see people trying to come up with some convoluted prompts trying to “hack” Chat GPT in doing something. I, on the other hand, wanted to be as simple and straightforward as possible. The “theory of mind” that I had there was just to talk to Chat GPT as if it was an eager junior developer.

My prompt:

write python code that generates a 2-d matrix — a maze with random walls

Chat GPT response:

To generate a 2-D maze with random walls in Python, you can use the random module to randomly add walls to the maze. Here's an example code that creates a 2-D matrix representing the maze:
import random
# Size of the maze
ROWS = 10
COLS = 10
# Initialize the maze with all cells as empty
maze = [[' ' for _ in range(COLS)] for _ in range(ROWS)]
# Add random walls to the maze
for row in range(ROWS):
for col in range(COLS):
if random.random() < 0.3: # 30% chance of adding a wall
maze[row][col] = '#'
# Print the maze
for row in maze:
print(' '.join(row))
In this example, the ROWS and COLS variables set the size of the maze. The maze variable is initialized as a 2-D list of empty cells. The for loop then iterates over each cell in the maze and has a 30% chance of adding a wall to that cell. Finally, the maze is printed using the print function.
You can adjust the chance of adding walls by changing the value passed to the random.random() function. For example, using 0.5 will result in a maze with more walls than empty cells.

The output code looks correct. It has a nice, concise style that is simple and to the point, with idiomatic Python and an air of confidence that you see in the code of people who write a lot of it daily. Chat GPT has made many assumptions here on how big the maze should be, how to initialize it, and how to visualize it. “Meaningful defaults” are an essential consideration in tool and API design, and it comes with recognizing (again, not using “understanding”) the shared context.

Chat GPT is trained on the same body of knowledge that pretty much all of us have, including the same terms and how they relate to each other when we talk/read/write about computer science, geometry, graph theory, and algorithms.

In practice, this means that you (a human) still need to learn all of those things so that you can effectively communicate what you want from the AIs and interpret the results produced by AIs like Chat GPT. For example, while iterating over the code and asking to implement the shortest path algorithm, the code didn’t include handling the case where no such path exists. Obviously, the code was failing with an error “cannot unpack non-iterable NoneType object” for some of the mazes. You should instruct Chat GPT to add handling for the case where the shortest path doesn’t exist.

What’s impressive about Chat GPT is that you can ask it for suggestions, how the code can be improved, and what corner cases there might be.

It’s possible to ask about time and space complexity, potential security and performance issues etc.

Here are my additional prompts to refine the code:

  • write visualization of this maze using plot
  • change the code so it also generates two random points in the maze with special status: start and stop. Make sure that there is always a path between start and stop
  • update the code that it visualize the path from start to end
  • modify code to show start and end points in the maze
  • update the code so that start and end points are not placed at the same coordinates as obstacles
  • update the code to show letter S for start point in the maze and letter E for end point in the maze. when finding path in the maze, only allow up, down, left and right movements
  • update the code to show letter S for start point in the maze and letter E for end point in the maze — i had to repeat the instruction because the code didn’t appear to be updated

In a couple of instances, I found it would be easier just to modify Python code directly than trying to explain what I want Chat GPT to use. So there will be some back and forth between an engineer and AI.

At the end, I got following listing:

import random
import matplotlib.pyplot as plt
from collections import deque


# Size of the maze
ROWS = 10
COLS = 10

# Initialize the maze with all cells as empty
maze = [[' ' for _ in range(COLS)] for _ in range(ROWS)]

# Add random walls to the maze
for row in range(ROWS):
for col in range(COLS):
if random.random() < 0.3: # 30% chance of adding a wall
maze[row][col] = '#'

# Generate a path between start and stop points
start_row, start_col = random.randint(0, ROWS-1), random.randint(0, COLS-1)
while maze[start_row][start_col] == '#' or (start_row == 0 or start_col == 0 or start_row == ROWS-1 or start_col == COLS-1):
start_row, start_col = random.randint(0, ROWS-1), random.randint(0, COLS-1)

stop_row, stop_col = random.randint(0, ROWS-1), random.randint(0, COLS-1)
while maze[stop_row][stop_col] == '#' or (stop_row == 0 or stop_col == 0 or stop_row == ROWS-1 or stop_col == COLS-1) or (start_row == stop_row and start_col == stop_col):
stop_row, stop_col = random.randint(0, ROWS-1), random.randint(0, COLS-1)

# Find the shortest path between start and stop points
queue = deque([(start_row, start_col)])
distances = [[float('inf') for _ in range(COLS)] for _ in range(ROWS)]
distances[start_row][start_col] = 0
prev = [[None for _ in range(COLS)] for _ in range(ROWS)]
path_found = False

while queue:
row, col = queue.popleft()
if (row, col) == (stop_row, stop_col):
path_found = True
break
for r, c in [(row-1, col), (row, col+1), (row+1, col), (row, col-1)]:
if r >= 0 and r < ROWS and c >= 0 and c < COLS and maze[r][c] != '#' and distances[r][c] == float('inf'):
distances[r][c] = distances[row][col] + 1
prev[r][c] = (row, col)
queue.append((r, c))# Find the shortest path between start and stop points
queue = deque([(start_row, start_col)])
distances = [[float('inf') for _ in range(COLS)] for _ in range(ROWS)]
distances[start_row][start_col] = 0
prev = [[None for _ in range(COLS)] for _ in range(ROWS)]

while queue:
row, col = queue.popleft()
if (row, col) == (stop_row, stop_col):
break
for r, c in [(row-1, col), (row, col+1), (row+1, col), (row, col-1)]:
if r >= 0 and r < ROWS and c >= 0 and c < COLS and maze[r][c] != '#' and distances[r][c] == float('inf'):
distances[r][c] = distances[row][col] + 1
prev[r][c] = (row, col)
queue.append((r, c))


# Mark start and stop points in the maze
maze[start_row][start_col] = 'S'
maze[stop_row][stop_col] = 'E'

# Plot the maze and path
cmap = plt.colormaps['tab20c']
cmap.set_under(color='white')
plt.imshow([[0 if x == ' ' or x == 'S' or x == 'E' else 1 for x in row] for row in maze], cmap=cmap, vmin=0.1)

# Trace back the shortest path from the end point to the start point
if path_found:
row, col = stop_row, stop_col
path = [(row, col)]
while (row, col) != (start_row, start_col):
row, col = prev[row][col]
path.append((row, col))
# Draw the shortest path
path.reverse()
for i in range(len(path)-1):
plt.plot([path[i][1], path[i+1][1]], [path[i][0], path[i+1][0]], color='blue')




# Mark the start and end points in the maze
plt.text(start_col, start_row, 'S', color='red', ha='center', va='center', fontsize=16)
plt.text(stop_col, stop_row, 'E', color='green', ha='center', va='center', fontsize=16)

plt.xticks([])
plt.yticks([])
plt.show()

Again, what was impressive is not to write code for you, but to answer various questions about it: what could be improved, how code could be optimized and so on. At the end, I asked Chat GPT to generate a prompt that would generate this code.

This is a mindboggling ability at self-reflection. The human readable prompt essentially becomes source code.

Generalizing what Chat GPT can can’t do

Since it’s a language processing model, it was specifically trained to do following tasks:

  1. Code Completion: Chat GPT is trained on a large dataset of code snippets to generate predictions for what code should come next based on the current context.
  2. Code Summarization: Chat GPT can be used to generate summaries of code that describe what it does in natural language.
  3. Bug Detection: We can use Chat GPT to detect bugs in code by analyzing the input code and identifying areas where errors might occur.
  4. Natural Language Processing for Code: Chat GPT can be used to generate code based on natural language descriptions of what the code should do.
  5. Software Maintenance: Chat GPT can be used to analyze and understand existing code to identify areas for improvement or refactoring.

That’s an impressive list that covers pretty much everything that a software engineer needs to do with code. However, coding is just a small percentage of what an engineer does. In my experience, only about 5% of an engineer’s time is spent actually writing code. What engineers primarily do is problem-solving, and the majority of those problems exist outside of the text domain. Nonetheless, writing code is a crucial part of what we do, so any help we can get in that area is always welcome.

In the following part, I will let myself dream a bit about the future where AI does steal my job :)