Skip to Content
先整理?
book

先整理?

by Kent Beck
May 2025
Intermediate to advanced
124 pages
1h 7m
Chinese
O'Reilly Media, Inc.
Content preview from 先整理?

第 6 章 凝聚秩序 凝聚秩序

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

你读了代码,发现要改变一个行为,就必须修改代码中分散的几个地方,于是你变得脾气暴躁。你该怎么办?

重新排列代码顺序,使需要更改的元素相邻。内聚顺序适用于文件中的例程:如果两个例程是耦合的,则将它们放在旁边。它也适用于目录中的文件:如果两个文件是耦合的,就把它们放在同一个目录中。它甚至还适用于不同的版本库:在修改耦合代码之前,将它们放在同一个版本库中。

为什么不直接消除耦合?如果你知道怎么做,那就 Go 吧。这是最好的整理,假设:

成本(脱钩) + 成本(变化) < 成本(耦合) + 成本(变化)

但由于种种原因,这可能并不可行:

  • 解耦可能是一种智力挑战(你不知道如何去做)。

  • 脱钩可能会耗费大量的时间和金钱(你可以做到,但现在花不起这个时间)。

  • 脱钩可能是一种关系拉伸(团队目前已经承受了它所能承受的最大变化)。

你不会被瑞士奶酪式的改变所束缚。Tidy 可以增加内聚力,使行为变化变得更容易。有时,内聚力稍有提高,就会使阻碍解耦的因素得以释放。有时,更好的内聚性能让你与耦合共存。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

超越Vibe编程

超越Vibe编程

Addy Osmani
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉

Publisher Resources

ISBN: 9798341656949