DEV Community

Daniel Pertu
Daniel Pertu

Posted on

37 components in our Expo app, and the one nothing has ever imported was written in the first commit

Munchable's app has no component library under it. 37 hand-written components, 3,056 lines, 83 lines each on average, and everything on screen is built out of them. I wanted to know which ones were actually load-bearing, so I counted importers: for each component, how many other files in src/ and app/ mention it at all, excluding its own file.

The top of the list is unsurprising and the bottom is the interesting part.

Text                 37
Icon                 29
Screen               17
Button               13
Header               10
Collapsible           8
DisclosureChevron     7
...
ConfidenceBadge       1
Glow                  1
SelectionCanvas       1
StillCrop             1
SummaryRow            1
VerdictHero           1
...
Stack.web             0
VerdictChip           0
Enter fullscreen mode Exit fullscreen mode

Eleven components have exactly one importer. Two have none.

One of the zeroes is the bundler doing its job

Stack.web.tsx has no importers and never will. Metro resolves platform extensions: a file importing ./Stack gets Stack.web.tsx on web and Stack.tsx everywhere else, so the web variant is reached by a name nobody types. Two files import ../src/components/Stack. Follow that import in your editor and you land in an 11-line file that re-exports the router's own stack. On the web build those same two imports resolve to 196 lines of hand-written screen transitions instead.

This is worth knowing before you run an audit like this over your own app, because it is the first way a dead-code count lies to you. Platform extensions, index resolution and anything loaded through a dynamic require all produce components that are used and look orphaned. So does the entire app/ directory: Expo Router finds its 20 route files by file convention, and not one of them is imported by anything. An honest version of this audit has to exclude all of those by hand, which is why I did not write it as a script that fails CI.

The other zero has been dead since the first commit

VerdictChip.tsx. 61 lines, two size variants, a solid pill with a colour, an icon and a label in it. It is a perfectly good component and nothing has ever rendered it.

I expected the git history to show a deletion somewhere, a screen that used to use it and got rewritten. It does not:

$ git log --oneline -S "VerdictChip" -- apps/mobile
d1dcc71 Initialize Munchable monorepo with rules engine, mobile and web apps
Enter fullscreen mode Exit fullscreen mode

One commit, the one that created the repository. -S reports commits that change the number of occurrences of a string, so a single result means no commit has ever added or removed a use of it. It was not used, then abandoned. It was written and never used, on day one, and has compiled cleanly ever since.

That is a different failure from dead code, and I think it is a more common one than we admit. The chip was how I pictured the result screen before I built the result screen. When I built it, the answer wanted to be one large thing rather than a small badge, so the screen got VerdictHero instead: a big disc in the verdict colour with the word beneath it. The chip was never deleted because deleting it was never on anybody's list. Nothing broke. It cost nothing visible.

Why the type system cannot see it

Worth being precise about, because this is the part people assume is covered.

noUnusedLocals catches an unused local. An exported symbol with no importers is not an unused local; it is a public API with no consumers, and TypeScript has no way of knowing whether that is a mistake or a library. The repo's compiler settings are strict, there is no any in the app, and none of that helps here. Finding this needs a tool that walks the import graph for exports nobody reaches, which is a different kind of tool from a type checker, and this repo does not run one.

Nor does a test suite help. A component with no importers has no test that fails and no screen that looks wrong. It is invisible to every automated thing pointed at this app, and the only reason I found it is that I was counting something else.

The real cost is the second answer

Here is why I would rather not have left it there, and it is not the 61 lines.

VerdictChip and VerdictHero do the same job: take a verdict and render it as a colour, a distinct icon and a word, never colour alone. They read from the same three token exports. One of them is on screen and one of them is not.

The hero has since had two bugs fixed in it. Its entry animation was rebuilt because the declarative version hoisted the verdict word out of the layout flow and painted it over the sentence underneath. And the coloured disc is now hidden from assistive technology, because labelling both the disc and the word made a screen reader read the answer twice, once as an image and once as text.

The chip has neither fix, because it was never on screen to have the bugs reported against it. So there are now two components in this repo that answer "how do we draw a verdict", and the one you would find by grepping for the word is the one with the old accessibility behaviour in it. The next person to need a verdict somewhere small has a 50 per cent chance of picking up the wrong one, and the wrong one will look right.

That is the argument for deleting it, and it is an argument about ambiguity rather than bytes. Dead code is cheap. A second plausible answer to a question you have already answered properly is expensive, and the price is paid by somebody who is not you, in six months, quietly.

The knowledge inside it is worth keeping and it is already kept: the three channels travel together through the token file, so there is no way to render a colour without also getting the icon and the word.

Eleven components with one importer, and why that is not a smell

The long tail looks like the same problem and is not.

ConfidenceBadge is used once. SelectionCanvas is used once. SummaryRow, Glow, ReconsentBanner, StillCrop, each used once. The instinct, especially if you have read a lot about premature abstraction, is to inline them.

The test I use is whether the component owns a decision. SelectionCanvas owns the on-screen half of a finger-drawn box over a camera preview, and sits on top of 194 lines of view-points-to-photo-pixels arithmetic that has nothing to do with the screen it is used on. ConfidenceBadge owns one mapping, from a level to a colour, an icon and a label, and that mapping has to be identical anywhere it appears whether or not it currently appears twice. Glow owns the fact that decoration is absolutely positioned, hidden from screen readers and passes touches through, which is three things that are easy to get wrong and impossible to notice when you get them wrong.

A component with one call site that owns a decision is a named decision. A component with one call site that owns nothing but markup is a paragraph you have to go and read somewhere else. The count does not distinguish them, which is why the count is a starting point and not a verdict.

While I was in there: 2 StyleSheet.create calls

One more number that came out of this, because it surprised me. The entire app contains two StyleSheet.create calls, both in the camera components, and neither of them for the usual reason.

Everything else is inline style objects reading from a theme hook, which 44 files use. That is the opposite of the advice I grew up on, and it follows from the app having a theme at all: a stylesheet is created once at module load, and the theme values it would need are not available then.

The two exceptions are both camera overlays, and if you look at what is in them the rule is visible. Not one value in either stylesheet comes from the theme: it is a scrim colour, an absolute position, and a 3px white border on the selection box. Those are literals by nature, drawn over a live camera frame rather than over the app's own surfaces, so there is nothing for a hook to supply. They are also the two components that re-render as your finger moves.

The two blocks are byte-identical for their first two entries, incidentally, which is the next small thing to fix.

So the pattern is not "inline styles are fine". It is that a token system and StyleSheet.create want opposite things, and in a themed app the stylesheet loses almost everywhere. Worth knowing before you follow a style guide into writing 37 of them.

If you want to see the components doing their actual job, the free tier needs nothing but an email address: get started, or the web build at app.munchable.app. The hero is the first thing you will see after a scan. The chip, you will not.

Top comments (0)