Tips for generating code with ChatGPT

artificial_intelligence_ai_in_education_

ChatGPT is a real superpower, not only when it comes to writing, but also when it comes to generating code. Getting started with ChatGPT is easy, but if you want to get the most out of it, then you need to learn how to make good prompts.

Prompts are instructions or discussion topics that you give the ChatGPT AI model to respond to. It can be a question, statement or any other query you want to spark creativity, reflection or engagement.

You really need to know a few hidden tips and formulas to use ChatGPT effectively. Implement these formulas in your tips and get the best results from ChatGPT.

Concrete presentation

When using ChatGPT, you want to avoid oversimplified prompts. Also, the more specific your request, the better.

The way you can look at this is not to think of ChatGPT as a vending machine where you put tokens in and it spits out answers at you. Instead, you want to think of ChatGPT as your personal assistant (whether for generating content or code).

To keep your queries as detailed and specific as possible (and to get the best possible answers), here's the basic structure you'll generally want to use when creating your prompts:

  • Specify what you want to do (could be editing, writing, or criticizing)
  • Follow up by telling ChatGPT what format you want it to output (JSON, JavaScript, Python, etc.).
  • Conditions and parameters to apply
  • role or style (optional)

So what you're doing here is writing functions that return the first and last three characters from a string. The format we want it to return is JavaScript. The condition is that we want it to be no more than three lines. The style here is to use best coding practices.

The actual screenshot is as follows.

Screenshot of function generated by ChatGPT

Screenshot of function generated by ChatGPT

Screenshot of function generated by ChatGPT

Screenshot of function generated by ChatGPT

Because what we're asking for in our prompt is specific, this is more nuanced and specific than what we want than what we're asking for is more general.

Another benefit of using ChatGPT to generate code is that it also interprets the code. This is useful if you are new to programming or don't understand the generated code.

If you are not satisfied with an answer, click the "Regenerate" button to generate another answer. You can also modify the prompt and make it more specific if it helps.

assign tone, style or audience

Often, you might generate code for a specific audience, such as beginners (this is common if you're writing code-related tutorials). Maybe you want to generate code that conforms to a specific coding style or convention, such as EMCAscript or TypeScript.

In this case, you need to inform ChatGPT of the style and tone you want to adopt, or the audience you want to write for. To do this, you typically add the following statement to your prompt: "Write in X tone," "Write in X language," or "Write for X audience."

ChatGPT does not use regular functions, but uses ES6 arrow function syntax ( () =>) to define functions 无限循环, according to our requirements. You can do the same for other programming languages, libraries and frameworks.

Introduce experts

If you're still new to programming, ChatGPT can play the role of an expert for you. You can ask it to look at your code and provide feedback on how it can be improved. The prompt you will use is: 你是一个(角色)。执行(操作).

Below the query, you'll paste the code you want ChatGPT to see and hit enter. In return, ChatGPT will review your code and provide feedback on how to improve it.

I'm asking ChatGPT to check my function to check if a string is a palindrome. In return, ChatGPT gave me an improved version of the code:

It also gave me some suggestions to improve its readability and conciseness:

Screenshot of suggestions by ChatGPT

Screenshot of suggestions by ChatGPT

Screenshot of suggestions by ChatGPT

Screenshot of ChatGPT's suggestion

Using this feature, you can review any code to ensure it meets the best coding standards. Keep in mind that ChatGPT may generate wrong codes. Therefore, you need to test run your code to confirm that it provides the expected output before pushing it to production.

Get out of trouble with Chat GPT

As a software developer, you are bound to encounter problems during the development process. Maybe your code doesn't work and you don't know why. Or, you're not sure how to create a function that performs a specific task.

The best places to find such solutions used to be on forums like StackOverflow and Reddit. But now you can use ChatGPT to solve any coding related problems.

The best thing about using ChatGPT is that it's instant, you don't need to browse through articles or solutions to find what you need. Each solution also comes with an in-depth explanation, something you might not find with answers from StackOverflow or Reddit.

Constantly iterate

You might not get what you want right away, but that's to be expected. Be ready to ask ChatGPT to rewrite content, regenerate code blocks, or make tweaks to your code. Alternatively, you can simply redo the entire prompt and get something entirely new out of it.

The key here is to have a collaborative conversation back and forth with ChatGPT, not just type in a prompt and expect one thing and try again.

What's really cool about ChatGPT is that you can have an ongoing conversation with it and it will remember your previous interactions. It will be able to remember the context of other answers it has given you in a particular chat.

in conclusion

The key to effective use of ChatGPT is well-designed prompts. Your prompt needs to be specific, concise, and as detailed as possible. It should contain an action, the output format, one or more conditions, and the role or style to use (this is optional).

ChatGPT not only generates code, but also checks your own code for errors and provides feedback on how to improve it. When used properly, ChatGPT can help simplify the process of developing applications. However, if you're looking for AI tools tailored specifically for programming, check out GitHub Copilot.

Guess you like

Origin blog.csdn.net/shupan/article/details/131777767