Author | Jef Meijvis | Publish date | 11/01/2023 |
Title | ChatGPT for software developers. | Id | 8 |
Source | 008-chatgpt-as-a-developer.md | Render timestamp | Dec 06, 2023, 06:08:31 AM (GMT+1) |
Views | 55 | Tags | openai, chatgpt, dotnet |
Author | Jef Meijvis |
Publish date | 11/01/2023 |
Title | ChatGPT for software developers. |
Id | 8 |
Source | 008-chatgpt-as-a-developer.md |
Render timestamp | Dec 06, 2023, 06:08:31 AM (GMT+1) |
Views | 55 |
Tags | openai, chatgpt, dotnet |
Share this post:
ChatGPT is a powerful chatbot developed by OpenAI that can generate human-like text. It is based upon the GPT-3 family of large language models. As a software developer, you can use ChatGPT to automate repetitive tasks, generate code, and even help with documentation. In this blog post, we'll explore some of the ways ChatGPT can help you as a developer, and how you can get started using it. The practical examples shown here will make use of C# as its programming language.
Image: OpenAI logo
The browser chat interface of ChatGPT can be accessed via chat.openai.com. Authorization can be done via a Google or Microsoft account. After logging in you can start interacting with the chatbot by asking it a question in the chatbox.
For example, the query Write a method in C# that calculates the median price out of a list of products. returns the following result:
Image: Interacting with the chatbot via its web interface
Lets imagine we have a simple User class as shown below. Each user has a name, and a list of friends, which are also Users.
Code snippet
1 | |
2 | |
3 | |
4 | |
5 | |
If we want to create some kind of utility logging function to log information about a specific user, we can ask ChatGPT to generate this for us after first pasting the class code in the chatbox. Query:
Could you create a method that prints out the summary of this user class for an instance?
ChatGPT came up with the following method:
Code snippet
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
Let's continue with our User example, and write a method that double links the friends.
Code snippet
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
Perhaps we would like to see this method written in Linq. Query: Could you convert this code to make use of Linq?
And ChatGPT will convert the method just as we asked:
Code snippet
1 | |
2 | |
3 | |
4 | |
I won't go into the discussion about whether or not Linq makes is more or les readable, but as you can see it get's properly converted.
Let's convert the method we initially created to Typescript. Query: could you convert this piece of code to typescript?
Code snippet
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
Lets say we encountered the above code in a wild codebase, and we could not figure out what it exactly does. Let's ask ChatGPT:
Image: Explaining a piece of code
Lets imagine the follwing setup. We have a User class, a mockable IKeyValueStore interface and a Save and Load method. We want to test these two methods.
Code snippet
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
Let's ask ChatGPT to generate some unit tests for us.
Code snippet
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
While these aren't perfect, they give us a good indication and baseline to start improving these testcases.
Let's try to invent a new language called JefSpeak. First we must declare some rules:
JefSpeak is a new programming language. It resembles C#, but there are a few changes:
Then we can ask it to convert a C# codeblock to this newly invented language. Could you convert the following piece of code to JefSpeak?
Code snippet
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
And we get the following result:
Code snippet
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
What's even beter, is that our chat session now has knowledge of the concept of JefSpeak. This means that from now on we can start asking questions about this new 'language'. Let's have a look and see how we could calculate the median out of a list of numbers in JefSpeak:
Code snippet
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
Well that's looks like an awful and unwieldy language. But it is a fun experiment to showcase ChatGPT's transformative capabilities nonetheless.
In conclusion, ChatGPT is an incredibly powerful tool that can help software developers automate repetitive tasks, generate code, and even help with documentation. It can save you a significant amount of time and effort, allowing you to focus on more complex tasks and improve the overall efficiency of your work. With easy integration and access to the model, it is worth giving it a try in your next project. However keep in mind that AI assisted tools can be wrong, while their response might seem really solid and confident.
An interesting alternative to check out might be Github Copilot, which can intergrate right in your IDE. It is also developed in part by OpenAI.