All Insights
#SundayCoffeeAndCode
November 2026

Sunday Coffee & Code: Complexity on Request - Can you make AI-written code less complex just by asking?

I gave Claude Code a 1996 C curses menu program and two prompts: a plain refactor, then one that named the cognitive and cyclomatic complexity rules. The worst function's cognitive complexity dropped from 43 to 3.

By Steve Harris

โ˜• ๐—ฆ๐˜‚๐—ป๐—ฑ๐—ฎ๐˜† ๐—–๐—ผ๐—ณ๐—ณ๐—ฒ๐—ฒ & ๐—–๐—ผ๐—ฑ๐—ฒ: ๐—–๐—ผ๐—บ๐—ฝ๐—น๐—ฒ๐˜…๐—ถ๐˜๐˜† ๐—ผ๐—ป ๐—ฅ๐—ฒ๐—พ๐˜‚๐—ฒ๐˜€๐˜ - ๐—–๐—ฎ๐—ป ๐˜†๐—ผ๐˜‚ ๐—บ๐—ฎ๐—ธ๐—ฒ ๐—”๐—œ-๐˜„๐—ฟ๐—ถ๐˜๐˜๐—ฒ๐—ป ๐—ฐ๐—ผ๐—ฑ๐—ฒ ๐—น๐—ฒ๐˜€๐˜€ ๐—ฐ๐—ผ๐—บ๐—ฝ๐—น๐—ฒ๐˜… ๐—ท๐˜‚๐˜€๐˜ ๐—ฏ๐˜† ๐—ฎ๐˜€๐—ธ๐—ถ๐—ป๐—ด? ๐—ข๐—ป ๐—ผ๐—ป๐—ฒ ๐˜ƒ๐—ฒ๐—ฟ๐˜† ๐—ผ๐—น๐—ฑ ๐—– ๐—ฝ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ, ๐˜†๐—ฒ๐˜€. ๐—ง๐—ต๐—ฒ ๐˜„๐—ผ๐—ฟ๐˜€๐˜ ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ปโ€™๐˜€ ๐—ฐ๐—ผ๐—ด๐—ป๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฐ๐—ผ๐—บ๐—ฝ๐—น๐—ฒ๐˜…๐—ถ๐˜๐˜† ๐—ฑ๐—ฟ๐—ผ๐—ฝ๐—ฝ๐—ฒ๐—ฑ ๐—ณ๐—ฟ๐—ผ๐—บ ๐Ÿฐ๐Ÿฏ ๐˜๐—ผ ๐Ÿฏ.

Iโ€™ve seen a few posts here lately saying code written by AI agents is overly complex and hard to maintain. That may be true if you donโ€™t ask for anything different. I wanted to know whether complexity is baked into the tool, or whether it changes with what you ask for.

๐—ง๐—ต๐—ฒ ๐—ฒ๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—บ๐—ฒ๐—ป๐˜

The subject: menu.c, a curses menu program I wrote in 1996 for an IBM AIX system running PICK applications. About 630 lines of C, with almost all the logic in one 247-line main() - I know, I know, bad out of the gate.

I gave Claude Code (Opus 5.5) two prompts:

  • โ€œRefactor the code in this file into a new file.โ€
  • Create a new version, based on the refactored version, that will โ€œvery carefully consider the Cognitive and Cyclomatic complexity rules and attempt to improve both scores.โ€

Both compile cleanly, and their screens matched the original side by side in an 80ร—24 terminal.

๐—›๐—ผ๐˜„ ๐—ถ๐˜ ๐˜„๐—ฎ๐˜€ ๐˜€๐—ฐ๐—ผ๐—ฟ๐—ฒ๐—ฑ

  • ๐—–๐˜†๐—ฐ๐—น๐—ผ๐—บ๐—ฎ๐˜๐—ถ๐—ฐ ๐—ฐ๐—ผ๐—บ๐—ฝ๐—น๐—ฒ๐˜…๐—ถ๐˜๐˜†: decisions + 1, roughly the number of tests needed to cover a function. Common guideline: 10 or below. (๐˜Š๐˜ณ๐˜ฐ๐˜ด๐˜ด ๐˜ค๐˜ฉ๐˜ฆ๐˜ค๐˜ฌ๐˜ฆ๐˜ฅ ๐˜ธ๐˜ช๐˜ต๐˜ฉ ๐˜ญ๐˜ช๐˜ป๐˜ข๐˜ณ๐˜ฅ.)
  • ๐—–๐—ผ๐—ด๐—ป๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฐ๐—ผ๐—บ๐—ฝ๐—น๐—ฒ๐˜…๐—ถ๐˜๐˜† (G. Ann Campbell, Sonar): how hard code is to understand. Flow breaks and nesting cost points; well-named functions cost nothing. SonarQubeโ€™s default limit: 15.

