โ ๐ฆ๐๐ป๐ฑ๐ฎ๐ ๐๐ผ๐ณ๐ณ๐ฒ๐ฒ & ๐๐ผ๐ฑ๐ฒ: ๐๐ผ๐บ๐ฝ๐น๐ฒ๐ ๐ถ๐๐ ๐ผ๐ป ๐ฅ๐ฒ๐พ๐๐ฒ๐๐ - ๐๐ฎ๐ป ๐๐ผ๐ ๐บ๐ฎ๐ธ๐ฒ ๐๐-๐๐ฟ๐ถ๐๐๐ฒ๐ป ๐ฐ๐ผ๐ฑ๐ฒ ๐น๐ฒ๐๐ ๐ฐ๐ผ๐บ๐ฝ๐น๐ฒ๐ ๐ท๐๐๐ ๐ฏ๐ ๐ฎ๐๐ธ๐ถ๐ป๐ด? ๐ข๐ป ๐ผ๐ป๐ฒ ๐๐ฒ๐ฟ๐ ๐ผ๐น๐ฑ ๐ ๐ฝ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ, ๐๐ฒ๐. ๐ง๐ต๐ฒ ๐๐ผ๐ฟ๐๐ ๐ณ๐๐ป๐ฐ๐๐ถ๐ผ๐ปโ๐ ๐ฐ๐ผ๐ด๐ป๐ถ๐๐ถ๐๐ฒ ๐ฐ๐ผ๐บ๐ฝ๐น๐ฒ๐ ๐ถ๐๐ ๐ฑ๐ฟ๐ผ๐ฝ๐ฝ๐ฒ๐ฑ ๐ณ๐ฟ๐ผ๐บ ๐ฐ๐ฏ ๐๐ผ ๐ฏ.
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
๐ฅ๐ฒ๐บ๐ฒ๐บ๐ฏ๐ฒ๐ฟ: ๐ง๐ต๐ถ๐ป๐ธ. ๐ ๐ผ๐ฑ๐ฒ๐น. ๐๐ฐ๐. (๐๐ฃ๐ ๐๐ค๐๐๐๐)
