用什么代码可以改变键盘
by John Cobb
约翰·科布(John Cobb)
为什么我改变了对代码质量的看法 (Why I changed the way I think about Code Quality)
What do you think about when you think about code quality?
当您考虑代码质量时,您会怎么看?
Is it consistency? Enforcing a set of standards and best practices on your code through linter rules and formatters? How about ensuring your code has tests that run automatically during your build process? What about pull requests and code reviews — protecting your master branch from direct commits and having peers review your code?
是否一致? 通过linter规则和格式化程序对代码执行一套标准和最佳实践? 如何确保您的代码具有在构建过程中自动运行的测试? 拉取请求和代码审查又如何—保护您的master分支免受直接提交并让同级审查您的代码?
They’re some of the things that come to mind for me. Automated processes and manual checks. Smart and efficient. Yet, while they’re all useful, they really only address half of the problem.
这些是我想到的一些东西。 自动化流程和手动检查。 聪明高效。 然而,尽管它们都很有用,但实际上只能解决一半的问题。
我们不能使一切自动化 (We can’t automate everything)
Automation is crucial for maintaining code quality. Static analysis of your syntax with a linter and automated testing should be mandatory. But I can write code that passes all the automated processes without any guarantee to its actual quality.
自动化对于保持代码质量至关重要。 必须使用lint对语法进行静态分析并进行自动测试。 但是我可以编写通过所有自动化过程的代码,而不能保证其实际质量。
Does the code follow established patterns? Does it use existing modules, or does it duplicate code? Is everything named sensibly? Is the code in the right place in the codebase? Will this change have wider, unintended, implications? Does this code actually address/solve what it intends to? Does it even work?
代码是否遵循既定模式? 它使用现有模块还是重复代码? 一切都被合理地命名了吗? 代码是否在代码库中的正确位置? 这种变化会产生更广泛的,意想不到的影响吗? 此代码是否实际解决/解决了预期的问题? 它甚至工作 ?
Automated processes can’t answer those questions for you (yet). If you (or another human being) aren’t asking these questions of your code, then you’re probably not shipping quality code. That’s why we have code reviews.
自动化流程无法为您回答这些问题(至今)。 如果您(或其他人)没有问代码的这些问题,那么您可能没有在交付质量代码。 这就是为什么我们要进行代码审查。
良好的代码审查不仅仅应包含代码 (A good code review should be about more than the code)
Of course a code review should be about the code (it’s right there in the name after all). But it should also be about the broader questions posed above, and also about the end product.
当然,代码审查应该是关于代码的(毕竟它就在名称中)。 但这还应与上面提出的更广泛的问题以及最终产品有关。
I’ve noticed a tendency for developers to treat code reviews as perfunctory. A rudimentary check of the modified code. A comment on any obvious mistakes (or just picking a nit or two to look busy).
我注意到开发人员倾向于将代码审查视作敷衍。 对修改后的代码的初步检查。 对任何明显的错误发表评论(或者只是挑一两个尼特,让他们看起来很忙)。
Five minutes, job done. Looks good to me.
五分钟,工作完成。 对我来说看起来不错 。
But, code that doesn’t address the requirements of the task is not quality code. Code that produces console errors or visual bugs in the device/browser is not quality code. Neither of those things can be picked up in a perfunctory code review. You can’t adequately review code unless you actually run it.
但是,不能满足任务要求的代码不是质量代码。 在设备/浏览器中产生控制台错误或视觉错误的代码不是质量代码。 这些功能都无法在敷衍的代码审查中找到。 除非您实际运行代码,否则您将无法充分审查代码。
I propose that a good code review should involve at minimum:
我建议良好的代码审查至少应涉及:
- Pulling down the branch to a local environment. 将分支拉到本地环境。
- Building the project (and checking that the linter and tests all pass). 构建项目(并检查短毛绒和测试是否全部通过)。
- Checking that the code runs error free in the target browsers/devices. 检查代码是否在目标浏览器/设备中无错误运行。
- Checking that the completed work matches the requirements of the task. 检查完成的工作是否符合任务要求。
If there are any issues with any of those steps the pull request should be rejected. Do not pass Go. Do not collect $200. Do not merge to master.
如果这些步骤中的任何步骤有任何问题,则应拒绝请求请求。 不要通过围棋。 不要收取200美元。 不要合并到母版。
Reviewers should also use the code review as an opportunity to ask questions. If you don’t understand the code, then you shouldn’t approve the pull request. Don’t assume that the author knows more than you do — if it doesn’t make sense to you, ask for clarification.
审阅者还应将代码审阅作为提问的机会。 如果您不理解代码,则不应批准请求请求。 不要以为作者比你了解更多—如果这对你没有意义,请澄清。
The reviewer has equal responsibility with the author for the quality of the code. This is a mindset that is essential for maintaining code quality.
审阅者与作者对代码质量负有同等责任。 这是保持代码质量必不可少的一种心态。
Comprehensive code reviews go a long way to helping ensure code quality. But there are steps you can take before you even open a pull request. Small things you can do that will help enhance the quality of your code, and reduce the effort required to review it.
全面的代码审查对确保代码质量大有帮助。 但是,您甚至可以在打开拉取请求之前执行一些步骤。 您可以做的小事情将有助于提高代码的质量,并减少检查代码所需的工作。
仔细检查您自己的作品是否完整 (Double check your own work for completeness)
I have an annoying habit. When I finish writing the last lines of code for a task, I mentally check the task off as complete.
我有个烦人的习惯。 当我完成任务的最后几行代码编写时,我会在脑海中检查任务是否完成 。
If I were to listen to that impatient voice in my head, I’d submit my pull request right then. But that code would likely contain many, or all, of the following:
如果我想听听我脑子里那急躁的声音,那我马上就要提交我的请求。 但是该代码可能包含许多或全部以下内容:
- Missed requirements. 错过的要求。
- Missing test cases. 缺少测试用例。
- Superfluous, unused or draft code. 多余的,未使用的或草稿的代码。
- Not enough code comments. 代码注释不足。
- Visual bugs in some browsers/devices. 某些浏览器/设备中的视觉错误。
If any of those things are true about your code, then your code is not complete. If any of those things end up in the master branch, then you have degraded the quality of the codebase.
如果您的代码中有任何上述情况,则您的代码不完整。 如果这些事情中的任何一个最终出现在master分支中,则说明代码库的质量下降了。
The main point here is this: code quality starts with the code author. You shouldn’t rely on automated tasks, a code review, quality assurance or user acceptance testing to catch your mistakes.
这里的重点是:代码质量始于代码作者。 您不应该依靠自动化任务,代码审查,质量保证或用户验收测试来发现错误。
Double checking work for completeness is an essential first step toward code quality. It’s the easiest step to take, but also the easiest one to ignore.
进行双重检查以确保完整性是迈向代码质量必不可少的第一步。 这是最简单的步骤,也是最容易忽略的步骤。
You should only open up a pull request when you are certain your code is complete.
仅在确定代码完成后才应打开拉取请求。
对分支机构进行自我审查 (Perform a self review of your branch)
I’m always surprised at how many issues — or opportunities to refine a solution — I can find in my own code. Issues and opportunities that only become visible to me when I step back and view my changes in isolation.
我总是在自己的代码中发现有多少问题或改进解决方案的机会,总是感到惊讶。 当我退后一步,孤立地查看我的更改时,问题和机会才对我可见。
You can review your work and apply your own feedback before assigning a team member to review your work. You can also use this opportunity to leave comments on the pull request to clarify anything for the reviewer.
您可以在分配团队成员来审查您的工作之前,先审查您的工作并应用您自己的反馈。 您还可以利用此机会在请求请求中留下评论,以使审阅者澄清任何事情。
Taking time to ensure your work is complete, to correct obvious mistakes, or assess your solution, will enhance the quality of your code. It also reduces the effort required to review it.
花一些时间来确保您的工作完成,纠正明显的错误或评估您的解决方案,将会提高代码的质量。 它还减少了审核所需的工作。
It might also save you some embarrassment. I know it has for me.
这也可能使您免于尴尬。 我知道它适合我。
确保代码质量应该是每个开发任务的内在要求 (Ensuring code quality should be an inherent requirement of every development task)
You may be thinking that this approach adds time to the length of a task. And you’re right, it does. But that isn’t a bad thing.
您可能会认为这种方法会增加任务时间。 你是对的,的确如此。 但这不是一件坏事。
Efficiency is important, but laziness and apathy is harmful. Apathy leads to a bloated, inconsistent code base. Laziness creates a growing backlog of bad technical debt. We can’t be passive and maintain code quality. It requires time and effort.
效率固然重要,但懒惰和冷漠是有害的。 冷漠会导致a肿的,不一致的代码库。 懒惰导致不良技术债务积压的情况越来越多。 我们不能被动地保持代码质量。 这需要时间和精力。
Changing the culture around code quality can be hard. Project managers and product owners generally aren’t concerned about code quality — they have their own concerns. Requesting extra time for code quality processes can sometime fall on deaf ears. However, maintaining code quality shouldn’t be thought of as something extra — it should be an inherent requirement of every task.
改变围绕代码质量的文化可能很难。 项目经理和产品所有者通常不关心代码质量-他们有自己的问题。 为代码质量过程请求额外的时间有时会置若de闻。 但是,不应将保持代码质量视为额外的工作—它应该是每个任务的固有要求。
As developers, if we don’t change the way we think about code quality, we can’t expect anyone else to.
作为开发人员,如果我们不改变我们对代码质量的方式,我们不能指望别人。
Nothing I’ve talked about here is particularly groundbreaking, nor is it prescriptive. Not every team, workplace, or project is the same, and some of the above may not be applicable to you.
我在这里谈论的没有什么特别开创性的,也不是规定性的。 并非每个团队,工作场所或项目都是相同的,并且上述某些内容可能不适用于您。
I do believe there is often a gap between the way developers think about code quality, and the actual actions taken to address it. If you’ve found that too, then hopefully there is something you can take away from this article — or perhaps you’ve already taken a different approach to addressing it. I’d love to hear your suggestions in the comments.
我确实相信开发人员对代码质量的思考方式与为解决该问题而采取的实际措施之间通常存在差距。 如果您也发现了这一点,那么希望本文可以对您有所帮助–也许您已经采用了另一种方法来解决它。 我很想听听您在评论中的建议。
Thanks for reading!
谢谢阅读!
翻译自: https://www.freecodecamp.org/news/why-i-changed-the-way-i-think-about-code-quality-88c5d8d57e68/
用什么代码可以改变键盘