Ten days ago I registered a study asking whether new code gets to choose where it goes, or is pulled into the place that already holds its data. The next day I took the registration back down. Working through the practice data showed I hadn't yet found what was doing the pulling. This post covers what changed, what the study now predicts, and how I've made sure every number in it comes from a script rather than from me.
Why the registration came down
A study registered before its data only means something if the plan is finished when it's registered. Mine wasn't. On the practice data, one chain per framework set aside from the analysis, the measures kept saying the same thing: code wasn't drawn to big methods or to complicated ones. It was drawn back to a small number of values that were private to one class.
So I removed the tag before any of the real runs had been read, and kept working on the practice chain. The scripts still refuse to read anything else until the tag is set again. Every change since is listed in the plan, dated, with what I'd seen when I made it. There are 112 numbered revisions now. That's a lot, and it's all public.
The force is encapsulation
Here's the new definition. A change is forced when its code uses a member no other class can reach: a private field, a private constant or a private method. That code can only live inside that class. Nothing about size or shape comes into it.
@RestController
class OwnerController {
private final ClinicService clinicService; // private: only this class can use it
private static final int MAX_PETS = 5; // a private constant forces too
ResponseEntity<?> addPet(int ownerId, Pet pet) {
Owner owner = clinicService.findOwnerById(ownerId);
if (owner.getPets().size() >= MAX_PETS) { // the new rule needs both
return ResponseEntity.badRequest().build();
}
...
}
}
The new rule needs clinicService and MAX_PETS. Both are private to OwnerController, so the rule has to go there. The next rule that needs them has to go there too. That's gravity: the class keeps growing because it's the only place the data is.
Doesn't dependency injection fix this? Spring injects clinicService, so any other class could declare it. That's true, and the study counts it separately as the lever injection gives you. But the agent hardly ever pulls that lever. It's always easier to add to the class that already has the field. A choice that's never taken isn't much of a choice, so a private injected field counts as forcing too.
In OfficeFloor, the rule is a function that's handed what it needs:
public class CheckPetLimit {
public void service(@Val Owner owner, PetLimits limits) throws TooManyPetsException {
if (owner.getPets().size() >= limits.maxPets()) {
throw new TooManyPetsException();
}
}
}
Nothing it uses is private to someone else, so nothing forces it into an existing class.
Essence and accident
The other change came from asking what coupling really is. Here's my view, and it's what the coupling prediction is built on.
The blocks of code are the essential complexity. They're the statements that actually solve the problem. Method calls are mostly accidental complexity: they exist to get those blocks run in the right order and to get data to them. A small program is one block with just enough method wrapping to call it. As it grows, the method structure grows around the blocks, and every call ties one block to another.
Function injection does away with much of that. Each function is handed its data, and the wiring says which function runs next. So I measure a program by its volume (the blocks of logic) and its surface (the links between them):
- a method call weighs 1, plus 1 for each parameter, plus 1 if it returns a value;
- a method using a field holding state, or a constant, weighs 1;
- a wiring edge between two functions weighs 1, plus 1 if it carries a value.
Injection isn't surface in either framework. An injected value isn't passed along the control flow. A call made on an injected object still counts, though: injection saves you finding the object, not calling it.
What the study now predicts
The hypotheses are numbered in the order of the story, so the numbers have changed since the last post. Every main test compares OfficeFloor with Spring directly.
- H1, the force. OfficeFloor's code is forced into an existing class less often than Spring's, and the gap widens as the system grows.
- H2, a core draws code in. Later code keeps coming back for the few values earlier code was bound to, not for everything else lying in the class. And it comes back less in OfficeFloor.
- H3, the encapsulation fills. Each class gathers more and more data that new code has to sit beside, needed or not.
- H4, function injection forms no core. OfficeFloor's later changes don't keep returning to the same busy places.
- H5, surface against volume. For the same amount of logic, OfficeFloor adds less link weight, and the gap widens over time.
- H6 and H7, the literature's two ways of looking. Code shape (long, complex) and co-change counts are both predicted not to see the force. Shape follows where code was forced; it doesn't cause it.
- H8, Run D. The same Spring stack with the agent told to make everything public. If encapsulation is the force, code should spread out.
H1, H2, H4 and H5 are the main tests, corrected together (Holm). H3, H6 and H7 are secondary. H8 is decided on its own run.
What the practice chain shows
This is one chain per framework, so it decides nothing. It's what I'd seen before writing the rules, and the plan records it:
- The force: OfficeFloor was forced into a class 0.15 times per change, against Spring's 0.44, a ratio of 0.25 [0.09, 0.65]. Whether the gap widens can't be read from one chain.
- The core: a value earlier code was bound to was come back to about a tenth as much in OfficeFloor, 0.09 [0.02, 0.32]. Later tangling where bound code was placed was 0.16 of Spring's at the class and 0.11 at the method.
- The surface: no difference yet, and the trend goes the wrong way. For the same logic, OfficeFloor added about as much link weight as Spring, 1.05 [0.91, 1.21], and its share grew over the stream (1.70). The reason is visible. As the stream went on, the agent wrote OfficeFloor helper methods that call each other, rising from almost none to two calls per change. It drifted back into a method structure.
That last result is the one I'd most like to be different, so it's the one I'm most careful to report. If the real runs show the same drift, H5 fails, and the honest conclusion is narrower: function injection removes the accidental complexity only while the code is kept to it.
Things I took out, and why
- "Choice narrows." H1 used to test whether the number of other places code could have gone shrinks over time. On the open-source histories, every placement was forced from the very first commit (638 of 638), so there was nothing to shrink from. Under the old rule, the strongest case of the force would have left H1 saying nothing. H1 now counts how often there's no choice at all, and the shrinking is reported alongside.
- The cost to fix a concentration. I briefly had a ninth hypothesis: how many links you'd have to cut to split a class in two. But it only looked inside the class. The real cost of rewriting a handler is outside it: its callers, its tests, and the private members a split has to open up. So I deleted it.
- Constants were left out of the surface in my first version. That was my choice, not a decision anyone made. A constant ties the code that uses it to the class that holds it, just as it forces code there, so it now counts. On the practice chain it moved the surface ratio from 1.06 to 1.05.
Guarding against myself
I created OfficeFloor, and I'd like it to come out well. So:
- Every main test is OfficeFloor against Spring, on the same agent and the same change requests in the same order.
- The weights were set so OfficeFloor can't win by construction. A wiring edge that carries a value weighs more, just as a call's parameters do.
- Results that go against OfficeFloor are recorded, like the surface trend above, and the plan names exactly what would count against each hypothesis.
- Nothing is read before the tag. Until
plan-v1is set, the scripts read only the practice chain. After it, any change is a dated, public amendment.
What's next
The plan is finished and so is the tooling. The tag waits on one thing: the ids of Run B, which swaps the front ends between the two backends so that a framework's effect can be told apart from its front end's. Then the tag goes up, and the real runs are read in order: OfficeHQ (the main test), petclinic as a replication, the open-source histories, and Run D last. The full plan is PLAN.md in placement-gravity-study. The results, including every place OfficeFloor does worse, will be in the next post.