{"slug": "ai-coding-tools-write-features-faster-than-you-can-finish-them-ship-them-dark-a", "title": "AI coding tools write features faster than you can finish them: ship them dark behind a feature flag", "summary": "Tomás Herrera, writing for NBT, outlined a lightweight feature-flag pattern for teams using AI coding tools, arguing that half-finished AI-generated features should be merged daily behind a single typed isEnabled() switch rather than parked on long-lived branches. The approach keeps flags in one file with a kind, owner, default and removal date, gates each feature at exactly one call site, and adds tests that run both flag states plus a guard against stray flag checks. Herrera recommends hashing user IDs for percentage rollouts instead of Math.random() and moving flag values to a database or service only when redeploys become a bottleneck.", "body_md": "*By Tomás Herrera, NBT*\n\nWith AI coding tools, half of a feature can exist by lunchtime. The new checkout page renders, the export runs for small files, the redesign covers three screens out of eight. The rest needs another day, a review, a test against real data.\n\nThe usual choices are both bad. Keep the work on a long-lived branch and it drifts from main until merging is painful. Merge it unfinished and users find it. A feature flag is the third option: merge small pieces into main every day, with the new code switched off until it's ready. It's also an off switch if the finished feature misbehaves in production.\n\nYou don't need a flag service to start. Here's a step-by-step setup that fits in one file, and the rules that keep flags from turning into a mess.\n\nMartin Fowler's article on feature toggles separates flags by how long they live and who changes them. Two kinds cover most small teams:\n\nWrite the kind next to each flag. A release flag that's still there six months later is a bug.\n\nEvery check should go through one function. Ask for it like this:\n\n```\nAdd src/flags.ts with:\n- a typed list of flags, each with a name, kind (release | ops), owner,\n  default value and a \"remove by\" date for release flags;\n- isEnabled(name, user?) that reads overrides from env vars\n  (FLAG_NEW_CHECKOUT=on|off) and falls back to the default;\n- a TypeScript error if code asks for a flag that isn't in the list.\nNo external service. Don't use the flag anywhere yet.\n```\n\nThe result looks roughly like this:\n\n``` js\nexport const FLAGS = {\n  newCheckout: {\n    kind: \"release\", owner: \"payments\", default: false,\n    removeBy: \"2027-01-15\",\n  },\n  pdfExport: { kind: \"ops\", owner: \"reports\", default: true },\n} as const;\n\ntype FlagName = keyof typeof FLAGS;\n\nexport function isEnabled(name: FlagName): boolean {\n  const env = process.env[`FLAG_${toSnake(name).toUpperCase()}`];\n  if (env === \"on\") return true;\n  if (env === \"off\") return false;\n  return FLAGS[name].default;\n}\n```\n\nThe typed list matters more than it looks: a misspelled flag name becomes a compile error instead of a feature that's silently never on.\n\nThe most common mistake in AI-written flag code is sprinkling `if (isEnabled(...))` across twenty files. Then turning the flag off leaves half the feature visible, and deleting the flag later is a long hunt.\n\nTell your AI coding tools where the switch goes:\n\n```\nGate the new checkout behind isEnabled(\"newCheckout\") in exactly one\nplace: the route that chooses which checkout page to render. Old and\nnew code paths stay separate files. No other file may check this flag.\n```\n\nFor a backend change, the single place might be where a service is chosen. For a UI change, it's usually where a route or a top-level component is picked.\n\nEnv vars are fine for on/off per environment: off in production, on in staging. To try the feature in production yourself before anyone else, add a simple user rule:\n\n```\nexport function isEnabled(name: FlagName, user?: { email: string }) {\n  if (user && process.env.FLAG_TESTERS?.split(\",\").includes(user.email)) {\n    return true;\n  }\n  // ...env and default as before\n}\n```\n\nThat's enough for \"staff only\". If you need a percentage rollout, hash the user id (so the same person always gets the same answer) rather than using `Math.random()` on each request, which flips people between versions.\n\nWhen you outgrow env vars, because you want to change a flag without redeploying, move the values to a database table or a flag service. Keep the same `isEnabled` function, so no calling code changes.\n\nA flag doubles the paths through that part of the code. Ask for tests that run both:\n\n```\nFor every test of checkout, run it twice: newCheckout on and off.\nAdd one test that fails if any file other than src/routes/checkout.ts\ncalls isEnabled(\"newCheckout\").\n```\n\nThe second test is a cheap guard for Step 3. A grep in CI does the same job.\n\nAn ops flag is only useful if flipping it is quick. Write down, in the README or runbook, exactly how to turn each ops flag off in production: which setting, where, and how long it takes to apply. If changing an env var needs a redeploy that takes ten minutes, your kill switch takes ten minutes. That may be fine. Know it before the incident.\n\nAlso decide what \"off\" means. For `pdfExport`, off should show a friendly \"PDF export is paused\" message, not a 500 error.\n\nEvery release flag got a \"remove by\" date in Step 2. Use it:\n\n```\nList release flags whose removeBy date has passed. For each fully\nrolled-out flag, delete the flag, the old code path and its tests,\nin a separate commit per flag.\n```\n\nRemoving a flag is a good task for AI coding tools: it's mechanical, the typed list shows every use, and the tests from Step 5 tell you whether anything broke. Do one flag per commit so a mistake is easy to revert.\n\nA CI check that fails when a `removeBy` date is more than two weeks past keeps this from slipping.\n\n```\nFeature flags:\n- Add flags only in src/flags.ts, with kind, owner, removeBy.\n- Check a flag in one place per feature.\n- New code paths go in new files; don't interleave old and new.\n- Tests run with the flag on and off.\n```\n\nWith these in place, you can merge AI-written work every day without waiting for the whole feature, and turn it off again in a minute if it misbehaves. That's also what makes it easier to [review code from AI coding tools](https://www.ninjabuilder.app/blog/review-ai-generated-code) in small, safe pieces instead of one giant diff.", "url": "https://wpnews.pro/news/ai-coding-tools-write-features-faster-than-you-can-finish-them-ship-them-dark-a", "canonical_source": "https://dev.to/attilaalbert/ai-coding-tools-write-features-faster-than-you-can-finish-them-ship-them-dark-behind-a-feature-flag-4bge", "published_at": "2026-12-06 13:06:42+00:00", "updated_at": "2026-12-06 13:14:20.791637+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools", "ai-agents"], "entities": ["Tomás Herrera", "NBT", "Martin Fowler"], "also_reported_by": [], "takeaway": "Gate each AI-generated feature behind one typed isEnabled() call site with a removal date, and add a test that fails if any other file checks that flag.", "recommendation": {"advice": "When AI tools leave features half-built, merge them daily behind a single typed flag check and add a CI test that fails if any other file calls isEnabled for it.", "playbook": "coding-agents", "playbook_url": "https://wpnews.pro/playbooks/coding-agents"}, "alternates": {"html": "https://wpnews.pro/news/ai-coding-tools-write-features-faster-than-you-can-finish-them-ship-them-dark-a", "markdown": "https://wpnews.pro/news/ai-coding-tools-write-features-faster-than-you-can-finish-them-ship-them-dark-a.md", "text": "https://wpnews.pro/news/ai-coding-tools-write-features-faster-than-you-can-finish-them-ship-them-dark-a.txt", "jsonld": "https://wpnews.pro/news/ai-coding-tools-write-features-faster-than-you-can-finish-them-ship-them-dark-a.jsonld"}}