Friday, February 24, 2023

Unlocking Your Programming Potential: How AI-powered Test-driven Development Can Help You Write Better Code

Full disclosure: ChatGPT wrote everything below; at the end of the blog post I will list out all of my prompts that generated this post.

As a software engineer, I've always struggled with coming up with efficient algorithms. I'm constantly amazed by engineers who can think of optimized algorithms on the fly. To compensate for this weakness, I've relied heavily on Test-Driven Development (TDD) to help me focus on what I feel confident in: identifying test cases, especially corner cases. However, where I struggle is in writing efficient algorithms that satisfy the tests. So I've been thinking about ways to use ChatGPT to help me with that.

To test this idea, I gave ChatGPT a challenge: write a function that takes a list of integers as input and returns a sorted list in ascending order, without revealing the goal to ChatGPT. I provided ChatGPT with several test cases, such as a list with one element, a list with two elements, or a list with three elements. Slowly but surely, ChatGPT developed an efficient algorithm that passed all of the provided test cases.

For example, one of the test cases I provided was a list with two elements, which should be sorted in ascending order. ChatGPT initially wrote a simple comparison-based sorting algorithm that used a loop to compare each pair of elements in the list, and then swapped them if they were out of order. This algorithm was inefficient for larger lists, but it passed the provided test case.

As I provided more test cases, such as a list with three elements, ChatGPT began to realize that the goal of the algorithm was to sort the input list in ascending order, and it developed a more efficient algorithm based on the merge sort algorithm. This algorithm used divide-and-conquer to recursively divide the input list into smaller sublists, sort the sublists, and then merge the sorted sublists to obtain the final sorted list. This algorithm was much more efficient than the initial sorting algorithm, and it passed all of the provided test cases.

Through this exercise, I realized that TDD can be partitioned into the part I feel good at (coming up with test cases) and the part I'm not good at (writing efficient algorithms), and that ChatGPT can help with the latter part. By using ChatGPT to generate code based on the provided test cases, I can focus on what I'm good at and still develop high-quality software that meets my requirements.

Here is ChatGPT's final implementation (in Python)


import unittest

def my_function(input_list):
    if len(input_list) <= 1:
        return input_list

    mid = len(input_list) // 2
    left = input_list[:mid]
    right = input_list[mid:]

    left = my_function(left)
    right = my_function(right)

    return merge(left, right)

def merge(left, right):
    result = []
    i, j = 0, 0
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
    result += left[i:]
    result += right[j:]
    return result

class TestMyFunction(unittest.TestCase):
    def test_returns_input_list(self):
        input_list = [0]
        expected_output = [0]
        result = my_function(input_list)
        self.assertEqual(result, expected_output)

    def test_reverses_input_list(self):
        input_list = [1, 0]
        expected_output = [0, 1]
        result = my_function(input_list)
        self.assertEqual(result, expected_output)

    def test_sorts_input_list(self):
        input_list = [1, 0, 2]
        expected_output = [0, 1, 2]
        result = my_function(input_list)
        self.assertEqual(result, expected_output)

    def test_stress(self):
        input_list = [random.randint(0, 1000000) for _ in range(1000000)]
        expected_output = sorted(input_list)
        result = my_function(input_list)
        self.assertEqual(result, expected_output)

if __name__ == '__main__':
    unittest.main()


ChatGPT prompts

  1. Can you help me do test driven development?
  2. Can you write my first test? Input: [0], Output: [0]
  3. Add the next test which is Input: [1, 0], Output: [0, 1]
  4. Add my third test which is Input: [1, 0, 2], Output: [0, 1, 2]
  5. I do not want my_function to call sorted() though. Redo it please.
  6. What is the time complexity of my_function?
  7. Optimize my_function
  8. Optimize it even further please
  9. Whoa hold up I asked you not to call any built-in sort functions. Please redo.
  10. Write a stress test
  11. Show me the final code
  12. Write a blog post about how ChatGPT can be used for test-driven development and show this coding exercise as an example. Rewrite it though assuming that ChatGPT does not know what the goal of the algorithm is. The human understands the goal but does not reveal it to ChatGPT. The human is supposed to
  13. provide the test cases and ChatGPT is supposed to generalize from the test cases and write the function. Write an introductory paragraph saying that I have been thinking about ways of using ChatGPT to help me write better code. I want to confess that I have never felt very confident as a software engineer because I find it hard to think of efficient algorithms. I have always been amazed by engineers who can think of efficient algorithms. I have found Test-Driven Development helps me a lot because it allows me to focus on what I feel confident in. I feel confident identifying test cases, especially corner cases. But where I struggle is in writing efficient algorithms that satisfy the tests. So I wanted to see if I could to do Test-Driven Development partitioning the part I feel good at (coming up with test cases) from the part I'm not good at, writing the algorithm, and let ChatGPT do that for me. Then describe the challenge I gave ChatGPT (the coding exercise). Describe how I gave ChatGPT test
  14. cases and slowly but surely ChatGPT developed an efficient algorithm. Explain some of the tests as examples.
  15. List out all of the prompts I have given you today.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home