Feedback and examples of using ChatGPT for developers

Thomas Latterner
7 min readAug 6, 2023

--

Photo by Drew Dizzy Graham on Unsplash

Developed by OpenAI, ChatGPT (Generative Pre-trained Transformer) is a conversational agent that uses artificial intelligence to generate contextual text. It has been trained on a wide variety of topics and data. The tasks it can perform are diverse and varied. For example, it can complete text, shorten it, translate it, rephrase it, change the tone to match specific requests, and even correct spelling and grammar errors.

I am going to share how, over the past several months, this tool has changed my daily life in my professional work. I also use it a lot in my personal life, but that is not the subject of this article.

ChatGPT: An essential ally for developers

In my professional life, as a CTO, I regularly use it to assist me in various tasks, especially in software development. It helps me find the name of a package or function, discover new things, stay updated, debug, generate code, etc.

Before, my first reaction would have been to search on a search engine, but now, I often first turn to ChatGPT. The answers come faster and are accompanied by customized examples that make the information even more useful.

The efficiency of ChatGPT: A precious time-saver

Whereas “yesterday”, a research and development task could take me several hours, ChatGPT allows me to achieve the same result in record time. You just need to know what to ask it and how to phrase it.

Let’s take a concrete example. One evening, I was working on a project in Go (or Golang, an open-source programming language developed by Google). I needed to make several API calls simultaneously to speed up the process, while avoiding overloading requests and having some other constraints. For a developer less familiar with Go like me, this task would normally have required several hours of research, practice, and testing.

Here is how I formulated my request:

You are a backend developer with 20 years experience. You work with Golang 1.18. Let's create a proxy that respects the following constraints:
- Send API calls and retrieve results
- Retry on failure (maximum 2 times)
- Manage a rate limit called "Request Per Minute", which must not exceed 200
- As long as the limit is not reached, we launch as many API calls as possible.
- API calls are given in a certain order, and can be sent in any order in parallel, but the results must be ordered in the same order as the initial requests.
- The rate-limiting system must be kept in memory for subsequent calls.

In a few minutes, I got precise and efficient code that perfectly met my needs.

Explanation and learning

ChatGPT is not just a code generation tool. It’s also a great teacher. It provided me not only with the code I needed, but also with detailed explanations about how it works. I simply had to ask:

Explain the code generated previously for a senior developer but beginner in Golang

Thus, I was able to ensure that I had correctly understood the previously generated code and reuse it with confidence.

It is important to note that each conversation with ChatGPT has a dedicated context, which is reused within the same conversation. That’s why I can refer to previous responses. However, this context has a limit: the longer your conversation, the blurrier references to the oldest responses will be.

Other uses of ChatGPT

Function implementation

Just provide the function’s signature (name, parameters, and return value), and describe its behavior, and ChatGPT will provide you with a first implementation. You can test it yourself. Here’s an example:

You are a go developer with 20 years of experiences. Your role is to write clean and testable code. You work with Golang version 1.18. Write a function that transforms a camelCase string into a snake_case string, and that handles errors. Below is the function signature:
```
func camelCaseToSnakeCase(camelCase string) (string, error)
```

Is the generated code too long? Ask it to shorten it or to make several small functions. The method used does not suit you? Ask it to do differently, to use a certain library or be more specific in the description of the function or the constraints it must respect.

Unit test generation

No more excuses for not writing your unit tests! Here again, you just need to provide ChatGPT with a function signature, a context, and a description. Here is an example:

You are a PHP developer with 20 year of experience. You are currently working with Symfony 5.2, already deployed in production. Your role is to write good software quality. Write unit testing for the following method:
```
public function getFinalPrice(int $articlePrice, int $quantity, float $discount): int
```

Here, I specify already deployed in production so that it doesn't instruct me how to configure PHPUnit and so that it gets straight to the point.

You can also provide it with the method body if needed (if for example the method throws Exceptions or if it contains some branching).

Explanation and learning

In the same way as in my first example, if a piece of code seems obscure to you, instead of spending time trying to decipher it, you can copy/paste the code into ChatGPT. Whether you are looking for a design pattern, having trouble understanding a concept, or trying to achieve something specific, ChatGPT can help. You can even use it as a pedagogical tool for learning new skills.

