ChatGPT 的五大功能可以帮助你提高代码质量

人工智能
ChatGPT 目前彻底改变了开发代码的方式,然而,大多数软件开发人员和数据专家仍然没有使用 ChatGPT 来改进和简化他们的工作。

ChatGPT 目前彻底改变了开发代码的方式,然而,大多数软件开发人员和数据专家仍然没有使用 ChatGPT 来改进和简化他们的工作。

这就是为什么我在这里概述 5 个不同的功能,以提高我们的日常工作速度和质量。

我们可以在日常工作中使用它们。现在,我们一起来了解一下吧。

注意:切勿在 ChatGPT 中使用关键代码或信息。

01.生成项目代码的框架

从头开始构建新项目时,ChatGPT 是我的秘密武器。只需几个提示,它就可以生成我需要的代码框架,包括我选择的技术、框架和版本。它不仅为我节省了至少一个小时左右的工作时间,而且还有助于使我的文档(或团队的文档)井井有条并保持最新,这就好像在给团队加外挂,感觉无形之中多了一双手!

例如,假设我想使用一些外部 API,我可以要求 ChatGPT 为我起草一个初始代码结构。

使用 Python 生成集成外部 API 的样板代码。

ChatGPT 立即回答:

图片

甚至,我可以请求一个框架来使用 Selenium 抓取 LinkedIn,ChatGPT 会给我以下输出。


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Initialize the web driver
driver = webdriver.Firefox()

# Navigate to LinkedIn
driver.get("https://www.linkedin.com/")

# Find the email and password input fields
email = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")

# Enter your credentials
email.send_keys("YOUR_EMAIL")
password.send_keys("YOUR_PASSWORD")

# Click the login button
driver.find_element_by_xpath("//button[@type='submit']").click()

# Wait for the page to load
time.sleep(5)

# Navigate to the profile of the desired user
driver.get("https://www.linkedin.com/in/USER_PROFILE_NAME")

# Extract the information you want from the page
name = driver.find_element_by_xpath("//span[@class='inline t-24 t-black t-normal break-words']").text
location = driver.find_element_by_xpath("//span[@class='t-16 t-black t-normal inline-block']").text

print(name)
print(location)

# close the browser
driver.quit()

很容易,对吧?尝试用您能想象到的任何项目来挑战 ChatGPT。

02.研究和比较

就如何实施某些事情做出决定可能很困难,尤其是当有多种选择可供选择时。我的首选方法是为每种方法创建一个基本的概念证明,然后将它们进行比较。但是,在 ChatGPT 的帮助下,这个过程变得简单多了。

我现在可以直接询问它的专家意见,了解哪个选项或库最适合我的代码开发。这节省了我在决策过程中的时间和精力,并确保我使用最好的工具来完成工作。

假设我想处理地理空间数据,但不确定是否应该使用 Geopandas 或 Plotly。我可以让 ChatGPT 为我进行比较——包含一个类型 ;它会立即回答两个库之间的主要区别。

屏幕截图来源与ChatGPT 聊天,ChatGPT 向我解释了 geopandas 和 plotly 之间的区别

如果现在我想抓取一个网站,我可以问问最好的库是什么。ChatGPT 使用 Python 中最流行的网络抓取库来回答。

图片

屏幕截图来源与ChatGPT 聊天,ChatGPT 解释最流行的抓取网站

你甚至可以询问你想要抓取的网站的最佳选择是什么——尽管 ChatGPT 很可能会警告你这将违反该网站的内容政策——所以要小心。

抓取社交网络的最佳选择是什么?

图片

03.理解代码

我们都去过那里,努力理解不是我们创建的代码库。浏览复杂且组织不当的代码(也称为意大利面条代码)可能是一项令人沮丧且耗时的任务。

但是,有了 ChatGPT,理解新的代码库就变得容易多了。我现在可以简单地要求它解释代码的功能并立即理解它。无需再浪费宝贵的时间和精力来破译糟糕的代码。

假设我正在尝试抓取 Linkedin,我在互联网上发现了一个随机代码,该代码应该可以向下滚动 Linkedin 工作机会网站。

以下代码的作用是什么?


#We find how many jobs are offered.
jobs_num = driver.find_element(By.CSS_SELECTOR,"h1>span").get_attribute("innerText")
if len(jobs_num.split(',')) > 1:
jobs_num = int(jobs_num.split(',')[0])*1000
else:
jobs_num = int(jobs_num)

jobs_num = int(jobs_num)

#Here I choose manually a number of jobs, so it wont take that long:
jobs_num = 1000;


#We create a while loop to browse all jobs.
i = 2
while i <= int(jobs_num/2)+1:
#We keep scrollind down to the end of the view.
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
i = i + 1
print("Current at: ", i, "Percentage at: ", ((i+1)/(int(jobs_num/2)+1))*100, "%",end="\r")
try:
#We try to click on the load more results buttons in case it is already displayed.
infinite_scroller_button = driver.find_element(By.XPATH, ".//button[@aria-label='Load more results']")
infinite_scroller_button.click()
time.sleep(0.1)
except:
#If there is no button, there will be an error, so we keep scrolling down.
time.sleep(0.1)
pass

