How to use ChatGPT to improve your "code" power?


  What is ChatGPT, I don't need to introduce it any more! I believe that everyone has seen its strength in the field of text. Although the current effect is still not comparable to that of professional text workers, its text ability has surpassed most ordinary people. The main reason is that it is quite cheap, allowing many text The relevant workers trembled. But today we will not discuss its text ability, but its code ability.

  After two months of use, I found that ChatGPT is still completely unable to replace programmers, especially in some strong context programming scenarios, such as some heavy business programming scenarios. However, in some more general programming scenarios, its capabilities are acceptable. To sum up, we programmers don't have to worry too much about ChatGPT robbing us of our jobs, but we can use it to complete some programming-related things and use it as a tool to greatly improve our work efficiency. Here are some of the things I can summarize Using some scenes of ChatGPT, I hope it will be helpful to everyone.

Code Review (CodeReview)

  You can use ChatGPT to conduct CodeReview on existing code, so as to detect problems early. ChatGPT can detect syntax errors
, code style issues, potential bugs, performance issues... However, it should be noted that ChatGPT may not be able to check the code as comprehensively and accurately as professional code review tools or experienced developers, while Errors may also occur. Therefore, in practical applications, it is recommended to use it only as an auxiliary tool, combined with other code review tools and manual review.

Reference prompt:

请对以下代码进行CodeReview,指出代码中可能存在的问题,包括但不仅限于代码风格、潜在bug、性能问题、代码可读性、代码安全性等问题。

Code optimization

  If you think the code review is not enough, you can directly let ChatGPT help you optimize and rewrite the entire code, of course, you can also ask it to return the reason for optimization. However, you need to pay attention to the same problem as the code review. ChatGPT can’t perform like a professional programmer. It is still recommended to use it as an auxiliary tool, refer to the results it gives, and optimize the code in combination with the suggestions of professionals.

Reference prompt:

请在保持原有代码逻辑的情况下优化下以下代码,尽可能提升代码的可读性、可维护性、性能……,并给出优化的理由。

Remarks: ChatGPT’s level of code review and code optimization is basically equivalent to the level of a senior engineer, but it is still far behind a senior engineer or expert in this field, so I personally recommend junior engineers Use ChatGPT to optimize your own code, and learn how to upgrade code instructions by the way.

Code interpretation

  Programmers spend a large part of their daily work maintaining old code, especially uncommented code written by others. If you need to modify some logic, you have to try to understand the original code, which is quite painful. At this time, you can give the code to ChatGPT and let it help generate some code interpretations, which can reduce your burden of understanding the code to a certain extent.

Reference prompt:

请解释以下代码的逻辑,请在代码中合适的地方增加注释。

provide the solution

  In our daily work, we often encounter some problems that we don’t know how to program to solve, especially for novice programmers. There are many such problems. At this time, describe the problems you encounter to ChatGPT clearly and let it give some suggestions and solutions. There is no specific prompt for this kind of problem, but I can give you an example.

  We recently encountered an online problem in the production environment. Due to the growth of our data volume, the kafka messages of the same device may be consumed by multiple machines at the same time, resulting in incorrect data status. At this time, I can ask ChatGPT to give me some suggestions to solve the problem , ChatGPT’s answer to this question is as follows: There
insert image description here
are many programming problems that can be solved with ChatGPT, and some small examples: such as how to solve the problem of information synchronization between multiple threads, how to add Cache in the code, which open source package in java Are there immutable collection classes that can be used directly? ?

code generation

For some given questions, ChatGPT can actually generate relevant code directly, and the generated code instructions are also available. For example, I used ChatGPT to generate a distributed lock annotation code last week, and the usability is quite high.
insert image description here
  The whole context is too long, so I won't post it in full. For the issue of distributed lock annotations, ChatGPT not only provides annotation interface codes, but also complete aspect implementation codes, and even specific usage examples. The complete demo can be run in minutes.

Remarks: I have asked ChatGPT several times about distributed locks, and the answers are still slightly different each time. There are even some problems with some codes, which need to be identified by myself. In short, it is not recommended to trust the answers of ChatGPT at present, and you need to do your own screening.

  There are also some code scenarios that are very practical to use ChatGPT, such as using it to generate unit tests for a certain piece of code. Although it cannot take into account a variety of data cases, at least it can help you write the basic code of the single test, you only need to improve other data.

Design Patterns and Architecture Recommendations

  Some of the above examples are how to make good use of ChatGPT at the level of code and implementation, but in fact, ChatGPT can also provide some suggestions at a higher level, such as code design or even architecture design. For example, our system now encounters a problem. Because it is a single application, http service, kafka consumer, and scheduled tasks are running on it, and different tasks may seize resources from each other. Therefore, we have been planning to split the service. At this time, you can seek advice from ChatGPT.

insert image description here
  Judging from ChatGPT's answer, it is still quite valuable.

learn new knowledge

  If you are a senior engineer in a certain field, I am sure that ChatGPT is definitely not as strong as you in this respect, but the breadth of its knowledge neighborhood is definitely far beyond your reach. Someone may achieve 90 points in a certain field, but ChatGPT can achieve 60 points or even higher in most fields, and we alone may only be able to achieve 10 points and 20 points in most fields, so ChatGPT can help you learn in areas you are not familiar with.

  To give a very simple example, I am a back-end engineer. I may have a Java level of 90 points, but I don’t understand the front-end code, but I can understand it. The level is about ten or twenty points. But with the help of ChatGPT, I used Electron to write a ChatGPT desktop client. There are really countless examples like this, as long as you are willing to learn, ChatGPT can teach you anything.

insert image description here

Remarks: Due to the timeliness of ChatGPT training data, it cannot provide very new information, and sometimes there will be random compilations, pay attention to screening.

Summarize

  I still remember that when ChatGPT first became popular, a certain boss made a bold statement that ChatGPT would replace xx% of programmers within x years. Recently, similar remarks have decreased a lot. Personally, I don’t think it will be that exaggerated, but it will definitely be Replace some of them, especially those with low technical levels (such as simple CURD). But as long as we learn to use ChatGPT well, we will not be replaced by it. Finally, I attach a sentence that Kevin Kelly said in an interview with csdn some time ago. AI will replace 90% of human skills and amplify the remaining 10% .

Guess you like

Origin blog.csdn.net/xindoo/article/details/130916178