{"slug": "why-ai-generates-500-lines-of-code-in-one-file-part-1-2", "title": "Why AI Generates 500 Lines of Code in One File...? (Part 1/2)", "summary": "A developer explains why AI-generated code often produces large, monolithic files with duplicated logic and inline styles, prioritizing prompt completion over long-term maintainability. The article contrasts AI's 'path of least resistance' with senior engineers' focus on code that can be maintained for years, and previews a follow-up on refactoring techniques.", "body_md": "TL;DR\n\nAI tends to generate the \"path of least resistance\" based on what dominates its training data and examples, not necessarily what is architecturally best for a long-lived codebase.\n\nKnowing this, there are steps we can take as seasoned developers to improve the code in multiple passes (part 2).\n\nSo your non-technical friends have told you about creating a complete landing page in a few minutes! Everything works. They can modify, create, delete elements and AI executed each perfectly. And fast. They love the site, and will definitely create more pages/sites this way.\n\nYou have a look at the code, and it looks like\n\n❌ Messy code:\n\n```\nexport default function XmasMugLandingPage() {\n  return (\n    <main className=\"min-h-screen bg-red-50 text-gray-900 overflow-hidden\">\n      <div className=\"absolute top-0 left-0 w-full h-full bg-gradient-to-br from-red-200 via-white to-green-200 opacity-70\"></div>\n\n      <section className=\"relative z-10 px-6 py-10\">\n        <div className=\"max-w-7xl mx-auto\">\n          <div className=\"flex flex-col lg:flex-row items-center justify-between gap-10\">\n            <div className=\"w-full lg:w-1/2\">\n              <div className=\"inline-block bg-red-600 text-white px-4 py-2 rounded-full text-sm font-bold mb-6\">\n                LIMITED CHRISTMAS EDITION\n              </div>\n\n// ... (messy code all in one function with \n//.      utility classes repeated 20 times)\n```\n\nThe definition of \"good\" AI is obviously different from your expectation! ...!?\n\nWhen we ask AI:\n\nBuild me a landing page with hero, pricing, FAQ and contact form.\n\nThe AI has several competing goals:\n\nNot because this is good architecture, But because it maximizes the probability of a correct response.\n\nA senior engineer thinks:\n\nSomeone will maintain this for five years.\n\nThe AI's priority:\n\nI need to successfully answer this prompt.\n\nThose are completely different optimization functions.\n\nThis explains: giant components, duplicated utility strings, repeated business logic and copy-pasted sections.\n\n**With CSS Module:**\n\n```\n// in a CSS file\n.card {\n  display: flex;\n  align-items: center;\n  gap: 1rem;\n  padding: 1.5rem;\n}\n\n// in a HTML file\n<div className={styles.card}>\n```\n\n**In Tailwind**\n\n```\n/// all in one file\n<div className=\"flex items-center gap-4 p-6\">\n```\n\nObviously, AI will have less to worry about if it's all in one file.\n\nThe AI can see the source code of:\n\ninside the project. No hidden implementation, and it can modify components directly.\n\nWith a library:\n\n``` js\nimport { Button } from \"@mui/material\"\n```\n\nthe AI cannot edit MUI's source code. It has to understand the MUI API - and that's harder.\n\nWith more and more apps written this way, it seems important that we developers learn to teach AI to tidy and refactor the code! Here are some outlines:\n\nIn the next article, I will expand on each step and provide a walk through.\n\nDo you have a similar experience with AI generated code? Does some of the AI tendencies surprise you??", "url": "https://wpnews.pro/news/why-ai-generates-500-lines-of-code-in-one-file-part-1-2", "canonical_source": "https://dev.to/cathylai/why-ai-generates-500-lines-of-code-in-one-file-part-12-j6p", "published_at": "2026-06-24 04:52:48+00:00", "updated_at": "2026-06-24 05:13:42.388097+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "developer-tools"], "entities": ["Tailwind CSS", "MUI"], "alternates": {"html": "https://wpnews.pro/news/why-ai-generates-500-lines-of-code-in-one-file-part-1-2", "markdown": "https://wpnews.pro/news/why-ai-generates-500-lines-of-code-in-one-file-part-1-2.md", "text": "https://wpnews.pro/news/why-ai-generates-500-lines-of-code-in-one-file-part-1-2.txt", "jsonld": "https://wpnews.pro/news/why-ai-generates-500-lines-of-code-in-one-file-part-1-2.jsonld"}}