Be intentional about how AI changes your codebase
TL;DR Highlight
A guide presenting concrete code writing principles — Semantic Function design, Pragmatic Function design, type-safe model design, and more — to prevent codebases from silently rotting as AI coding agents rapidly generate code.
Who Should Read
Backend and fullstack developers or team leads who use AI coding agents (Cursor, Copilot, etc.) daily and worry about codebase quality degradation.
Core Mechanics
- Semantic Functions are functions whose names and signatures fully communicate their intent and constraints — making them easier for AI agents to use correctly without reading the implementation.
- Pragmatic Functions handle side effects, I/O, and state mutations in clearly bounded, explicit ways — preventing AI agents from inadvertently scattering side effects across the codebase.
- Type-safe model design — using discriminated unions, branded types, and exhaustive pattern matching — creates guardrails that catch AI-generated errors at compile time rather than runtime.
- The guide argues that 'AI-resistant code' is code that's explicit, strongly typed, and well-named enough that even an AI agent generating code around it is unlikely to introduce subtle bugs.
- These principles aren't new — they're established software engineering best practices — but their value is amplified in AI-assisted development where code generation speed outpaces human review speed.
Evidence
- The author shared before/after code examples where applying these principles to a codebase measurably reduced the frequency of AI-generated bugs in adjacent code.
- Commenters with TypeScript backgrounds found the type-safe model design section particularly compelling — several noted that discriminated unions are underutilized even in human-written code.
- Some pushback: applying all these principles rigorously adds overhead and might slow down initial development, even if it helps quality long-term.
- The observation that 'good code is good code regardless of who wrote it' resonated — the principles aren't special-cased for AI, they're just good engineering.
How to Apply
- Audit your codebase for functions with side effects scattered throughout — refactor these to be explicit and contained as a first step toward AI-resistant code.
- Introduce discriminated unions for state that has multiple distinct modes (loading/error/success patterns) — this prevents AI agents from conflating states.
- Write function signatures as contracts: if a function should never receive null, encode that in the type system rather than relying on documentation.
- When reviewing AI-generated code, specifically check: are side effects contained? Are types explicit? Are function names accurate to behavior? These are the most common AI failure modes.
Code Example
// Brand Type example (TypeScript)
type DocumentId = string & { readonly __brand: 'DocumentId' };
type MessagePointerId = string & { readonly __brand: 'MessagePointerId' };
function getDocument(id: DocumentId) { /* ... */ }
const msgId = 'abc123' as MessagePointerId;
// getDocument(msgId); // ❌ Compile error: prevents incorrect type usage
// Semantic Function example
function retryWithExponentialBackoff<T>(
fn: () => Promise<T>,
maxRetries: number,
baseDelayMs: number
): Promise<T> {
// Only takes inputs, only returns outputs, no side effects
}
// Add skills to AI agent with npx
// npx skills add theswerd/aicodeTerminology
Related Papers
Show HN: OpenKnowledge – open source AI-first alternative to Obsidian/Notion
Git 기반 동기화와 Claude/Codex/Cursor 연동을 내장한 로컬 우선 마크다운 에디터로, AI 에이전트의 두 번째 뇌(LLM Wiki)로 활용할 수 있는 오픈소스 도구다.
The Unfireable Safety Kernel: Execution-Time AI Alignment for AI Agents and Other Escapable AI Systems
AI 에이전트가 자신의 안전장치를 우회할 수 없도록, 에이전트 프로세스 바깥에 수학적으로 증명된 강제 통제 게이트를 배치하는 아키텍처
RubyLLM: A Ruby framework for all major AI providers
OpenAI, Claude, Gemini 등 주요 AI 프로바이더를 단일 인터페이스로 통합한 Ruby 프레임워크로, Rails 통합과 에이전트 기능까지 지원해 Ruby 개발자가 AI 기능을 빠르게 붙일 수 있다.
Qwen-AgentWorld: Language World Models for General Agents
Alibaba Qwen 팀이 AI 에이전트가 행동 결과를 미리 시뮬레이션할 수 있는 'Language World Model'을 공개했다. 에이전트 훈련과 실행 경로 검증에 새로운 패러다임을 제시하는 연구다.
SHERLOC: Structured Diagnostic Localization for Code Repair Agents
버그 위치만 알려주는 게 아니라 '왜, 어떻게 고쳐야 하는지'까지 진단 리포트를 생성해서 코드 수정 에이전트의 성능을 높이는 training-free 프레임워크
Show HN: peerd – AI agent harness that runs entirely in your browser
백엔드 서버 없이 Chrome/Firefox 확장 프로그램으로만 동작하는 AI 에이전트 실행 환경으로, 브라우저 탭을 직접 조작하고 WASM Linux VM까지 구동할 수 있어 프라이버시와 보안을 동시에 챙길 수 있다.