Home  >  Article  >  Technology peripherals  >  ChatGPT’s five major features can help you improve code quality

ChatGPT’s five major features can help you improve code quality

WBOY
WBOYforward
2023-04-14 14:58:151372browse

ChatGPT is currently revolutionizing the way code is developed, however, most software developers and data experts are still not using ChatGPT to improve and simplify their work.

That’s why I’m here to outline 5 different features to improve the speed and quality of our daily work.

We can use them in our daily work. Now, let’s find out together.

Note: Never use key codes or information in ChatGPT.

01.A framework for generating project code

ChatGPT is my secret weapon when building a new project from scratch. With just a few prompts, it generated the code skeleton I needed, including the technology, framework, and version of my choice. Not only does it save me at least an hour or so of work, but it also helps keep my documents (or the team's documents) organized and up to date. It's like adding a plug-in to the team. Hands!

For example, let's say I want to use some external API, I can ask ChatGPT to draft an initial code structure for me.

Use Python to generate boilerplate code for integrating external APIs.

ChatGPT Immediate Answer:

ChatGPT’s five major features can help you improve code quality

#Even, I can request a framework to scrape LinkedIn using Selenium and ChatGPT will give me the following output.

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[@]").text
location = driver.find_element_by_xpath("//span[@]").text

print(name)
print(location)

# close the browser
driver.quit()

Easy, right? Try challenging ChatGPT with any project you can imagine.

02. Research and Compare

Making a decision on how to implement something can be difficult, especially when there are multiple options to choose from. My preferred approach is to create a basic proof of concept for each method and then compare them. However, with the help of ChatGPT, the process becomes much simpler.

I can now directly ask its expert opinion on which option or library is best for my code development. This saves me time and energy in the decision-making process and ensures I'm using the best tool for the job.

Suppose I want to work with geospatial data, but I'm not sure if I should use Geopandas or Plotly. I can ask ChatGPT to do the comparison for me - include a type; it will instantly answer the main differences between the two libraries.

ChatGPT’s five major features can help you improve code quality

Screenshot source Chat with ChatGPT who explained to me the difference between geopandas and plotly

If I want to scrape a website now, I You can ask what the best library is. ChatGPT answers using the most popular web scraping library in Python.

ChatGPT’s five major features can help you improve code quality

Screenshot Source Chat with ChatGPT, ChatGPT explains the most popular crawling sites

You can even ask the best for the site you want to crawl What are the options – though ChatGPT will most likely warn you that this will violate the site’s content policy – ​​so be careful.

What are the best options for scraping social networks?

ChatGPT’s five major features can help you improve code quality

#03. Understand the code

We’ve all been there, struggling to understand Not the codebase we created. Navigating complex and poorly organized code (also known as spaghetti code) can be a frustrating and time-consuming task.

But, with ChatGPT, understanding the new code base becomes much easier. I can now simply ask it to explain what the code does and understand it instantly. No more wasting valuable time and energy trying to decipher poor code.

Let's say I'm trying to scrape Linkedin and I find a random code on the internet that is supposed to scroll down the Linkedin job opportunities website.

What does the following code do?

#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 will immediately explain the above code in great detail and you can even ask for more details in the given section of the code - just Just play with ChatGPT for a little bit and you will find that it can do a lot of wonders.

ChatGPT’s five major features can help you improve code quality

Screenshot ChatGPT chat, ChatGPT explained the code that I didn’t understand

04. Add comments to our code

ChatGPT can also help us improve the quality and maintainability of our own code. By adding comments line by line as you request, we can ensure that our code is properly documented before release. This helps us fulfill our requirement to add comments to our code and makes it easier for others to understand and use our code.

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’s five major features can help you improve code quality

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

总结

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

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

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

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

The above is the detailed content of ChatGPT’s five major features can help you improve code quality. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete