How to Write Code with ChatGPT

As we all know, ChatGPT is a large language model (LLM) that is trained on a wide variety of massive data. It includes data from general knowledge, common sense, reasoning, mathematical problems, coding, and more. Just like you can use ChatGPT to write essays, you can also use it to write code and several other tasks. In this tutorial, we are specifically going to learn about coding and how we can write code with ChatGPT. We have discussed ChatGPT coding prompts with an app demo and explained other solutions like GitHub Copilot X and CodeGPT. On that note, let’s jump into the article.

ChatGPT Prompts to Write Code with Examples

1. To begin, you can start with something simple. You can simply ask ChatGPT to write code in your favorite language. It will output the code immediately. You can also use “implement” in place of “write code”.

write code for Dijkstra's algorithm in Python
How to Write Code With ChatGPT

2. You can also define the role of ChatGPT in the beginning to get better code output. Here is an example, which will trigger chain-of-thought reasoning to deliver a better response from ChatGPT.

You are an expert Java developer. Your task is to write quality Java code for Android apps.
java

3. For a demo, we asked ChatGPT to create an Android app that reminds users to drink water after regular intervals. And it started explaining everything from scratch, including which IDE to use, which SDK level to target, use either Java or Kotlin, and more.

I am a beginner in Android development. How to go about creating an Android app?
How to Write Code With ChatGPT

4. When I was unsure which option to pick, I simply asked ChatGPT, which summarized everything for me in a simple manner.

create android app with chatgpt

5. Finally, once we put all the tools in place, we asked ChatGPT to write the code in Kotlin. It generated the code for an Android app targeting Android 7.0 and above API level.

ChatGPT Prompts to Write Code With an Example

6. Of course, we faced many errors on the first run, but we used ChatGPT to troubleshoot the errors as well. You can simply paste the error message into ChatGPT, and the AI bot will analyze the issue and fix the error. You can also paste the whole code block into ChatGPT, asking the AI to look for errors or issues.

Note: Keep in mind that the free version of ChatGPT (3.5) often produces code that results in errors. If you are using ChatGPT 4, you are likely to get better output.

How to Write Code With ChatGPT

7. And after many back-and-forth attempts, we could finally run the app. Although it’s a barebones app, the fact that you can code and create apps using ChatGPT without any prior knowledge is bonkers. Sure, you need to have some degree of coding knowledge and logical understanding, especially when you are developing Android apps, but generally, ChatGPT makes programming very accessible to general users.

How to Write Code With ChatGPT

GitHub Copilot X: Write Code with ChatGPT

You can also use the power of ChatGPT for writing code in GitHub directly. The new GitHub Copilot X tool is powered by OpenAI’s GPT-4 model which is one of the most powerful programming models out there. It can help you write an entire function in real time, explain the code in natural language, find errors, and offer context-aware suggestions as well. In addition, it supports pull requests and can automate a lot of tasks using AI.

Not just that, with GitHub Copilot, you can focus more on important code blocks and spend less time on repetitive code patterns where Copilot can quickly complete the code. In addition, you can write your logic in natural language as a comment and it will instantly implement the solution. You can also integrate Copilot on Visual Studio, Neovim, VS Code, JetBrains IDEs, and more.

Some of the popular languages it supports are C, C++, C#, Go, Java, JavaScript, PHP, Python, Ruby, Scala, TypeScript, and more. If you are a developer, I think GitHub Copilot X is better suited for you than ChatGPT as it’s tailored only for programming. GitHub Copilot X is currently in technical preview, and you can sign up for the private beta right here. It will cost you $10 per month. It’s one of the coolest new ChatGPT alternatives, alongside Amazon CodeWhisperer.

Use CodeGPT in VS Code to Get Help Writing Code

Apart from ChatGPT and GitHub Copilot X, CodeGPT is another project that leverages the power of OpenAI’s GPT language model to help you write code. It’s a free extension available in VS Code that can make programming much more productive and helpful for you. The best part is that you can use the latest GPT-4 model along with the GPT-3.5 model, just like Copilot X. That said, you must have GPT-4 API access in order to use the ChatGPT 4 model.

How to Write Code with ChatGPT

In my testing of the CodeGPT extension, I found it to be quite useful. It can generate code with just a comment, explain functions and code snippets, refactor the code, write documentation, and do much more. What I value the most is solving errors with CodeGPT. You can select a code block, right-click on it and ask CodeGPT to find problems. It will contextually go through the code and respond with a possible solution. All of this is done through the OpenAI API key which you will have to buy once the free credit is exhausted. All in all, if you use VS Code as your IDE, I will highly suggest checking out CodeGPT.

comment Comments 0
Leave a Reply