给程序员的ChatGPT使用指南:优化工作流程的 3 种实用方法

article/2024/9/20 17:41:44

ChatGPT for Coders: 3 Practical Ways to Optimise your Workflow

e6933692ea44c49afae3ecdcafde6463.png

自从 ChatGPT 发布以来,我在互联网上看到了很多关于它对未来编程可能意味着什么的讨论。

肯定有很多“FUD”在那里传播,但它基本上归结为两个论点:

  1. 编程将继续有利可图,我们会使用 ChatGPT 和 Copilot 等工具进行调整。

  2. ChatGPT 和 Copilot 等工具只是未来的开始,编程的未来是不确定的。

老实说,这两种未来都是可能的,甚至可能不会相互排斥。但是,该讨论可以保留到另一篇文章。

截至今天,程序员可以从像 ChatGPT 这样的工具中受益,我们应该拥抱它而不是回避它。

在这篇文章中,我想强调 3 个 ChatGPT 用例,这些用例让我受益匪浅,也可能对您有所帮助。特别是,它们是:解释代码、调试和编写样板。

Since the release of ChatGPT, I’ve seen plenty of discussion on the internet about what it might mean for the future of programming.

There is certainly a lot of “FUD” being spread out there, but it essentially boils down to two arguments:

  1. Programming will continue to be lucrative, and we adapt using tools like ChatGPT and Copilot.

  2. Tools like ChatGPT and Copilot are only the start of what is to come, and the future of programming is uncertain.

In honesty, both of these futures are possible and might not even be mutually exclusive. However, that discussion can be reserved for another article.

As of today, programmers can stand to benefit from a tool like ChatGPT, which we should embrace rather than shy away from.

In this post, I want to highlight 3 use cases of ChatGPT that have benefited me and could help you too. In particular, these are: Explaining Code, Debugging, and Writing Boilerplates.

1.解释代码

假设您选择了一个具有现有代码库的新项目。

您正在尝试了解所有内容是如何拼凑在一起的,以前的开发人员编写了什么以及它是如何工作的。这可能是一个相当大的挑战。

虽然 ChatGPT 无法拼凑出整个程序的工作原理,但它可以帮助您了解特定的部分或部分。

例如,我要求 ChatGPT 解释以下摘自ProbeX的代码,这是我正在从事的一个副项目。

1. Explaining Code

Say you’ve picked up a new project with an existing codebase.

You’re trying to understand how everything is pieced together, what the previous developers have written and how it works. This can be quite a challenge.

While ChatGPT can’t piece together how the entire program works, it can help you understand particular sections or segments.

For example, I asked ChatGPT to explain the following piece of code taken from ProbeX, a side project I’m working on.

