【AI Thinking Space】ChatGPT vertical and horizontal programming world, lighting up the spark of wisdom | JD Cloud technical team

Author: JD Retail Wang Yingjie

overview

This document records the actual application cases of cloud transaction development partners in the development process, records typical cases, and focuses on solving practical problems in the development process, covering design solutions, coding, testing, integration, deployment, etc.

Purpose: To contribute best practices, share experiences, and grow together!

1. How to construct Prompt

1.1 Basic composition

In general, Prompt can be divided into the following four parts:

  • Instruction: guidance, that is, the type of problem to be solved
  • Context: context, the background of the problem
  • Input Data: input data, that is, specific questions
  • Output Indicator: Output indication, that is, some constraints on the output

Example:

Instruction: Explain to me the technology stack required for the front-end Context: Assume that you are a front-end interviewer, and I am a fresh graduate who graduated from a bachelor's degree InputData: Explain to me the skills that the front-end industry requires fresh graduates to master at this stage Output Indicator: Use as simple as possible understandable language

1.2 Design principles

  • Be clear , avoid complexity or ambiguity, and if there are terms, they should be clearly defined.
  • Concrete , the description language should be as specific as possible, not abstract or ambiguous.
  • Focus and avoid questions that are too broad or open.
  • Be concise and avoid unnecessary descriptions.
  • Related , mainly refers to the theme related, and it is during the whole dialogue, don't go over everything.

Example:

✅Effective Prompt Summarize the content of "Robinson Crusoe"? # Focus, related What are the most authentic local restaurants in Beijing? # specific, relevant

❌ Invalid Prompt Tell me about why this world exists? # Can being broad and open get the job done for me? #Open Hello# No purpose, no focus

2. Best Practice Case Collection

Case 1 Quickly optimize code during development

(1) Provide code snippets and put forward your own specific optimization direction at the appeal level

(2) Further propose specific optimization directions

(3) The optimized code is processed and tested according to the business scenario

  • Get the optimized code and integrate it into the project
  • Optimize the code according to the business scenario, and change the captured exception Exception to NullPointerException (Optimization scenario: Due to the consistency problem here, when checking the commodity master data, there is a scenario where the commodity master data write service data has not been synchronized to the read service, and a null pointer is reported , you need to retry after fetching)
  • After optimization, there is no problem in the self-test

Case 2 Using chatGPT to optimize the JVM

Case 3 SQL optimization and troubleshooting

1) Help check if there is any problem with the sql, this is to add a few fields to the business table

It helps to identify some problems

Modify according to the question it prompts;

I let it merge three sql into one, otherwise jed will prompt to merge, so let GPT help merge.

It is still very smart, and jed's sql checks have also passed smoothly, which can help us review the correctness of sql and reduce the risk of wrong operations.

Case 4 GPT generates tool classes on demand

You are a programmer, and you are helping to write a program. You are required to enter a string of strings separated by ",", with a maximum of 50 segments, and then you need to check whether these segments are duplicated, and return the duplicates in the set;

GPT generates python code for me;

Re-question: You are a Java programmer, and you are helping to write a program. You are required to enter a string of strings, separated by "," in the middle, with a maximum of 50 segments, and then you need to check whether these segments are duplicated, and the duplicates are placed in the set return inside

Optimize again: use 50 as a variable, don't write it hard, and regenerate a method;

Summary: In the project, there is a need to verify a string of 50 channels, separated by commas, and determine whether the channels are duplicated. Therefore, by asking questions step by step, let GPT help generate a tool class , so that we can directly use the modification tool in the project class for quick verification.

Case 5 The cloud transaction encryption service needs to be deployed on the Douyin server, and the deployment environment needs to be built from scratch.

Background: Due to the long-term use of Jingdong Jdos to deploy code, some instructions for Linux have been forgotten. So I use ChatGPT to guide me to build a JAVA deployment environment from scratch.

1) Below is the command I gave to ChatGPT

Instruction 1: Install the JAVA environment on CentOS7, then start the java jar package in the background through the nohub command and output the log to the douyin.txt file

ChatGPT answers as follows:

Instruction 2: It is too troublesome to download and install JDK from the official website. Help me to replace the above steps of installing jdk with yum installation

ChatGPT answers as follows:

