统一与分离差异
Unified versus Split Diff

原始链接: https://matklad.github.io/2023/10/23/unified-vs-split-diff.html

在软件开发中,代码审查期间通常使用两种类型的 diff 格式 - “分割”​​diff 与“统一”diff。 虽然分割差异适用于较小的更改,但对于较大且更复杂的更新,这两种格式都不适合。 相反,高效的代码审查工作流程需要使用脚本在本地签出拉取请求,然后擦除其提交,同时保留更改。 这允许更仔细地检查更新的代码,类似于直接编辑代码。 然而,Magit 状态缓冲区和打开的编辑器窗口之间仍然缺乏自动同步,需要手动干预。 代码审查的主要目标不仅仅是评估变更; 请参阅另一篇有关两种代码审查的相关文章。 总体而言,尽管工具有助于简化工作流程,但与实际编辑相比,它们仍然存在不足。

持续集成测试可确保在管道中尽早发现损坏的功能,最好作为预提交测试。 此外,在整个开发周期中实施结对编程或其他协作技术可以帮助防止出现更大的拉取请求。 重构和优化也应该在整个开发过程中频繁发生,而不是推迟到必要时,因为这可以降低引入主要架构缺陷的风险。 这些技术可确保代码随着时间的推移保持干净和可维护,从而减少后续大规模重构或重大更新的需要。 最终,不惜一切代价避免大差异只是整体最佳开发和协作实践的一个症状。
相关文章

原文

Which is better for code reviews, a unified diff or a split diff?

A split diff looks like this for me:

And this is a unified one:

If the changes are simple and small, both views are good. But for larger, more complex changes neither works for me.

For a large change, I dont want to do a diff review, I want to do a proper code review of a codebase at a particular instant in time, paying specific attention to the recently changed areas, but mostly just doing general review, as if I am writing the code. I need to run tests, use goto definition and other editor navigation features, apply local changes to check if some things could have been written differently, look at the wider context to notice things that should have been changed, and in general notice anything that might be not quite right with the codebase, irrespective of the historical path to the current state of the code.

So, for me, the ideal diff view would look rather like this:

On the left, the current state of the code (which is also the on-disk state), with changes subtly highlighted in the margins. On the right, the unified diff for the portion of the codebase currently visible on the left.

Sadly, this format of review isnt well supported by the tools everyone seems to be happy reviewing diffs, rather than the actual code?

I have a low-tech and pretty inefficient workflow for this style of review. A gpr script for checking out a pull request locally:

$ gpr 1234 --review

Internally, it does roughly

$ git fetch upstream refs/pull/1234/head
$ git switch --detach FETCH_HEAD
$ git reset $(git merge-base HEAD main)

The last line is the key it erases all the commits from the pull request, but keeps all of the changes. This lets me abuse my workflow for staging&committing to do a code review edamagit shows the list of changed files, I get go to next/previous change shortcuts in the editor, I can even use the staging area to mark hunks I have reviewed.

The only thing I dont get is automatic synchronization between magit status buffer, and the file thats currently open in the editor. That is, to view the current file and the diff on the side, I have to manually open the diff and scroll it to the point I am currently looking at.

I wish it was easier to get this close to the code without building custom ad-hoc tools!

P.S. This post talks about how to review code, but reviewing the code is not necessary the primary goal of code review. See this related post: Two Kinds of Code Review.

联系我们 contact @ memedata.com