Does Claude Code write bloated or insecure code?

Why AI-written code can be bloated, chatty or insecure, and the CLAUDE.md rules, hooks and reviews that keep Claude Code output lean and safe.

Lesson 4.3โฑ 9 min read๐ŸŽ๏ธ Power userโ“ 3-question quiz
The short answer

The criticisms have some truth: left to its defaults, an AI agent can add extra code, files and comments, and it can repeat insecure patterns it has seen. The fix is to treat Claude like a fast junior colleague: set house rules in CLAUDE.md, keep tasks small, enforce checks with hooks and linters, run a security review, and keep a person accountable for what ships, with human review for risky changes.

Is the criticism fair?

People say AI coding tools bloat repositories, write chatty comments and produce insecure code. Each of those can happen. None of them is inevitable. Most come from vague instructions and missing review, the same things that cause problems with a new human hire.

Claude follows the standards you give it. If you never say "keep the change small" or "no comments that repeat the code", it falls back to general habits that may not match yours.

Analogy: a talented new starter on their first day. Without a style guide or code review they will still produce work, but it will not look like your team's work. Give them the guide and review their pull requests, and the problem mostly disappears.

Why bloat happens

  • Defensive extras: checks for cases that cannot happen, wrappers and helper functions used once.
  • Scope creep: "while I was there" changes to files you did not ask about.
  • Leftover files: scratch scripts, notes and test files that end up committed.
  • New dependencies: a package added for something the language already does.
  • Narrating comments: // increment the counter above count++.

Fix 1: write your house rules into CLAUDE.md

Add a short section to your project's CLAUDE.md. Claude reads it at the start of every session:

## Code style
- Make the smallest change that solves the task. Do not refactor unrelated code.
- Comments explain why, never what. No comments that restate the code.
- Do not add a dependency without asking first.
- Do not create new files when an existing one is the right home.
- Delete any scratch files before you finish.
- Match the patterns already used in this codebase.

Be specific. "Write clean code" is too vague to change behaviour; "no comments that restate the code" is not.

Fix 2: keep tasks small and review the diff

  • Plan first for anything bigger than a small fix, and cut the plan down before approving it.
  • One job per session. Big, vague requests produce big, vague changes.
  • Read the diff (git diff) before every commit, and check git status for stray files.
  • Ask for a trim: "Review your change. Remove anything not needed for the task, including comments that restate the code." Claude is good at cutting its own work back.

Why security gaps happen

Models learn from a huge amount of public code, and public code contains insecure patterns. Common risks to watch for:

  • Injection: building SQL queries or shell commands from user input.
  • Secrets in code: API keys pasted into files instead of environment variables.
  • Missing checks: an endpoint that forgets to confirm who is logged in.
  • Made-up or risky packages: a dependency name that does not exist (attackers register such names) or one that is unmaintained.
  • Prompt injection: instructions hidden in a web page, issue or file that Claude reads, trying to steer it.

Fix 3: build security into the workflow

  • Run a security review: Claude Code has a built-in /security-review command that checks your pending changes for common vulnerabilities. Run it before merging.
  • Block secrets: add deny rules so Claude cannot read files such as .env, and keep keys in environment variables.
  • Enforce with hooks: a PreToolUse hook can block edits to sensitive files; a PostToolUse hook can run your linter and secret scanner after every edit.
  • Check every new package: confirm it exists, is maintained and is the one you meant, and run your dependency audit.
  • Least privilege: only allow the commands and MCP tools a task needs, and never give an agent production credentials.
  • Extra human review for login, payments, personal data and anything that touches money or customers.
The rule that matters most: a person stays accountable for every change that goes live. Many teams now let AI review routine changes, backed by tests and automated checks, and keep human review for the risky ones: logins, payments, personal data and anything that touches money or customers. What good teams never do is ship code that nothing and nobody has checked.

Our view

Claude Code is a force multiplier, and that cuts both ways: good standards get applied faster, and missing standards get missed faster. Teams that get the most from it write their rules down, keep changes small, automate their checks and keep a person in the review loop. Set up that way, the output is often tidier than what the team was producing before, because the rules are applied every single time.

Check yourself

Three quick questions to check it stuck.

Ask the tutor

Stuck on something in this lesson? Ask the AI tutor. It answers from this lesson and points you to others that help. Answers are written by AI and can be wrong. Do not include personal details, passwords or company secrets.

Frequently asked

Is code written by Claude Code secure?

It can be, but not automatically. Treat it like any new contributor: run a security review (Claude Code has a /security-review command), use linters and secret scanning, block access to secrets, and have a person review changes around login, payments and personal data, and stay accountable for the rest.

How do I stop Claude Code adding so many comments?

Add a clear rule to your project CLAUDE.md, such as "Comments explain why, never what. No comments that restate the code." Then ask Claude to trim any change that still has noisy comments before you commit.

Does Claude Code bloat repositories?

It can add extra code, files and dependencies if left to its defaults. Ask for the smallest change, keep one task per session, check git status and git diff before committing, and require approval for new dependencies.