Rhys Brett-Bowen Coding in the age of AI

Frontend Code Has Two Shapes

13 min

Confidence one change of my own, React's docs, and a guess about agents I haven't tested

“That’s because UI and data models often have the same information architecture—that is, the same shape.” — Thinking in React, react.dev

A page drawn as three stacked regions. A button sits in the second region; a dashed outline in the top region marks where it was. To the right, a tree of data nodes; dotted lines run from both the old and the new spot to the same leaf, and the line from the new spot has to take a longer way round.

In 2021 I moved a button.

It was the button that creates a map from a data sheet, and it lived in the sheet’s toolbar. The design changed: when the app bar was switched on, the button had to sit in the style header instead, a strip lower down the same page. Same button, same click handler, same rule for when it’s disabled. A different spot on the screen.

The change touched 8 files. Six were the move itself. The button had been taking seven props from the toolbar, and the style header had none of them. So four moved into the button, read from the store, and three got threaded down from the sheet application through a component that had never needed them. The other two files deleted 39 lines that turned out to be a second copy of the button, with its own copy of the disabled rule, difference(requiredTraits, columnTraits), written in a different shape so the app bar could render it.

Nothing about what the button did changed. Only where it sat. Backend code moves too, but almost never because of where something sits on a screen.

Bottom line up front

  • Backend code mostly answers to its API. Frontend code answers to the API and the layout. The layout moves for reasons the logic doesn’t care about. A designer moves a button and the code has to follow.
  • The component tree usually follows the layout, so moving something on screen usually gives it a new parent. Every prop it took from the old parent is now a question. That’s where the coupling you couldn’t see shows up.
  • Build components that can move. Props say which thing it is and where it sits. The component looks up the rest. You pay for that in visibility and testability, and it’s worth it where the layout moves more often than the data does.
  • Try composition first. If the layers in between only relay, pass the component down as children instead.
  • Test it by asking for the move. “Put this on the other side of the page.” Count what changes. That’s the check for whether code is future-proof, and it works on an agent’s code as well as yours.
  • Frontend never settled on one answer. I think part of the reason is who it attracted, and that unsettled code is what an agent learns from. Whether it shows in what the agent writes is a guess I haven’t tested.

Two shapes

React’s own guide starts from the happy case. Your data has a shape, your UI has a shape, and “often” they’re the same, so you split the screen into components that match the data. It works until the design moves something.

Then the two shapes come apart. The data didn’t change. The layout did. And the component tree mostly follows the layout, because a toolbar’s buttons are the toolbar’s children. Move the button to the header and it gets a new parent. Whatever it took from the old one has to come from somewhere else. React calls the usual fix lifting state up, moving it “to their closest common parent”, and says it’s “one of the most common things you will do writing React code.” When the common parent is far away, the same docs name what you get: “prop drilling.”

The tree doesn’t have to follow the layout. A portal lets a component render “into a different place in the DOM” while it keeps its parent: “A portal only changes the physical placement of the DOM node.” That’s the right tool for a tooltip or a modal, something that floats above the page but belongs to the thing that opened it. My button belonged in the header, beside the header’s other controls, and I moved it into the header’s component tree.

My button’s toolbar version took rowCount, artifactsState, columnTraits and sheetInfo because the toolbar happened to have them. All four were about the button, not where it sat. The toolbar was only fetching them on the button’s behalf. They came through the layout because that’s where props come from. The move didn’t create that coupling. It showed it.

Compare a backend change of the same size. Move the code that calls an endpoint from one module to another and the endpoint doesn’t notice. The API is the boundary, and callers adapt to it. Backend has its own version of the problem: move work across a transaction or a service boundary and a lot gets rewired. But that happens when the system’s boundaries change, not because someone wanted a button on the left. So frontend code is built against a structure that moves.

Make it movable

What made the button movable was a small change: it stopped asking its parent for what it needed and read it from the store itself. After that the style header passed three things: the sheet’s id, its config and a dispatcher. Which sheet, and how to act on it. The button could sit anywhere those existed.

That’s the rule I’d take from it. Props say which thing it is and where it sits. The component looks up the rest. Where it sits covers size, alignment, which way a popover opens. My button still hard-codes its popover to open downwards, which is exactly the kind of thing the spot should decide. rowCount is exactly the kind of thing it shouldn’t.

That isn’t new as a pattern. Dan Abramov’s 2015 split separated data access from presentation: containers supplied data and callbacks, and presentational components received them through props. That’s close to the opposite of my rule. In 2019 he added a note: “I don’t suggest splitting your components like this anymore.” Hooks made “read it where you use it” ordinary. What I’d add is the reason to choose it per component: how often its spot moves.

The strongest objection comes from the backend side. A component that reaches into a store for its data is a service locator, and backend learned to prefer dependency injection: pass everything in so the dependencies are visible. React says the same about context: “Just because you need to pass some props several levels deep doesn’t mean you should put that information into context.” Passing props “makes it very clear which components use which data.”

That’s true, and it costs more than visibility. The toolbar version was explicit and easy to test. My button can’t render in a test without a store behind it now. I’d still make the trade, for one reason: how often the call site moves. A backend service’s callers change when the architecture does. A button’s parent changes when the design does, and in my experience the design changes far more often than the architecture. Explicit inputs pay off where the caller is stable. Where the caller is the thing that moves, every explicit input is a wire to re-run.

There’s a third option, and React suggests it first: pass JSX as children, so the middle layers don’t relay data they don’t use. If the problem is a component passing props it doesn’t need, compose first. If the problem is a component that will sit under different parents, let it look up what it owns.

The second copy of the button is the same problem one step further. The app bar wanted the button in a different shape, a config object rather than a component, so someone wrote it again. Two copies of one disabled rule, in two layouts, waiting to drift.

The check: ask for the move

Reading code tells you little about whether it’s future-proof. Every component looks fine sitting where it is. The move shows it.

So that’s the test. Take the component, an agent’s or yours, and ask for it on the other side of the page. Then look at the diff:

  • Files touched. A rough line from my own changes: one or two is a move, six is a rewire.
  • Props added to components that don’t use them. Each one is a relay. That’s the drilling.
  • A second copy. If the new spot needs the logic in a different shape, the logic was tied to the old spot.

It’s cheap, and it’s an output you can judge without reading every line. You don’t have to rule on whether the code is good. You count what one layout change cost, and the diff does the counting.

Why nobody settled it

Backend inherited a long tradition of drawing boundaries, from Parnas on information hiding in 1972 onwards, and plenty of people trained in it. My read is that frontend got much less of either.

It didn’t get many traditionally trained software engineers. It got people like me: I came in back in 2012, writing scripts for online advertising. At university it was barely taught beyond JavaScript scripts. Even the ACM’s 2013 model curriculum kept web platforms out of the core, along with every other particular platform, “without including any such platform in the core curriculum.”

The people who did shape it often came from Ruby rather than from enterprise Java. Prototype was built as part of Rails’s Ajax support. Yehuda Katz sat on the Rails and jQuery core teams before co-creating Ember. Alex MacCaw, a self-described JavaScript and Ruby developer, wrote Spine in CoffeeScript, and Rails shipped CoffeeScript by default. Not everyone: jQuery, Dojo and YUI came from elsewhere.

Ruby, in Matz’s words, “inherited the Perl philosophy of having more than one way to do the same thing.” The Rails doctrine says to “optimize for programmer happiness.” My guess is that people who pick a language for that are choosing freedom over ceremony. So my read of who frontend was getting: the developers who wanted out of old-school Java, not the ones Java had trained. Rails itself is opinionated (“convention over configuration”), so the claim is about who chose Ruby’s world over Java’s, not that Rails people brought mess. A handful of framework authors isn’t a census, and I’m not claiming one. It’s the pattern I saw from inside.

Put that crowd on a structure that moves, and you get what frontend has: props, context, stores and hooks, all in use, often in the same file, and no settled answer to where data should come from.

What an agent learns from this

Here’s the part I can’t prove yet. An agent learns frontend from the code frontend developers wrote, and that code never agreed on where data should come from. And a request for a button almost never says “the design will move this next quarter.”

So I’d expect an agent’s first version to wire data through whatever parent is nearest, because that works and nothing in the request says otherwise. It passes every check you’d run on the screen in front of you. It fails the move.