2) Next, install JDK8 by referring to the instructions it gives, and then detect the JDK version, and it is successfully installed.

3) Next, I uploaded the jar package to a specified directory, and I forgot the command to create the directory. . . Just ask ChatGPT.

4) Upload the developed jar package to the created directory

5) Execute the nohup command

6) Browser access to swagger is successful

6) Using experience, my problem was solved in a few minutes. It has prevented me from going to Baidu step by step, and it is easy to find wrong answers when I go to CSDN. Great time saver for me. Because there are still several machines to be deployed later, the commands and steps have been organized into documents and settled down.

Case 6 jar package stop process Linux command consultation

Last time I used chatgpt to help me inquire about the command to build the java environment in linux and deploy the jar package in the background. This time the jar package update needs to replace the jar package. I also consulted how to stop the process where the jar package is located.

1) Execute the first command, ps -ef | grep jar command to find all running jar package processes

2) Execute the second command, kill -9 PID, here the process PID of my jar package is 32723

3) Re-deploy the jar package service. This command has been asked by ChatGPT before and settled down. Copy it and use it directly.

4) Check whether the deployment is successful

Case 7 If the Turing Award winner and the author of the software engineering bible "Mythical Man-Month" were still alive, how would he view ChatGPT to improve R&D efficiency?

Think about how ChatGPT can improve R&D efficiency today. Just when I was racking my brains for this problem. Suddenly, I had a flash of inspiration, why not ask ChatGPT to help me think about how to use ChatGPT to improve R&D efficiency?

Just like in martial arts TV dramas and movies, in the drama, a martial arts master can fly into the air by stepping on his left foot and his right foot. Then I asked ChatGPT how to use ChatGPT to improve R&D efficiency, and it is definitely feasible. The theory works, so OK let's get into practice.

First we set a role for ChatGPT. Who are the very experienced masters in the field of software engineering efficiency improvement? I immediately thought of the author of the Mythical Man-Month - FrederickP.Brooks.Jr Brooks

1) Let's give it an instruction: If you are Frederick P. Brooks. Jr., the author of "The Mythical Man-Month", you need to think about the questions I want you to ask from its thinking and perspective. If you understand, please reply ok.

We can see that it replied OK.

2) Next, we give it a second instruction: ChatGPT is very powerful now, how do you think it can be used to improve R&D efficiency?

It answers from the perspective of a software engineer that ChatGPT technology can improve R&D efficiency from the perspectives of automated testing, language translation, code optimization, and automatic documentation.

1 and 3 have already been thought of, 2 is not currently needed, and the fourth automatic documentation is really not thought of. Next, let's continue to in-depth questioning in combination with the automatic documentation of Article 4.

3) Next is the third instruction: For the fourth automatic documentation, please write a Java controller interface for querying the student's name and age, and then generate an API document for the interface to provide to the front end.

Below it directly writes an API interface, and directly uses swagger to generate api documents.

swagger interface

The result is not very satisfactory, let's ask if there are other suggestions to improve R&D efficiency with ChatGPT

4) Next is the fourth instruction: Please put forward 10 suggestions for improving R&D efficiency with ChatGPT from the perspective of Frederick P. Brooks. Jr., the author of "Mythical Man-Month".

In the end it helped us generate 10 suggestions

Here are some suggestions for you:

1. By providing natural language processing capabilities, Chat GPT can help team members communicate and collaborate more efficiently, reducing misunderstandings and miscommunication.

2. ChatGPT can be used for intelligent project management. For example, use the Chat GPT chatbot in a team to track progress, assign tasks, and coordinate the work of team members.

3. By understanding the intent and needs of developers, Chat GPT can provide more intelligent and personalized development aids, such as code hints, error checking, etc.

4. ChatGPT can use machine learning and data mining technology to extract best practices, code snippets and templates from existing code bases, and recommend best practices and code reuse to developers.

5. ChatGPT can be used as a knowledge base to help developers get the required information faster. For example, ChatGPT can answer questions about a certain technology or library, or provide code samples, etc.

6. During the development process, Chat GPT can automatically conduct code review and testing, provide timely feedback and correct errors, and help developers discover and solve problems earlier.

7. ChatGPT can automatically analyze duplicate code, legacy code and inefficient code in the code base, provide optimization suggestions, and reduce the burden of code maintenance.

