少点错误 1小时前
Example of Splitting a PR
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何通过拆分提交来改进代码审查和验证过程。作者分享了在进行代码重构和添加新功能时,将大型改动分解为更小、更集中的PR的经验。通过先进行重构和添加测试,然后在新功能PR中引用重构,可以更容易地验证代码的正确性,提高代码质量和审查效率。这种方法利用版本控制工具,优化了代码提交的顺序,从而增强了代码的可维护性和可读性。

🧐作者强调了编写小型、专注的PR的重要性,认为这有助于更容易地判断代码是否正确。例如,当需要同时进行重构和添加新功能时,应该将它们拆分成两个独立的PR。

💡作者分享了将大型改动分解为更小、更易于验证的PR的具体方法。首先,在新的分支上提交所有更改,然后创建另一个分支进行重构和添加测试。在重构分支上删除与新功能相关的内容,并验证输出未修改。之后,通过交互式变基将重构提交合并,并作为第一个PR提交。

✨作者解释了拆分提交的好处。通过这种方法,可以确保重构没有破坏任何东西,并且使得代码审查更容易。提交的顺序经过优化,更易于验证,提高了代码的质量和可维护性。

Published on May 26, 2025 2:20 AM GMT

I'm a big fan of small focused PRs: it's much easier to tell whetherthey're actually doing the right thing. For example, I recently raninto a common scenario where I started adding some functionality, andrealized this required some refactoring. The refactoring also made itclear that the original code was missing some important test coverage,so I added some tests. I ended up with a big set of changes whichcombined large refactors and a major (intended) change to the output.

This is a bad combination: if my refactoring accidentally changedfunctionality in a way not captured by the tests we might miss it.And with so many changes it's going to be hard for the reviewer (andeven the author) to keep it all in their head at once. Instead, itwould have been better if I had first done the refactoring and addedtests, and then followed up with the new functionality. The first PRwould be a bit noisy but conceptually straightforward and easy tovalidate because it produces exactly the same output for all inputs.The second PR would be very clean, but would require carefulvalidation to verify that the changes really are an improvement.

I might be tempted to say "oh well, I should have approached thisdifferently, I'll do better next time" but really I did need tocombine these two changes when writing them: I didn't know for surewhat I would want for the refactoring until I'd tried to use it toimplement the functionality change. So if my plan was to do betternext time I wouldn't actually do any better.

Instead I took advantage of version control to split my changesinto two sets, in an order that is optimized for validation:

    I committed all my changes to a new branch,jefftk-new-feature, with a message that only mentioned thefeature I was adding, not the refactoring or the new tests.

    I made a new branch off of jefftk-new-feature torepresent the refactoring and new tests: git checkout -bjefftk-refactor.

    On jefftk-refactor I deleted everything related tothe new feature, leaving only the refactoring and new tests. Iverified that the output was unmodified, as you'd expect for arefactoring-only change. I committed this, with a message that onlymentioned the refactoring and tests.

    I interactively rebased (git rebase -i HEAD~2)this branch to squash the two commits into one. I only kept therefactoring commit message. This branch is now ready to be my firstPR, and is ready to send out for review.

    I switched back to jefftk-new-feature, and rangit rebase jefftk-refactor. This rewrote my original bigcommit as if it was a follow-up to the refactoring commit. There werea few merge conflicts I needed to resolve manually, but nothing toopainful. This branch is now ready to be my second PR, and I'll uploadit to GitHub so I can reference it in the first PR in case thereviewer is interested in taking a peek at what's coming.

This was a bit more work, but I think it's definitely worth it: I'mmuch more confident that my refactoring didn't break anything, and Iexpect it to be much easier to review.

Comment via: facebook, mastodon, bluesky



Discuss

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

PR 代码审查 版本控制 重构 提交
相关文章