Recently, our company has been promoting the use of ChatGPT among programmers. Some people who have tried it feel that this thing will affect their coding thinking and reduce efficiency; some people who have tried it find it amazing and can't live without it. Their computer screens are filled with text generated by ChatGPT all day long. I also want to record my use cases of ChatGPT in programming.
Code#
Code Understanding#
When writing requirements in daily work, it is often not starting from scratch. Many times, it is based on the code written by others. Each person's coding style and complexity are different. At this time, we can use ChatGPT to help us organize the basic logic of the code.
Example
ChatGPT's response:
Writing Code#
Sometimes it is very difficult to write a piece of code in a language that you have not learned. At this time, you can ask ChatGPT to help you write it. Or if you have a rough idea of the implementation but don't know where to start, you can ask ChatGPT to write a demo for you.
- If the code provided is not complete, there is a word limit for ChatGPT's response.
- You can provide:
Please provide the rest of the code
- You can provide:
- If the provided code has problems and throws errors when running
- You can provide:
Describe the problem, please complete the code
- If the code is too long and generating the code each time will waste time or token, you can describe it like this:
Describe the problem, please point out which line of code needs to be modified
- You can provide:
Sometimes the code provided by ChatGPT is always problematic. At this time, you need to understand the code yourself (you can also ask ChatGPT to explain the meaning of the code to you), find the problems in the code, and clearly point out the problems to ChatGPT, which can improve the speed of problem solving.
Improving Code Quality#
Let ChatGPT help you fix bugs
When you finish writing a requirement and have several core logic sections, you can ask ChatGPT to check if there are any bugs in the code. Or if there is a problem in the production environment and you know exactly which section of the code has a problem, but you are not sure where the problem is, you can ask ChatGPT to check and provide you with a solution.
- Sometimes the error message shows which line the error occurred, you can also tell ChatGPT which line of code has a problem, so that it can locate and solve the problem more accurately.
- You can also add more detailed descriptions in the code.
Let ChatGPT help you optimize code
ChatGPT can also help you optimize code
It can also help you optimize SQL
Testing Scenarios#
Generating Test Data#
When we finish developing a feature, we often need to test it ourselves. At this time, we can ask ChatGPT to generate test data for us. Sometimes this data can also be used to generate Python code for the corresponding data.
Generating Unit Test Cases#
After writing a piece of code, we need to write corresponding unit test cases to ensure the stability and accuracy of the code. However, writing unit test cases is often time-consuming, and sometimes even more complex than the original logic. At this time, we can ask ChatGPT to help us write the unit test cases for the code.
Problem Solving Assistant#
When you receive a requirement, you often have no idea or there are multiple implementation methods. At this time, you can ask ChatGPT to give you a solution and provide you with a problem-solving approach.
Companion for Learning Programming#
When learning a new technology, you often don't know where to start. You can ask ChatGPT to give you a learning outline and arrange a learning plan for you. You can also ask it questions about the problems you encounter. It can be a great companion for learning.
Tips and Tricks#
- When your conversation is going to be long, you can break it into paragraphs and ask ChatGPT to respond to each paragraph, and add "If you understand, please answer clearly" in each paragraph.
- When ChatGPT's response is already comprehensive and only a small part needs to be modified, it is recommended to do it yourself. If you insist on asking ChatGPT for a completely accurate answer, it may not be worth the effort.
Finally, ChatGPT is not omnipotent. Don't expect to rely on it to solve everything when you don't know anything. It's like a tool that can assist you in solving problems, but you still need to have your own understanding and judgment.