function recent_scans() {var xhttp = new XMLHttpRequest();xhttp.onreadystatechange = function() {if (this.readyState == 4 && this.status == 200) {resp = JSON.parse(this.responseText);for (key in resp){if(location.href.search('reports.html') != -1){scan_data = "<a href=/reports.html#"+resp[key]['scanid']+" onclick='location.reload()'>"+resp[key]['url']+"</a><br>";} else{scan_data = "<a href=/reports.html#"+resp[key]['scanid']+">"+resp[key]['url']+"</a><br>";}// Update dictionaryconsole.log(resp[key]["scan_status"]);if(resp[key]["scan_status"] == "Completed"){resp[key].scan_status = '<span class="label label-success">Completed</span>';}else{resp[key].scan_status = '<span class="label label-warning">In Progress</span>';}resp[key].scanid = scan_data;resp[key].id = parseInt(key) + 1;console.log(resp);}$(function () {$('#table').bootstrapTable({data: resp});});}};xhttp.open("GET", "/scan/scanids/", true);xhttp.send();
}

ChatGPT 的回复如下:

此代码定义了一个名为“recent_scans()”的 JavaScript 函数。调用时,该函数使用 XMLHttpRequest 对象向指定的 URL(在本例中为“/scan/scanids/”)发送 GET 请求。当服务器响应时,该函数检查响应的 readyState 是否为 4(意味着请求已完成)和状态是否为 200(意味着请求成功)。

如果满足这些条件,该函数会将响应解析为 JSON 并使用 for 循环迭代 JSON 对象。对于对象中的每个键,它检查当前 URL 是否包含“reports.html”,如果包含,它会创建一个带有重新加载页面的 onclick 事件的锚标记。如果 URL 不包含“reports.html”,它会创建一个没有 onclick 事件的锚标记。

然后该函数更新对象中每个键的 scan_status,在状态旁边添加“已完成”或“进行中”标签。最后,该函数使用 Bootstrap Table jQuery 插件使用来自 JSON 对象的数据创建一个表并将其显示在页面上。

这让我初步了解了函数的作用,但我知道代码需要修复。特别是,reports.html点击时页面未加载。

因此,我请 ChatGPT 帮助我进行调试。

ChatGPT’s response was the following:

This code defines a JavaScript function called “recent_scans()”. When called, the function uses the XMLHttpRequest object to send a GET request to the specified URL (in this case, “/scan/scanids/”). When the server responds, the function checks that the response’s readyState is 4 (meaning the request is done) and status is 200 (meaning the request was successful).

If those conditions are met, the function then parses the response as JSON and uses a for loop to iterate over the JSON object. For each key in the object, it checks if the current URL contains “reports.html”, and if it does, it creates an anchor tag with an onclick event that reloads the page. If the URL does not contain “reports.html”, it creates an anchor tag without an onclick event.

The function then updates the scan_status of each key in the object, adding a ‘Completed’ or ‘In Progress’ label next to the status. Finally, the function uses the Bootstrap Table jQuery plugin to create a table with the data from the JSON object and display it on the page.

This gave me an initial understanding of what the function does, but I knew the code needed to be fixed. In particular, the reports.html page wasn’t loading upon click.

Hence, I asked ChatGPT to help me with debugging.

2、调试

在我看来,从编程的角度来看,这是 ChatGPT 最能改变生活的用例。正如我在之前的帖子中提到的,感觉就像是有第二个人与您一起解决问题。

现在,如果我简单地询问 ChatGPT 上面的代码有什么问题,它给我的建议太笼统了。因此,在这里必须尽可能具体。

例如,这是我输入的提示:

使用此代码时页面未被重定向;可能是什么问题?下面的代码部分是我希望您注意的部分。

scan_data = “<a href=/reports.html#”+resp[key]['scanid']+” οnclick='location.reload()'>”+resp[key]['url']+”</一个<br>”;

这是 ChatGPT 返回的响应:

  1. 确保将 scan_data 变量正确附加到页面的 HTML 中。在浏览器中加载页面后,您可以通过检查该页面的 HTML 源代码来检查这一点。

  2. 确保正确调用了 location.reload() 函数。您可以尝试将 console.log 语句添加到 onclick 事件处理程序,以确认单击链接时正在调用该函数。

  3. 确保 a 元素的 href 属性设置正确。如果 href 属性设置为错误的 URL,即使调用 location.reload() 函数,页面也不会重定向。

  4. 检查控制台是否有任何可能提供有关该问题的更多信息的错误消息。打开浏览器的开发人员控制台(通常通过按 F12)并查找可能有助于识别问题的任何错误消息。

这是一个很好的起点。

我尝试了它给我的每条建议,并将结果报告给了 ChatGPT。然而,这些建议都没有奏效。

无论如何,它帮助我更多地思考这个问题。通过与 ChatGPT(和谷歌)的来回过程,我意识到reports.html只需要更多时间来加载。

然后,我向 ChatGPT 报告了我的发现,并要求它生成延迟页面加载的代码。我将它的响应添加到代码库中,它起作用了!

在此必须注意,此过程是一个迭代过程。它需要与 ChatGPT 进行一些来回交流,并对程序员部分进行一些了解,以了解可能存在潜在问题的位置。

2. Debugging

In my opinion, this is the most life-changing use case of ChatGPT from a programming perspective. As I mentioned in my previous post, it almost feels like you have a second person working with you to solve a problem.

Now, if I simply ask ChatGPT what was wrong with the above code, it gives me advice that is too general. So, here it is essential to be as specific as possible.

For example, this is the prompt I entered:

The page isn’t being redirected when using this code; what could be the issue? The code section below is what I want you to pay attention to.

scan_data = “<a href=/reports.html#”+resp[key][‘scanid’]+” οnclick=’location.reload()’>”+resp[key][‘url’]+”</a><br>”;

Here was the response ChatGPT came back with:

  1. Make sure that the scan_data variable is being correctly appended to the HTML of the page. You can check this by inspecting the HTML source code of the page after it has been loaded in the browser.

  2. Make sure that the location.reload() function is being called correctly. You can try adding a console.log statement to the onclick event handler to confirm that the function is being called when the link is clicked.

  3. Make sure that the href attribute of the a element is set correctly. If the href attribute is set to an incorrect URL, the page will not redirect even if the location.reload() function is called.

  4. Check the console for any error messages that might provide more information about the issue. Open the browser’s developer console (usually by pressing F12) and look for any error messages that might help identify the problem.

This was a great starting point.

I tried each of the suggestions it gave me and reported the outcomes back to ChatGPT. However, none of the advice actually worked.

Regardless, it helped me to think more about the problem. Through a back-and-forth process with ChatGPT (and Google), I realised that reports.html just needed more time to load.

I then reported my findings to ChatGPT and asked it to generate code that delays the page’s loading. I added its response to the codebase, and it worked!

It is essential to note here that this process is an iterative one. It requires some back-and-forth with ChatGPT and some understanding on the programmers part to know where potential issues can exist.

3. 编写样板

最后,让 ChatGPT 编写整个代码样板可以极大地提高生产力。

这是 ChatGPT 的一个更常见的用例,但仍然是一个强大的用例。

从本质上讲,您可以要求 ChatGPT 编写完整的函数、脚本和代码段,您可以根据自己的具体情况对其进行修改。

让我们举一个基本的例子。

提示:编写一个简单的 Flask API。

ChatGPTs 响应的一部分:

3. Writing Boilerplates

Lastly, having ChatGPT write entire boilerplates of code can be a huge productivity boost.

This is a more common use case of ChatGPT, but nonetheless, a powerful one.

Essentially, you can ask ChatGPT to write entire functions, scripts, and segments of code, which you can modify with your own specifics.

Let’s take a rudimentary example.

Prompt: Write a simple Flask API.

Part of ChatGPTs response:

from flask import Flask, jsonify, requestapp = Flask(__name__)@app.route('/greet', methods=['GET'])
def greet():name = request.args.get('name')return jsonify(message=f"Hello, {name}!")if __name__ == '__main__':app.run(debug=True)

From here, we can simply change the route, the function name and the return value, and it would be ready for deployment.

Obviously, this is a trivial example that we could have just Googled. However, the premise remains the same if we wanted something more complex.

从这里,我们可以简单地更改route、函数名称和return值,它就可以部署了。

显然,这是一个简单的例子,我们可以用谷歌搜索。但是,如果我们想要更复杂的东西,前提保持不变。

Conclusion

If it hasn’t already, AI tools like ChatGPT will likely shape how programmers write code in the future. As I see it, this means we can spend more time on the quality of the product rather than the intricacies of the code.

The use cases I’ve highlighted in this post are ones that I’ve found to be the most revolutionary, but honestly, I believe this only scratches the surface. For instance, other scenarios that come to mind are code optimisation, documentation writing and learning new technologies.

All in all, I like to think of ChatGPT as my very own on-demand assistant programmer; there to help me when I need, if I need. That’s it.

结论

如果还没有,像 ChatGPT 这样的人工智能工具可能会影响程序员未来编写代码的方式。在我看来,这意味着我们可以将更多时间花在产品质量上,而不是代码的复杂性上。

我在这篇文章中强调的用例是我发现最具革命性的用例,但老实说,我相信这只是表面现象。例如,想到的其他场景是代码优化、文档编写和学习新技术。

总而言之,我喜欢将 ChatGPT 视为我自己的按需助理程序员;在我需要的时候在那里帮助我,如果我需要的话。就是这样。


http://chatgpt.dhexx.cn/article/GjNL1CIr.shtml

相关文章

ChatGPT能承担哪些工作?

ChatGPT拥有高效的自然语言处理能力&#xff0c;它最容易取代的领域可能是&#xff1a;文本分类、聊天机器人、文本生成、问答系统、文字识别和自动翻译方面。最容易取代的职业可能是&#xff1a;客服人员、技术类、媒体类、教师、法律类、金融类。使相关的人员在工作生活中更加…

重磅!ChatGPT 网页版来啦!响应速度超快!

平替 ChatGPT 继公众号对接 ChatGPT 以来&#xff0c;经过大家的试用与提出的建议&#xff0c;上线、优化了诸多功能&#xff0c;使得我们的公众号几乎完美~ 在此之上&#xff0c;不负众望&#xff0c;五一假期掏粪三天三夜&#xff0c;平替 ChatGPT 网页版 已经上线&#xf…

2023 最新ChatGPT网页源码 无需KEY打开即用

买了些key放着也是放着&#xff0c;所以干脆集成到源码里面了&#xff0c;不需要大家输入key密钥就可以用&#xff0c;上传到宝塔搭建即可!

2023 网页chatgpt 最新模型 可自定义api

最新网页版chatgpt 支持自定义api 国内服务器&#xff0c;虚拟主机都支持搭建 chatgpt 最新模型 自定义api入口&#xff1a;域名/adminkey.php 直接把源码放到根目录即可运行

怎么看待ChatGPT封号这件事呢?

最近的ChatGPT大量封号&#xff0c;刷爆了全网&#xff0c;我的两个个人账号被封禁了&#xff0c;不知道大家最近有没有遇到相关的报错信息&#xff0c;要么就是检查你当前的浏览器配置&#xff0c;最后来一个access denied&#xff0c;要么直接就给你来一个当前的国家不支持。…

开源chatgpt网站源码 带完整搭建教程

分享一个开源版的chatgpt网站源码&#xff0c;功能强大&#xff0c;带完整的搭建教程。 春哥AI智能助理系统源码&#xff0c;是一款非常强大的人工智能对话系统&#xff0c;它可以陪你聊天&#xff0c;帮你写文章&#xff0c;帮你写论文&#xff0c;帮你写代码&#xff0c;帮你…

ChatGPT中文网页版带PHP接口源码

☑️ 品牌&#xff1a;ChatGPT ☑️ 语言&#xff1a;PHP ☑️ 类型&#xff1a;ChatGPT ☑️ 支持&#xff1a;PCWAP &#x1f389;有需要的朋友记得关赞评&#xff0c;浏览底部查看&#xff01;&#xff01;&#xff01; &#x1f389; ✨ 源码介绍 ChatGPT 中文网页版带PHP…

2023最新ChatGPT网页版源码V4.7.8版+新增回答复制功能/功能强大

正文: V4.7.8 优化公告弹出必须点击"我记住啦"才可以关闭 防止用户误触没看到公告 优化手机端输入框有时候不下滑BUG 修复后台KEY密钥自动增加BUG 新增是否开启前台客服按钮开关 新增回答复制功能 程序: wweorgs.lanzouu.com/ihg980wag13i 图片:

使用 Tampermonkey 插件流畅使用 ChatGPT 网页端教程

在本教程中&#xff0c;我们将详细介绍如何使用 Tampermonkey 插件改善您在浏览器中使用 ChatGPT 网页版的体验。由于某些原因&#xff0c;浏览器访问 ChatGPT 容易出现断开连接或报错的情况。使用 Tampermonkey&#xff0c;您可以运行一个用户脚本&#xff0c;解决这些问题&am…

一键部署你的私人 ChatGPT 网页版,这款开源应用太牛了

一键免费部署你的私人 ChatGPT 网页应用。 在线部署 准备好你的 OpenAI API Key&#xff0c;进入 https://vercel.com/new开始部署&#xff1a; 环境变量 本项目大多数配置项都通过环境变量来设置。 OPENAI_API_KEY &#xff08;必填项&#xff09;&#xff1a;OpanAI 密钥&…

2023全新ChatGPT网页程序源码V4.2版本+支持用户付费套餐/有后台模块

正文: 4.2版本来了&#xff0c;优化新增了不少东西&#xff0c;安装简单 安装教程: 搭建宝塔 解析域名 上传程序至根目录 配置数据库信息:lib/config.php 导入数据库 PHP选择:7.3 访问网页即可&#xff01; 配置APIKEY&#xff0c;登录网站后台自定义配置&#xff0c…

2023最新商业版ChatGPT网页版源码V3.9+支持用户付费/功能强大

正文: 3.9版本已经更新&#xff01; 安装教程: 搭建宝塔 解析域名 上传程序至根目录 配置数据库信息:lib/config.php 导入数据库 PHP选择:7.3 访问网页即可&#xff01; 配置APIKEY&#xff0c;登录网站后台自定义配置&#xff0c;不然网站无法使用&#xff01; 网站…

2023最新ChatGPT网页版源码/支持付费购买套餐功能/UI也很不错

正文: 2.0版本的&#xff0c;前端模板已经更新了&#xff0c;之前那个太丑了&#xff0c;所以换成现在这样了 第一步-配置APIKEY:在"index.php"最顶部配置自己的APIKEY&#xff0c;不然网站无法使用&#xff01; 第一步-配置数据库:lib/config.php 第三步-导入数…

最新ChatGPT网页版源码无需KEY/打开就能用

简介&#xff1a; 买了些key放着也是放着&#xff0c;所以干脆集成到源码里面了&#xff0c;不需要大家输入key密钥就可以用&#xff0c;上传到宝塔搭建即可! 云盘地址&#xff1a; http://zijieyunpan.cn/m2duvYSjvrC 阅览&#xff1a;

基于Vercel自建ChatGPT网页应用

因为平时我们在使用ChatGPT官方提供的网页应用的时候&#xff0c;总是经常访问不了&#xff0c;或者经常报错&#xff0c;越来越频繁&#xff0c;一旦你离开页面太久&#xff0c;再返回跟它对话的时候&#xff0c;就会出现如下报错等等。 An error occurred. If this issue per…

2023全新UI商业版ChatGPT网页版源码V4.7.7+支持Ai绘画

正文: 安装教程: 搭建宝塔 解析域名 上传程序至根目录 配置数据库信息:lib/config.php 导入数据库 PHP选择:7.3 访问网页即可&#xff01; 配置APIKEY&#xff0c;登录网站后台自定义配置&#xff0c;不然网站无法使用&#xff01; 网站后台地址/admin 默认账号:admi…

精心设计的ChatGPT网页版源码最聪明的镜像ChatGPT

demo软件园每日更新资源,请看到最后就能获取你想要的: 1.精心设计的 ChatGPT网页版源码 最聪明的镜像ChatGPT ChatGPT网页版源码 最聪明的镜像ChatGPT 支持GPT3.5turbo以及GPT4.0turbo的各种版本模型 精心设计的 UI&#xff0c;响应式设计&#xff0c;支持深色模式 极快的首…

ChatGPT网页版(基于SpringBoot和Vue)

目录 1.使用说明1.1 登录1.2 获取OpenAI KEY或User Token1.3 创建对话1.4 删除会话 1.使用说明 1.1 登录 登录http://chatgpt.alpacos.cn/ 1.2 获取OpenAI KEY或User Token 如果自己有OpenAI的token(sk-xxxx)的话&#xff0c;可以直接输入&#xff0c;点击右上角绑定按钮&am…

ChatGPT实现HTML网页文本提取

网页自动化工具 既然ChatGPT对于编程语言有非常强大的理解能力&#xff0c;那么它是否可以用来自动化地处理网页呢&#xff1f;答案是肯定的。ChatGPT可以使用机器学习算法来识别网页元素中的文本&#xff0c;并抽取出有用的信息。 例如我们提供一段层数比较多的相对来说较为…

【ChatGPT实战】5.使用ChatGPT自动化操作网页

在当今数字化的时代&#xff0c;网页已经成为了人们获取信息、娱乐、社交和购物等方面的主要途径。然而&#xff0c;随着我们对网页的需求和使用不断增加&#xff0c;我们也经常会面临着一些繁琐的网页操作&#xff0c;例如自动填充表单、自动化浏览和搜索等&#xff0c;这些操…