Maintaining a polyglot codebase: the cost of context switching
The hidden cognitive overhead of working in PHP, Go and TypeScript at the same time, where the invisible cost builds up, and how I keep it manageable.
1 note was added later: October 31, 2026
I’ve been working with PHP, Go, and TypeScript simultaneously for a few years now. I write in different languages on different days — sometimes switching between two languages within the same day. This practice has a cognitive cost, and most of it is invisible. It doesn’t show up in meeting time, compile time, or test pass rates. Yet it’s real.
Context switching costs are usually discussed in terms of tasks: dropping a feature mid-way to fix a bug, then jumping into a meeting. Language-level context switching is a less-discussed but — in my experience — deeper category.
The way each language thinks
Every language imposes a different way of thinking. That might sound like a cliché, but it has concrete consequences in practice.
In Go, error handling is at the center of idiomatic code. There’s an if err != nil check after every function call — a linear, explicit, lexically visible flow. In PHP, throwing and catching exceptions is more common; the control flow is more implicit.
When I spend two hours writing Go in the morning and then switch to Laravel code, I notice small mental hesitations in how I handle errors. Getting back into idiomatic PHP requires a warm-up period. The same is true in reverse — switching from PHP to Go, my fingers type the wrong syntax out of habit for the first few lines.
TypeScript adds a different layer on top of both: a richer type system, stronger IDE integration, but the quirks of the browser/Node environment always lurking in the background. When moving from PHP or Go to TypeScript, I have to reload the “async everywhere” mindset.
Where the invisible cost accumulates
The cost of working across three languages is usually looked for in the wrong place. “Forgetting language syntax” is a surface-level problem; the real cost runs deeper.
Loss of idiomaticity. Every language has an idiomatic style. In Go you range over slices, in PHP you reach for array_map/filter/reduce or a collection pipeline, in TypeScript you favor functional flows. Context switching blurs those styles. The PHP code I write sometimes looks like “PHP written with a Go mindset.” It works, but it’s a poor signal for anyone reading it later.
Tool memory. Debugger shortcuts, IDE configurations, package manager habits. go mod tidy in Go, composer require in PHP, npm install in TypeScript. Small differences, but high-frequency repetition adds up and steals time.
Library switching points. You solve the same problem with different libraries in different languages. For date-time operations: Carbon in PHP, the standard time package in Go, date-fns or luxon in TypeScript. Choosing which API to think in is itself part of context switching.
Ways to reduce the cost
I keep this cost manageable with a few practices.
Working in language blocks. Rather than switching languages frequently throughout the day, I try to carve out blocks when possible — Go in the morning, PHP in the afternoon. It’s not always feasible, but when it is, it reduces the friction of switching.
Separate workspaces per language. Editor windows, terminal tabs, and sometimes even browser profiles are separated by language. This small physical/digital separation supports the mental transition.
Deliberate attention to idiomaticity. When writing in a given language, I put extra effort into sticking to that language’s idiomatic style. During code reviews, one of the things I look for is: does this code look like it was written by someone who writes in this language?
// Go idiomatic pattern
users, err := repository.FindActive()
if err != nil {
return nil, fmt.Errorf("active users fetch failed: %w", err)
}
// PHP idiomatic pattern
try {
$users = $repository->findActive();
} catch (RepositoryException $e) {
throw new ServiceException('Active users fetch failed', previous: $e);
}
Both handle errors, but each one reflects its own language’s expectations.
Is the polyglot return still positive?
Yes. Seeing different languages’ approaches to problem-solving gives you a perspective that a developer stuck in a single language simply doesn’t have. I don’t want to mimic Go’s error handling approach in PHP — but having seen that approach makes me think more clearly about error design in general.
The cost is real, and so is the return. The answer isn’t to ignore the context-switching overhead, but to be aware of it and manage it. An unmanaged cost accumulates silently over time — not in the quality of your output, but in the energy required to sustain it.
Experiments on this topic
A 4X that ends in one sitting: the constraint chose the scope
Build notes on a dice-free 4X core of 480 hexes and at most 60 turns, offline in the browser: a constraint chose the scope, and it shipped as Regnarium.
What it does today
Runs a full 4X match on a 480-hex map in at most 60 turns; rules, map generation and the opponent AI live in one pure TypeScript core, and combat rolls no dice. Anyone with half an hour and an appetite for strategy can open it in a browser.
TokenMetrics: making AI agent token spend visible per project
A desktop app that reads Claude Code, Codex CLI and Gemini CLI logs locally and shows token usage by project, model and session.
What it does today
Collects AI agent token usage from local logs and shows it by project, model, session and time, with estimated cost and subscription limit tracking. A developer who uses Claude Code, Codex CLI or Gemini CLI heavily and wants to see the spend can install it.
Comments
Sign in with your GitHub account to join the discussion. Comments are stored in GitHub Discussions.