如何使用 GitHub Copilot 重构代码

We’ve all been there—staring at a function that looks like it was written by an over-caffeinated goblin at 3 AM (maybe even your alter ego). You could pretend it doesn’t exist, or you could refactor it. Luckily, GitHub Copilot makes the second option less painful.

我们都经历过——盯着一个看起来像是由一个过度喝咖啡的哥布林在凌晨 3 点写的函数(也许甚至是你的另一个自我)。你 可以 假装它不存在,或者你可以重构它。幸运的是,GitHub Copilot 使第二个选项变得不那么痛苦。

Let’s get to it.

让我们开始吧。

Don’t forget you can try this for free

别忘了你可以免费尝试这个

By now, you’ve likely heard that GitHub Copilot is available for free to all GitHub users, and hopefully had a chance to try it out for yourself. If not, consider giving it a spin and seeing how it could help.

到现在为止,你可能已经听说过 GitHub Copilot对所有GitHub用户免费提供,希望你有机会亲自试用一下。如果没有,考虑试试看,看看它能如何帮助你。

What is code refactoring?

什么是代码重构?

Feel free to breeze past this section if you already know what’s involved with refactoring code, but we wanted to take a moment to cover what we’ll be looking at today.

如果您已经知道重构代码涉及的内容,可以随意跳过此部分,但我们想花一点时间来介绍今天要讨论的内容。

Think of refactoring as giving your project some digital spring cleaning—a glow up for your functions, classes, and modules. But instead of just tidying up, you’re making your code more efficient, maintainable, and readable, all without changing its external behavior.

将重构视为给你的项目进行一次数字春季清理——为你的函数、类和模块进行一次焕然一新的升级。但不仅仅是整理,你还在使你的代码更高效、可维护和可读,同时不改变其外部行为。

Some standard ways of refactoring include:

一些标准的重构方式包括:

  • Simplifying complex conditionals (because no one should need a PhD to read your if statements)
  • 简化复杂的条件语句(因为没有人应该需要博士学位来阅读你的 if 语句)
  • Extracting duplicated logic (so you’re not trying to maintain code in ten different places)
  • 提取重复的逻辑(这样你就不必在十个不同的地方维护代码)
  • Improving variable and function names (because doThing() is a crime against humanity)
  • 改善变量和函数名称(因为 doThing() 是对人类的犯罪)
  • Converting monolithic functions into smaller, modular pieces (to prevent the dreaded “function that spans multiple screens” scenario)
  • 将单体函数转换为更小的模块化部分(以防止令人畏惧的“跨多个屏幕的函数”场景)

Refactoring isn’t just about tidiness—it’s about making your codebase more resilient, scalable, and e...

开通本站会员,查看完整译文。

Home - Wiki
Copyright © 2011-2025 iteam. Current version is 2.142.0. UTC+08:00, 2025-02-22 04:20
浙ICP备14020137号-1 $Map of visitor$