跳转到正文
莫尔索随笔
返回

50 个 ChatGPT 实用 Prompt:提升软件开发效率的 AI 编程指南

预计 7 分钟

第一时间捕获有价值的信号

本文总结 50 个 ChatGPT 实用 Prompt,涵盖代码生成、完成、错误检测、审查等软件开发任务。助你利用 AI 编程工具,大幅提升开发效率和代码质量。

核心内容

这篇文章总结了 50 条一些适合软件开发的 ChatGPT 提示词。这些提示可以协助完成代码生成、代码完成、错误检测、代码审查、API 文档生成等任务。我将其简单翻译如下,并附上英文对照原文

Code Generation 代码生成

  • Generate a boilerplate [language] code for a [class/module/component] named [name] with the following functionality: [functionality description]. 为名为 [name] 的 [class/module/component] 生成具有以下功能的样板 [language] 代码: [functionality description].
  • Create a [language] function to perform [operation] on [data structure] with the following inputs: [input variables] and expected output: [output description]. 创建一个 [language] 函数以使用以下输入在 [data structure] 上执行 [operation] :[输入变量] 和预期输出: [output description]
  • Generate a [language] class for a [domain] application that includes methods for [methods list] and properties [properties list]. 为 [domain] 应用程序生成一个 [language] 类,其中包括 [methods list] 的方法和 [properties list] 的属性。
  • Based on the [design pattern], create a code snippet in [language] that demonstrates its implementation for a [use case]. 基于[设计模式],用[语言]创建一个代码片段,演示其对[用例]的实现。

Example:

Generate a boilerplate Python code for a shopping cart module named "ShoppingCart" with the following functionality:

- A constructor that initializes an empty list to store cart items.

- A method called "add_item" that takes in an item object and adds it to the cart.

- A method called "remove_item" that takes in an item object and removes it from the cart if it exists.

- A method called "get_items" that returns the list of items in the cart.

- A method called "get_total" that calculates and returns the total price of all items in the cart.

Code Completion 代码完成

  • In [language], complete the following code snippet that initializes a [data structure] with [values]: [code snippet]. 在 [language] 中,完成以下用 [values] 初始化 [数据结构] 的代码片段: [code snippet]
  • Finish the [language] function that calculates [desired output] given the following input parameters: [function signature]. 在给定以下输入参数的情况下完成计算 [所需输出] 的 [language] 函数: [function signature]
  • Complete the [language] code to make an API call to [API endpoint] with [parameters] and process the response: [code snippet]. 完成 [language] 代码以使用 [parameters] 对 [API endpoint] 进行 API 调用并处理响应: [code snippet]

Example : Finish the Python function that calculates the average of a list of numbers given the following input parameters: 示例:完成计算给定以下输入参数的数字列表的平均值的 Python 函数:

def calculate_average(num_list)

Bug Detection 错误检测

  • Identify any potential bugs in the following [language] code snippet: [code snippet]. 识别以下 [语言] 代码片段中的任何潜在错误: [code snippet]
  • Analyze the given [language] code and suggest improvements to prevent [error type]: [code snippet]. 分析给定的 [language] 代码并提出改进建议以防止 [error type]: [code snippet]
  • Find any memory leaks in the following [language] code and suggest fixes: [code snippet]. 在以下 [语言] 代码中查找任何内存泄漏并提出修复建议: [code snippet]

Example : Identify any potential bugs in the following Python code snippet: 示例:识别以下 Python 代码片段中的任何潜在错误:

def calculate_sum(num_list):
    sum = 0
    for i in range(len(num_list)):
        sum += num_list[i]
    return sum

Code Review 代码审查

  • Review the following [language] code for best practices and suggest improvements: [code snippet]. 查看以下 [language] 代码以获得最佳实践并提出改进建议: [code snippet]
  • Analyze the given [language] code for adherence to [coding style guidelines]: [code snippet]. 分析给定的 [language] 代码是否遵守 [coding style guidelines] : [code snippet]
  • Check the following [language] code for proper error handling and suggest enhancements: [code snippet]. 检查以下 [语言] 代码以正确处理错误并提出改进建议: [code snippet]
  • Evaluate the modularity and maintainability of the given [language] code: [code snippet]. 评估给定 [language] 代码的模块化和可维护性: [code snippet]

Example : Review the following Python code for best practices and suggest improvements: 示例:查看以下 Python 代码以获得最佳实践并提出改进建议:

def multiply_list(lst):
    result = 1
    for num in lst:
        result *= num
    return result