ChatGPT 会立即对上面的代码进行非常详细的解释,你甚至可以在代码给定部分中询问更多详细信息——只需稍微玩一下 ChatGPT,您就会发现它可以创造很多奇迹。

图片

屏幕截图 ChatGPT 聊天,ChatGPT 解释了我没看懂的代码

04.在我们的代码中添加注释

ChatGPT 还可以帮助我们提高自己代码的质量和可维护性,通过你提出的要求,它会逐行添加注释,我们可以确保我们的代码在发布前得到正确记录。这帮助我们实现了对代码添加注释的要求,并使其他人更容易理解和使用我们的代码。

ChatGPT 可以明显提高了代码可读性、可维护性和与他人协作的能力。

假设之前的代码根本没有注释,我们可以让 ChaGPT 直接给它添加注释。

以下是ChatGPT为代码添加注释 :


# Find the number of jobs offered on the website
jobs_num = driver.find_element(By.CSS_SELECTOR,"h1>span").get_attribute("innerText")

# Check if the number of jobs contains a ',' and if so, convert it to number * 1000
if len(jobs_num.split(',')) > 1:
jobs_num = int(jobs_num.split(',')[0])*1000
else:
jobs_num = int(jobs_num)

# Convert jobs_num to an integer
jobs_num = int(jobs_num)

# Set the number of jobs to scrape to 1000
jobs_num = 1000;

# Initialize a counter variable to scroll through the jobs
i = 2

# Start a while loop to scroll through the jobs
while i <= int(jobs_num/2)+1:
# Scroll to the bottom of the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Increment the counter variable
i = i + 1
# Print the current number of jobs scrolled through and the percentage of completion
print("Current at: ", i, "Percentage at: ", ((i+1)/(int(jobs_num/2)+1))*100, "%",end="\r")
try:
# Try to locate the "Load more results" button and click on it
infinite_scroller_button = driver.find_element(By.XPATH, ".//button[@aria-label='Load more results']")
infinite_scroller_button.click()
# Sleep for 0.1 seconds
time.sleep(0.1)
except:
# If the button is not present, sleep for 0.1 seconds and continue scrolling
time.sleep(0.1)
pass

令人印象深刻吧?基本可以添加注释的代码均添加了。

05.使用某种风格重写我们的代码

ChatGPT 不仅是理解陌生代码的宝贵工具,而且还可以帮助我们确保自己的代码遵循行业标准和惯例,通过要求它更正我们的代码以符合 Pep-8 约定,或者甚至为我们的编码风格创建自定义约定,我们可以避免在合并来自不同存储库或团队的代码时进行昂贵且耗时的重构。

这有助于简化协作流程并提高效率,总的来说,ChatGPT 是一个多功能工具,可以提高我们代码库的质量和可维护性。

如果我们让ChatGPT用Pep-8标准写之前的代码,它会直接给我们重构后的代码。

你能用 Pep8 标准重写下面的代码吗 ?

图片

屏幕截图 ChatGPT 聊天,ChatGPT 按照 Pep8 标准提供我们的代码

总结

我希望读完本文后,您会意识到 ChatGPT 可以帮助我们提高工作效率并创造更高质量的输出。我知道很容易陷入认为人工智能最终会接管我们工作的陷阱,但正确的人工智能可以成为一种强大的资产,想办法让它可以为我们所用。

然而,重要的是要记住,批判性思维在与 AI 合作时仍然是关键,就像在与我们的人类同事合作时一样。

因此,在您急于实施 AI 生成的响应之前,请确保先花时间审查和评估它们。相信我,这最终是值得的!

如果 ChatGPT 的其他一些优秀功能让您感到惊讶,请您在留言区告诉我,让我们一起努力让人工智能为我们服务。

责任编辑:华轩 来源: web前端开发
相关推荐

2013-01-21 09:38:47

交换机交换技术网络设备

2012-04-17 14:23:49

2014-02-18 09:42:34

云存储公有云混合云

2011-05-13 17:24:46

金山Android版

2010-12-16 20:54:07

Windows Azu

2013-09-12 13:42:48

企业邮箱高效办公

2016-08-02 13:38:31

2009-07-01 09:41:00

无线路由器固件番茄固件

2010-11-10 10:29:51

职场

2010-06-10 22:25:37

2023-05-17 08:00:00

ChatGPT人工智能

2024-03-21 17:29:45

2020-08-01 16:40:09

代码语言Python

2018-04-03 12:26:14

2011-09-20 11:06:26

网易邮箱极速4.0

2011-12-24 22:08:51

Siri

2018-03-16 15:16:46

2018-08-17 05:09:29

电脑软件清理电脑

2010-07-06 10:53:41

RationalJazz测试管理

2019-06-04 10:40:07

点赞
收藏

51CTO技术栈公众号