The nearest evidence is in other languages, and it’s a correlation. The datasets used to train Python code generators are full of code smells, and so is what the generators write. In a Java study, generated code had more smells than professional reference solutions, topics that were smellier in the references were smellier in the generated code too, and the increases were largest on object-oriented topics. I haven’t found a study of the frontend case. Benchmarks that check whether a function works don’t help: Codex did as well on JavaScript as on Python in one of them. They test functions, not how components are wired. The move tests what that wiring costs to change.

Two things follow, and neither depends on my guess being right. Tell the agent the layout will move. And run the move test on what it gives you.


How sure I am:

  • From experience: the button move, one change of my own from 2021, genericised. 8 files, six for the move and two to delete a 39-line second copy; 4 props moved into the button, 3 threaded through. The file-count line in the move test is from my own changes, not measured. Coming into frontend through advertising scripts in 2012, and the university line.
  • Prior art: React’s docs on matching UI to data, lifting state up, prop drilling, portals, and when not to reach for context. Abramov on container and presentational components. ACM CS2013 on platforms. Matz and the Rails doctrine. Siddiq et al. and Ghosh Paul et al. on smells in generated code. MultiPL-E on JavaScript.
  • My take: that the component tree usually follows the layout, so layout moves expose data coupling more often in frontend than call-site moves do in backend; that “props say which thing and where; look up the rest” is the line to draw, and that how often the spot moves is what decides it; that backend got the boundary tradition and frontend mostly didn’t; that who frontend attracted is part of why it never settled. The parts are backed. The population step is mine, from a handful of names and from being there.
  • A guess, untested: that agents write less movable frontend code than backend code. The test: the same feature in TypeScript, once as frontend and once as backend, so the language is held fixed. Then one move each: the frontend component to a different parent region, the backend call site to a different module. Score both on files touched and inputs threaded through code that doesn’t use them. Inputs threaded is the direct measure of nearest-parent wiring. Prediction: the frontend move costs more on both. Falsifier: the frontend move costs the same or less on either measure.
  • What didn’t survive: “frontend code is worse because of JavaScript.” The best-known study linking languages to defect rates mostly failed to reproduce (Berger et al.), and what survived was tiny. That doesn’t prove the language doesn’t matter, but it’s no case for blaming it.

Sources

  • React, Thinking in React; Sharing State Between Components; Passing Data Deeply with Context, including “Before you use context”; createPortal.
  • Dan Abramov, Presentational and Container Components, 2015, with the 2019 update.
  • ACM/IEEE-CS, Computer Science Curricula 2013, Platform-Based Development knowledge area — elective; “without including any such platform in the core curriculum.”
  • Prototype JavaScript Framework (Sam Stephenson, 2005, part of Rails’s Ajax support); Yehuda Katz, About (Rails, jQuery and Ember core); CoffeeScript shipped in Rails 3.1 (2011); Alex MacCaw, JavaScript Web Applications (O’Reilly, 2011), and Spine, written in CoffeeScript.
  • David L. Parnas, On the Criteria to Be Used in Decomposing Systems into Modules, CACM 15(12), 1972.
  • Bill Venners, The Philosophy of Ruby: A Conversation with Yukihiro Matsumoto, Artima, 2003.
  • The Rails Doctrine, rubyonrails.org/doctrine — “Optimize for programmer happiness”; “Convention over Configuration”.
  • Mohammed Latif Siddiq, Shafayat H. Majumder, Maisha R. Mim, Sourov Jajodia and Joanna C. S. Santos, An Empirical Study of Code Smells in Transformer-based Code Generation Techniques, SCAM 2022.
  • Debalina Ghosh Paul, Hong Zhu and Ian Bayley, Investigating The Smells of LLM Generated Code, arXiv:2510.03029.
  • Federico Cassano et al., MultiPL-E: A Scalable and Polyglot Approach to Benchmarking Neural Code Generation, IEEE TSE 2023 — Codex on JavaScript matched Python; other models varied.
  • Emery D. Berger, Celeste Hollenbeck, Petr Maj, Olga Vitek and Jan Vitek, On the Impact of Programming Languages on Code Quality: A Reproduction Study, TOPLAS 2019 (arXiv:1901.10220).

Related: You Can’t Save a Layout You Don’t Own (the layout as a model you own, at the window level); You Can’t Judge What You Can’t See (build the output you can judge; the move test is one).