You are a lead developer with 20 years of experience working with Node.js and JavaScript. Explain what a closure is, when it should and should not be used, and provide two examples.

Code Translation

I consider development as a foreign language. Imagine that you find a function on the internet written in a specific language or that you have developed a function in a certain language, but you need to transcribe it into another language. Provide your function’s code in Language A, then ask ChatGPT to translate it into Language B.

Another example would be if you have an OpenAPI (formerly Swagger) definition in YAML, but you now need this definition in JSON. Again, you can quickly rewrite your definition into a new format in no time.

Use of OpenAPI and Mock Generation

Speaking of OpenAPI, I’ve used it to generate API definitions very quickly. Explain what your endpoint does, add constraints, and let the magic happen.

Once your definition is generated, or by indicating an existing definition, you can ask ChatGPT to generate examples that could be used to improve your documentation, for your tests, or to mock an API response.

Via an existing JSON, you can also ask it to generate a JSON schema.

Refactoring and Code Improvement

ChatGPT can also be used for other tasks, such as refactoring. It can suggest changes to improve your code quality, explaining why these changes are necessary.

You can ask for help to make your code more readable, more testable, or more efficient. You can also ask for help adding comments, or for renaming variables and functions to improve the readability of your code.

Finally, if you need to break down a function into smaller ones or merge multiple functions into one, ChatGPT can help you do this.

You can also use the Github Copilot extension in your preferred and compatible IDE (Visual Studio Code, JetBrains IDE, etc.) that uses a specialized version of ChatGPT to function.

Conclusion

As you’ve probably noticed, the prompts I’ve shared have a well-defined structure. I first put GPTChat in a role by specifying the expected behavior. Then, I clearly express my expectations. Finally, if necessary, I provide additional information (like a function definition or a code snippet).

There are many prompts shared by users on the Internet. I highly encourage you to check them out to explore all the possibilities they offer: https://github.com/f/awesome-chatgpt-prompts

ChatGPT is a fantastic tool for me. On a daily basis, ChatGPT saves me a lot of time. For those who might fear that it will replace certain professions, including developers, don’t worry for now. From my perspective, it is unlikely to happen in the short term, as it requires:

  • Knowing how to use it and how to structure its prompts (we also talk about prompt engineering)
  • Greater democratization (I still meet people who do not know ChatGPT or who do not use it)
  • Expertise to formulate requests (there is a noticeable difference between the requests of a junior developer and those of a senior developer, not to mention tech sensitivity).
  • Expertise to use ChatGPT’s answers, a business knowledge of applications, and a knowledge of systems.

However, despite the fact that ChatGPT has transformed my way of working in many areas, including development, everything is not perfect. Without going into detail, it is important to mention certain limitations of this tool.

Since ChatGPT is not connected to the Internet, and its training data dates back to at most September 2021, it is not aware of the latest versions of software and libraries. It can produce obsolete code, not respecting current best practices, or no longer matching new standards. It can also suggest you use an outdated library, not updated, or containing security vulnerabilities. To remedy this, you can always perform a search on a search engine to verify that the produced code is up-to-date, provide it with the signatures of new functions or packages, or use recently available plugins.

I advise you to be careful about what information you share in your prompts. By default, the conversation history is stored on OpenAI’s servers. This could potentially be analyzed and then used to improve ChatGPT. Some large tech companies, like Apple or Amazon, limit their usage. Recently, Samsung experienced issues with sensitive data leaks.

ChatGPT is a fantastic tool that has changed my way of working. I hope this sharing of experience will be useful to you and will make you want to use it.

I am eager to see how this technology will evolve in the future and how it will change our way of developing. For my part, I will continue to use it and will continue to learn to use it and understand its limits. I am convinced that there is still much to discover with ChatGPT and LLMs in general.

I hope you enjoyed your reading! If you want to encourage me continuing writing articles like this, or if you find it useful, feel free to give me some “claps” or to leave a comment.

Do you have an idea, an example, a question, or do you want to share your experience? Don’t hesitate to comment, and above all, share!

--

--

Thomas Latterner

Tech lover, LLM Enthusiastic, Entrepreneur, Co-Founder & Chief Technology Officer at Jus Mundi https://jusmundi.com/