CyberSecurity // Azure // Frontend // Svelte // Cloud Native // Software // CyberSecurity // Azure // Frontend // Svelte // Cloud Native // Software

Chatgpt as a developer

Cover for Chatgpt as a developer

ChatGPT

ChatGPT

Note

Update 8/12/2023: The AI landscape has been changing rapidly over the past last year, so this article is wildly outdated nowadays!

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.

OpenAI logo

Image: OpenAI logo

Accessing ChatGPT

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:

Interacting with the chatbot via its web interface

Image: Interacting with the chatbot via its web interface

Generating code

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.

// CSHARP //

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:

// CSHARP //

Converting code

Let's continue with our User example, and write a method that double links the friends.

// CSHARP //

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:

// CSHARP //

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.

Changing the language

Let's convert the method we initially created to Typescript. Query: could you convert this piece of code to typescript?

// CSHARP //

Helping with Documentation

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:

Explaining a piece of code

Image: Explaining a piece of code

Unit tests

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.

// CSHARP //

Let's ask ChatGPT to generate some unit tests for us.

// CSHARP //

While these aren't perfect, they give us a good indication and baseline to start improving these testcases.

Creating a new language!

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:

  • a method declaration follows the following syntax: 'method' returntype methodname|arguments|
  • the 'void' return type doesn't exist anymore, as it is replaced with 'nothing'
  • the int type is replaced with number
  • above each method name there is an automatically generated comment block that explains the function of each parameter
  • the '=' assignment operator is replaced with the word 'is'
  • the '+' addition operator is replaced with the word 'and'
  • the Console.Write() method is replaced is with say

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?

// CSHARP //

And we get the following result:

// CSHARP //

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:

// CSHARP //

Well that's looks like an awful and unwieldy language. But it is a fun experiment to showcase ChatGPT's transformative capabilities nonetheless.

Conclusion

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.

Alternatives

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.

Further reading