Stop sharing code

Sharing code is overrated and abused as the solution to code repetition. What’s the alternative? The answer is related to cohesion, code hotspots, and the law of proximity.

Luís Soares
CodeX

--

Photo by Kamesh Vedula on Unsplash

Cohesion

Cohesion refers to the degree to which the elements inside a module (e.g., a class) belong together. High cohesion means all the code in a module is related; low cohesion means the module contains unrelated pieces of code.

Cohesion

Modules with low cohesion can lead to code hotspots — like black holes of code — attracting every feature. They harm modularity because they bind multiple features together. They make your codebase ill. Since there are multiple reasons to change hotspots, they grow indefinitely and are often a source of merge conflicts. Hotspots are the traffic jams of the software world.

There are several reasons why code grows into hotspots. The most common reason is low cohesion, which means that the hotspot contains several unrelated parts and lacks modularity. Such hotspots attract many commits because they have too many responsibilities and those responsibilities…

--

--

Luís Soares
CodeX
Writer for

I write about automated testing, Lean, TDD, CI/CD, trunk-based dev., user-centric dev, domain-centric arch, ...