How ChatGPT Can Enhance Your Coding

Tired of sifting through cluttered websites to figure out that git command? In this post I try out ChatGPT, a preview release by OpenAI. Not only does it provide help with git commands, I also found it useful for various tasks such as remembering how to use Python’s functools.reduce function, enhancing documentation, creating clear docstrings, optimising functions, and even finding edge case tests. Join me in discovering how AI can boost productivity!

I started by putting ChatGPT to the test by requesting code for a linter I was building. The linter enforces the use of the spec or spec_set argument when creating unittest.mock.Mock objects. ChatGPT got remarkably close after a few hints, although it occasionally it was not successful:

ChatGPT unable to generate the linter

In this try, ChatGPT was able to provide mostly correct code for my linter, however, it included unnecessary elements such as a formatter, which is not needed for just a linter:

Starting point for a flake8 linter generated by ChatGPT

It’s worth noting that using AI assistants like ChatGPT to generate large portions of code may have legal implications. I am not a lawyer, it could be advisable to wait for further court cases to unfold before fully adopting AI assistants to write code for you. When asked, ChatGPT mentions that its responses are not copyrighted:

ChatGPT response to a question about code licensing

The next thing I tried was seeking help with Git tags from ChatGPT. I had mistakenly created a tag and wanted to delete it. Instead of searching through articles on the internet, ChatGPT provided a helpful solution:

ChatGPT response to a question about how to delete a git tag

I also used it to consult how to use the Python function functools.reduce:

Chat GPT response to requesting hot to use the Python functools.reduce function

I’m probably not going to use Google to figure out how to use Python functions, git commands, bash functions etc and just use ChatGPT going forward. ChatGPT provides quick answers with examples and eliminates the need to search through multiple websites. However, it’s important to have a basic understanding of what you’re looking for to evaluate the quality of the answers provided. This is similar to when you read answers on stack overflow or on other websites.

After using ChatGPT to look up documentation, I tried asking for help optimising functions I had written. Sometimes it just tells you that your code is already pretty good:

ChatGPT tried to improve a function

Introducing an unnecessary if statement, ChatGPT advises that the if statement isn’t needed and provides back the original function:

ChatGPT response after introducing an unnecessary if statement

After trying this a few times, I found that ChatGPT can provide useful suggestions when asked for help in code optimization. However, it’s important to evaluate the recommendations as some may not be optimal for your specific case. For example, ChatGPT once suggested using type(<instance>) is <type> instead of isinstance(<instance>, <type>). Additionally, there were instances where the generated code was not correct.

Another useful question is to ask for test cases to be generated for a function. This helped me get a test for an edge case I had been working on for the _get_fully_qualified_name function:

Tests generated by ChatGPT for the _get_fully_qualified_name function

The tests are fairly comprehensive and probably sufficient for this function. I would rather use pytest and parametrize the tests and also add a bit more documentation for the tests. ChatGPT did generate a test case that made me think to pass in None.function_1 which tests the case of returning an empty tuple which I hadn’t thought of myself.

Another useful feature is to ask it to optimise your docstrings. For example:

The prompt sent to ChatGPT requesting the docstring of the function to be optimised
ChatGPTs optimised docstring

The generated docstring is quite good. Probably a bit lengthy given the function. It also re-states the types which are already on the type hints of the function signature. It accurately describes how the function works as well, which can be helpful in the documentation. However, some people may prefer to let readers refer to the code to understand how it works. You can ask ChatGPT to make adjustments, although it missed the request regarding type hints in this case:

ChatGPT modifying the docstring after requesting for it to be shorter and not to include types

I can see myself frequently using this functionality, especially for more complex functions. I plan to use it as a starting point and not simply copying the response. It can also generate a docstring, which though not compliant with PEP 257, can be a helpful starting point:

Asking ChatGPT to write the docstring for the _get_fully_qualified_name function

After finding success with docstrings, I also tried it out for other documentation. For example, I provided ChatGPT with a paragraph of documentation I had written and asked ChatGPT to improve it. Note: the following example is for another linter as I have already used ChatGPT to enhance the documentation of the linter we have been discussing:

You can see that ChatGPT’s version flows better and I would probably just copy what it has written into the documentation. I might remove the last sentence as it is a bit generic and it may be more beneficial for readers to consider the benefits rather than having them explicitly stated. I will probably ask ChatGPT to help improve any documentation I write in the future, although I would draft it first to ensure it covers the necessary content.

Finally, after having written the first draft of this post, I asked ChatGPT to help improve the content. I doubt blog posts fully written by AI are particularly useful, they tend to produce general information that sounds right although lacks insight. However, being an editor seems to work quite well. Here is an example of asking ChatGPT to improve the first paragraph of this post:

ChatGPT optimising the opening paragraph of this blog post

I did edit what ChatGPT produced although it is much better than what I had originally written. You can take a look at the opening paragraph as published and compare it to the ChatGPT response for a comparison. By the time this blog post is published, I will also have asked ChatGPT to optimise the remaining paragraphs of this post and will probably do so going forward.

In summary, ChatGPT is going to enter my development workflow. Whilst it cannot produce code for entire projects, and even if it could it may not be usable due to licensing issues, it can help you come up with ideas for how to start, optimise functions you write, provide you with documentation and help improve your own documentation.

One thought on “How ChatGPT Can Enhance Your Coding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s