API Documentation Generation API 文档生成

  • Generate API documentation for the following [language] code: [code snippet]. 为以下 [language] 代码生成 API 文档: [code snippet]
  • Create a concise API reference for the given [language] class: [code snippet]. 为给定的 [language] 类创建简明的 API 参考: [code snippet]
  • Generate usage examples for the following [language] API: [code snippet]. 为以下 [language] API 生成用法示例: [code snippet]

Example : Generate API documentation for the following JavaScript code: 示例:为以下 JavaScript 代码生成 API 文档:

/**
 * Returns the sum of two numbers.
 * @param {number} a - The first number to add.
 * @param {number} b - The second number to add.
 * @returns {number} The sum of a and b.
 */
function sum(a, b) {
  return a + b;
}

Query Optimization 查询优化

  • Optimize the following SQL query for better performance: [SQL query]. 优化以下 SQL 查询以获得更好的性能: [SQL query]
  • Analyze the given SQL query for any potential bottlenecks: [SQL query]. 分析给定的 SQL 查询是否存在任何潜在瓶颈: [SQL query]
  • Suggest indexing strategies for the following SQL query: [SQL query]. 为以下 SQL 查询建议索引策略: [SQL query]
  • Optimize the following NoSQL query for better performance and resource usage: [NoSQL query]. 优化以下 NoSQL 查询以获得更好的性能和资源使用: [NoSQL query]

Example : Optimize the following SQL query for better performance: 示例:优化以下 SQL 查询以获得更好的性能:

SELECT *
FROM orders
WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31'
ORDER BY order_date DESC
LIMIT 100;

User Interface Design 用户界面设计

  • Generate a UI mockup for a [web/mobile] application that focuses on [user goal or task]. 为专注于 [ user goal or task][web/mobile] 应用程序生成 UI 模型。
  • Suggest improvements to the existing user interface of [app or website] to enhance [usability, accessibility, or aesthetics]. 建议改进 [app or website] 的现有用户界面以增强 [usability, accessibility, or aesthetics]
  • Design a responsive user interface for a [web/mobile] app that adapts to different screen sizes and orientations. 为 [web/mobile] 应用程序设计响应式用户界面,以适应不同的屏幕尺寸和方向。

Example : Generate a UI mockup for a mobile application that focuses on managing personal finances. 示例:为专注于管理个人财务的移动应用程序生成 UI 模型。

Automated Testing 自动化测试

  • Generate test cases for the following [language] function based on the input parameters and expected output: [function signature]. 根据输入参数和预期输出为以下 [语言] 函数生成测试用例: [function signature]
  • Create a test script for the given [language] code that covers [unit/integration/system] testing: [code snippet]. 为涵盖 [单元/集成/系统] 测试的给定 [语言] 代码创建测试脚本: [code snippet]
  • Generate test data for the following [language] function that tests various edge cases: [function signature]. 为以下测试各种边缘情况的 [language] 函数生成测试数据: [function signature]
  • Design a testing strategy for a [web/mobile] app that includes [unit, integration, system, and/or performance] testing. 为 [网络/移动] 应用程序设计测试策略,包括 [单元、集成、系统和/或性能] 测试。

Example: Generate test cases for the following Python function based on the input parameters and expected output: 示例:根据输入参数和预期输出为以下 Python 函数生成测试用例:

def divide(a: float, b: float) -> float:
    if b == 0:
        raise ZeroDivisionError('division by zero')
    return a / b

Code refactoring 代码重构

  • Suggest refactoring improvements for the following [language] code to enhance readability and maintainability: [code snippet]. 建议对以下 [语言] 代码进行重构改进,以增强可读性和可维护性: [code snippet]
  • Identify opportunities to apply [design pattern] in the given [language] code: [code snippet]. 确定在给定的 [语言] 代码中应用 [设计模式] 的机会: [code snippet]
  • Optimize the following [language] code for better performance: [code snippet]. 优化以下 [语言] 代码以获得更好的性能: [code snippet]

Example : Optimize the following Python code for better performance: 示例:优化以下 Python 代码以获得更好的性能:

def find_max(numbers):
    max_num = numbers[0]
    for num in numbers:
        if num > max_num:
            max_num = num
    return max_num

Design pattern suggestions 设计模式建议

  • Based on the given [language] code, recommend a suitable design pattern to improve its structure: [code snippet]. 根据给定的[语言]代码,推荐合适的设计模式来改进其结构: [code snippet]
  • Identify opportunities to apply the [design pattern] in the following [language] codebase: [repository URL or codebase description]. 确定在以下 [语言] 代码库中应用 [设计模式] 的机会: [repository URL or codebase description]
  • Suggest an alternative design pattern for the given [language] code that may provide additional benefits: [code snippet]. 为可能提供额外好处的给定 [语言] 代码建议替代设计模式: [code snippet]

Example: Based on the given Python code, recommend a suitable design pattern to improve its structure: 示例:根据给定的Python代码,推荐合适的设计模式来改进其结构:

class TotalPriceCalculator:
    def calculate_total(self, items):
        pass

class NormalTotalPriceCalculator(TotalPriceCalculator):
    def calculate_total(self, items):
        total = 0
        for item in items:
            total += item.price * item.quantity
        return total

class DiscountedTotalPriceCalculator(TotalPriceCalculator):
    def calculate_total(self, items):
        total = 0
        for item in items:
            total += item.price * item.quantity * 0.9 # apply 10% discount
        return total

class Order:
    def __init__ (self, items, total_price_calculator):
        self.items = items
        self.total_price_calculator = total_price_calculator

    def calculate_total(self):
        return self.total_price_calculator.calculate_total(self.items)

class Item:
    def __init__ (self, name, price, quantity):
        self.name = name
        self.price = price
        self.quantity = quantity

Algorithm development 算法开发

  • Suggest an optimal algorithm to solve the following problem: [problem description]. 建议解决以下问题的最佳算法: [problem description]
  • Improve the efficiency of the given algorithm for [specific use case]: [algorithm or pseudocode]. 提高 [specific use case] : [algorithm or pseudocode] 给定算法的效率。
  • Design an algorithm that can handle [large-scale data or high-throughput] for [specific task or operation]. 设计一个可以处理 [specific task or operation][large-scale data or high-throughput] 的算法。
  • Propose a parallel or distributed version of the following algorithm to improve performance: [algorithm or pseudocode]. 提出以下算法的并行或分布式版本以提高性能: [algorithm or pseudocode]

Code translation 代码翻译

  • Translate the following [source language] code to [target language]: [code snippet]. 将以下 [source language] 代码翻译成 [target language][code snippet]
  • Convert the given [source language] class or module to [target language] while preserving its functionality and structure: [code snippet]. 将给定的 [source language] 类或模块转换为 [target language] ,同时保留其功能和结构: [code snippet]
  • Migrate the following [source language] code that uses [library or framework] to [target language] with a similar library or framework: [code snippet]. 将以下使用 [library or framework][source language] 代码迁移到具有类似库或框架的[目标语言]: [code snippet]

Example: Translate the following Python code to JavaScript: 示例:将以下 Python 代码转换为 JavaScript:

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

Personalized learning 个性化学习

  • Curate a list of resources to learn [programming language or technology] based on my current skill level: [beginner/intermediate/advanced]. 根据我当前的技能水平整理学习 [programming language or technology] 的资源列表:@1​​#。
  • Recommend a learning path to become proficient in [specific programming domain or technology] considering my background in [existing skills or experience]. 考虑到我在 [existing skills or experience] 的背景,推荐一个精通 [specific programming domain or technology] 的学习路径。
  • Suggest project ideas or coding exercises to practice and improve my skills in [programming language or technology]. 建议项目想法或编码练习以练习和提高我在 [programming language or technology] 中的技能。

Code visualization 代码可视化

  • Generate a UML diagram for the following [language] code: [code snippet]. 为以下 [language] 代码生成 UML 图: [code snippet]
  • Create a flowchart or visual representation of the given [language] algorithm: [algorithm or pseudocode]. 创建给定 [language] 算法的流程图或可视化表示: [algorithm or pseudocode]
  • Visualize the call graph or dependencies of the following [language] code: [code snippet]. 可视化以下 [language] 代码的调用图或依赖关系: [code snippet]

Example : Generate a UML diagram for the following Java code: 示例:为以下 Java 代码生成 UML 图:

public abstract class Vehicle {
    private String model;

    public Vehicle(String model) {
        this.model = model;
    }

    public String getModel() {
        return model;
    }

    public abstract void start();

    public abstract void stop();
}

public class Car extends Vehicle {
    public Car(String model) {
        super(model);
    }
    @Override
    public void start() {
        System.out.println("Starting car engine");
    }
    @Override
    public void stop() {
        System.out.println("Stopping car engine");
    }
}
public class Motorcycle extends Vehicle {
    public Motorcycle(String model) {
        super(model);
    }
    @Override
    public void start() {
        System.out.println("Starting motorcycle engine");
    }
    @Override
    public void stop() {
        System.out.println("Stopping motorcycle engine");
    }
}

Data visualization 数据可视化

  • Generate a bar chart that represents the following data: [data or dataset description]. 生成代表以下数据的条形图: [data or dataset description]
  • Create a line chart that visualizes the trend in the following time series data: [data or dataset description]. 创建一个折线图,以可视化以下时间序列数据中的趋势: [data or dataset description]
  • Design a heatmap that represents the correlation between the following variables: [variable list]. 设计一个表示以下变量之间相关性的热图: [variable list]