top | item 33866113

(no title)

bluejellybean | 3 years ago

Yupp, it can! I've been able to get it to do simple examples. I was able to get it to print out a 'sum of digits' style function. I was then purposefully placed a silly bug in it by hand. I then ran the code through a 'please code review' style prompt, got a number of decent suggestions out. Using those suggestions, I was able to prompt it to refactor the function to: fix the bug, add documentation comments, include type definitions, and even add tests!.

A simple example, and not 'perfect', but it's quite impressive. Even a simple case of throwing your code 'over the wall' to the AI for review before going to an actual person is a huge time-saver.

The token size limit is the killer here, it means larger files, let alone projects, can't be fully processed in a single prompt.

Initial code produced: ``` def sum_of_digits(n):

    if n == 0:

        return 0

    else:

        return n % 10 + sum_of_digits(n // 10)
```

I didn't save the exact code review, but essentially it was just a list of suggested code changes. The prompt was:

``` prompt = f'Python3. Act like you are a senior engineer writing a code review comment on the code. Write at least two specific changes to make, being sure to check for any errors first: {prompt}. Be as concise and correct as possible, and also be sure to write in a way that is easy to understand.

```

The resulting output code was:

``` import unittest

def sum_of_digits(n):

    if n < 0:  # Added base case check for negative numbers

        return None

    elif not isinstance(n, int):  # Check type of argument passed in

        raise TypeError("Input must be an integer")

    elif n == 0:

        return 0

    else:

        return n % 10 + sum_of_digits(n // 10)


# Unit tests using built-in test suite

class TestSumOfDigits(unittest.TestCase):

    def test_sum_of_digits_positive(self):

        self.assertEqual(sum_of_digits(123), 6)



    def test_sum_of_digits_negative(self):

        self.assertEqual(sum_of_digits(-123), None)


    def test_sum_of_digits_type(self):

        with self.assertRaises(TypeError):

            sum_of_digits('abc')
if __name__ == '__main__':

unittest.main() ```

Edit: trying to cleanup hn formatting for readability

discuss

order

Jtsummers|3 years ago

> Edit: trying to cleanup hn formatting for readability

HN code formatting is simple, prefix each line with 2 spaces and then you don't need any extra blank lines like with normal paragraphs.

  class TestSumOfDigits(unittest.TestCase):
      def test_sum_of_digits_positive(self):
          self.assertEqual(sum_of_digits(123), 6)

      def test_sum_of_digits_negative(self):
          self.assertEqual(sum_of_digits(-123), None)

      def test_sum_of_digits_type(self):
          with self.assertRaises(TypeError):
              sum_of_digits('abc')

  if __name__ == '__main__':
      unittest.main()
``` does nothing, it's just noise here.