๐—ง๐—ต๐—ฒ ๐—ฟ๐—ฒ๐˜€๐˜‚๐—น๐˜๐˜€ (worst function, cyclomatic / cognitive)

  • Original (1996): 20 / 43
  • Prompt 1, refactor: 7 / 9
  • Prompt 2, reduce complexity: 4 / 3

๐—”๐—ฐ๐—ฟ๐—ผ๐˜€๐˜€ ๐˜๐—ต๐—ฒ ๐˜„๐—ต๐—ผ๐—น๐—ฒ ๐—ฝ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ, ๐˜๐—ต๐—ฒ ๐˜๐—ผ๐˜๐—ฎ๐—น ๐—ฐ๐—ผ๐—ด๐—ป๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฐ๐—ผ๐—บ๐—ฝ๐—น๐—ฒ๐˜…๐—ถ๐˜๐˜† ๐˜„๐—ฒ๐—ป๐˜ ๐Ÿฒ๐Ÿญ โ†’ ๐Ÿฑ๐Ÿฎ โ†’ ๐Ÿฎ๐Ÿฎ.

๐—ช๐—ต๐—ฎ๐˜ ๐—œ ๐˜๐—ผ๐—ผ๐—ธ ๐—ณ๐—ฟ๐—ผ๐—บ ๐—ถ๐˜

  • A plain refactor fixed the worst function. Claude broke the giant main() into small, named functions.
  • Asking again went further: 80% below the original on cyclomatic, 93% on cognitive.
  • Not everything improved first time. Total cyclomatic rose from 42 to 59, partly because each new function adds a base point, partly from new checks that fixed buffer overflows and bad input handling. Prompt 2 kept those fixes and still came in at 41.
  • ๐—ช๐—ต๐—ฎ๐˜ ๐˜†๐—ผ๐˜‚ ๐—ฎ๐˜€๐—ธ ๐—ณ๐—ผ๐—ฟ ๐—บ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐˜€. ๐—ก๐—ฎ๐—บ๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—บ๐—ฒ๐˜๐—ฟ๐—ถ๐—ฐ๐˜€ ๐—ฎ๐—ป๐—ฑ ๐—ฝ๐—ผ๐—ถ๐—ป๐˜๐—ถ๐—ป๐—ด ๐—ฎ๐˜ ๐˜๐—ต๐—ฒ ๐—ฟ๐˜‚๐—น๐—ฒ๐˜€ ๐—บ๐—ฎ๐—ฑ๐—ฒ ๐˜๐—ต๐—ฒ ๐—ฏ๐—ถ๐—ด๐—ด๐—ฒ๐˜€๐˜ ๐—ฑ๐—ถ๐—ณ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ. ๐—œ๐—ณ ๐˜†๐—ผ๐˜‚ ๐—ฐ๐—ฎ๐—ฟ๐—ฒ ๐—ฎ๐—ฏ๐—ผ๐˜‚๐˜ ๐—ฐ๐—ผ๐—บ๐—ฝ๐—น๐—ฒ๐˜…๐—ถ๐˜๐˜†, ๐˜€๐—ฎ๐˜† ๐˜€๐—ผ, ๐—ฎ๐—ป๐—ฑ ๐—ด๐—ถ๐˜ƒ๐—ฒ ๐˜๐—ต๐—ฒ ๐—ฎ๐—ด๐—ฒ๐—ป๐˜ ๐˜๐—ต๐—ฒ ๐˜†๐—ฎ๐—ฟ๐—ฑ๐˜€๐˜๐—ถ๐—ฐ๐—ธ.

Obviously this is a small 1990s C program, written by one person. Larger modern codebases may differ. Also worth noting is the numbers can be gamed, a small part of the second drop came from removing checks that could never fail.

The code and all three versions are on GitHub: steveh250/Unix-Menu-Program

๐—ฅ๐—ฒ๐—บ๐—ฒ๐—บ๐—ฏ๐—ฒ๐—ฟ: ๐—ง๐—ต๐—ถ๐—ป๐—ธ. ๐— ๐—ผ๐—ฑ๐—ฒ๐—น. ๐—”๐—ฐ๐˜. (๐™–๐™ฃ๐™™ ๐™˜๐™ค๐™›๐™›๐™š๐™š)

Want to Discuss This Topic?

Steve is always happy to have a direct conversation.