8. ChatGPT can help developers conduct data analysis and visualization, provide interactive exploration and information presentation functions, so as to better understand data and models.

9. By using Chat GPT to generate natural language descriptions, the results of machine learning models can be presented to non-technical personnel in a more readable and understandable way, and communication efficiency can be improved.

10. Finally, Chat GPT can be used to develop intelligent applications, such as chat robots, intelligent customer service, etc., so as to better serve users.

Case 8 Use ChatGPT to help find bugs

Using mybatis to judge the conditional expression that the String string is empty, I wrote || at the beginning. I watched it for a long time and didn't see the problem. Ask ChatGPT to help me find out at a glance.

Case 9 Exploration of refactoring code AI plug-in

idea installation plug-in [NexChatGpt]

1. In actual development, perform abstract refactoring on the two familiar classes in the code.

The abstract parent class of the two classes can be directly generated for reference

At present, this plug-in sometimes has problems, and idea needs to be restarted.

Sometimes you are prompted to paste your own code. . .

Ask 30 questions for free. . .

Also a little unstable. . .

Case 10 Generate an algorithm for randomly obtaining ip

A small test of the chopper, successfully done

Use a random algorithm to get an ip

With ip as an input parameter, my needs are successfully met

Case 11 Help write a simple spring ioc framework

It's too complicated to support at all, so here's a simple one

Then improve it, and help write a test case test to see

Verify by yourself whether the test is correct and whether it can run

Although it can run, the test results are inconsistent with its expectations. The reason for the analysis error is that the annotation inject is used incorrectly. When using it correctly, you need to add the meta-annotation @Retention to the annotation. Its function can be simply understood as setting the life cycle of the annotation.

The @Retention annotation passes in the RetentionPolicy enumeration, which has three constants, namely SOURCE, CLASS and RUNTIME

The difference between the three is as follows:

SOURCE means that annotations are only kept at the source level, and will be discarded when the compiler compiles Java files into class files.

CLASS means that the annotations are kept in the class file, and will be discarded when the JVM loads the class file.

RUNTIME means that the marked annotation will be preserved by the JVM, so the runtime environment can use it.

Summary: chat-gpt may not be able to control the technical details, but the overall program framework and logic verification are still no problem.

Case 12 Generate a mind map of common Linux commands and use markdown syntax to output

(This case can be used in the use of any mind map to improve the efficiency of writing mind maps, tips: thinking supports markdown file import)

Case 13 Name the project structure/module/class, and the naming is simple.

Martin Fowler proposed a classic language about naming: "A good naming is like good magic, it can make things clear and understandable." This language expresses the importance of good naming in code, good The naming can help readers understand the intent and function of the code faster, and improve the readability and maintainability of the code. At the same time, precise naming can also reduce ambiguity and errors in the code, making the code more robust.

Taking the e-commerce commodity system as an example, provide a DDD project module naming example

Case 14 Character processing, text formatting

Formatting and compression of JSON strings:

Case 15 Add code comments/code review/ask code design ideas

ChatGPT generates code comments to improve code readability and maintainability.

Code readability is important because code is written and maintained by humans. When the code is poorly readable, it

It will be difficult to understand and modify, which will affect the maintainability and scalability of the code. especially big

Everyone works in a team. If the readability of the code is poor, it will inevitably affect other people in the team.

Often not easy to expand.

ChatGPT conducts code reviews to improve the quality and reliability of the code.

You can also conduct code reviews by using ChatGPT. Code review is the process of examining code to discover and

The process of fixing bugs, defects, and bad practices. ChatGPT for code reviews can help developers discover

Potential problems in the code and suggestions for improvement to improve the reliability of the code.

ChatGPT assists in code logic refactoring

Complex codes can be refactored as needed, and combined with design patterns, ChatGPT can be given instructions to refactor codes according to design patterns.

ChatGPT assists source code reading to improve source code reading skills.

Case 16 For the learning of new concepts, it can be developed step by step, for example.

Case 17 The origin of the poetic name of this article

The naming ability of ChatGPT is really impeccable, and the processing ability and understanding ability of natural language are too powerful!

Author: JD Retail Wang Yingjie

Content source: JD Cloud developer community

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/8785177