The Secret to Making AI Write 'Clean' Code: Creating Your Own AI Ruleset
Sometimes, after struggling with a complex bug in front of the screen for hours, I step away from the computer for a short walk. Interestingly, I usually make the best architectural decisions in those moments when I am completely away from the code. Actually, the situation is not much different in software development; rather than getting lost in complexity, setting simple and clear rules from the beginning saves lives.
Especially in this period where generative AI has settled at the center of our lives and become indispensable.
I actively use terminal-based AI tools in my workflow. These tools drastically shorten the processes we call "grunt work" by generating massive blocks of code in seconds. However, as Uncle Bob (the author of Clean Code) says right at the beginning of his book: "We spend more time reading code than writing it."
If you leave AI to its own devices, it can often give you spaghetti code written with a "just make it work, no matter how" mentality, where everything is piled into a single file, or we get lost within the code due to unstable prompt quality and end up with a structure that is not open to development. Exactly at this point, the role of the developer shifts from being the "code writer" to a "technical editor" who manages the system and draws boundaries for the AI. The most effective way to do this is through creating AI Rulesets, which are considered the constitution of artificial intelligence.
Why Do AI Assistants Need Strict Boundaries?
Simply telling an AI "Write me clean code" is as vague as telling an architect "Build me a beautiful house." For AI, "clean" might be the most common usage in its current dataset; whereas for your project, "clean" could mean an architecture where UI and business logic are separated by strict lines.
To rein in the AI, you need to add a specific .ruleset to your prompts or your project's root directory. These files determine the strict architectural boundaries that the AI must follow in every code generation.
A Real-World Example
Recently, I published a CLI tool named react-native-boilerplate on NPM for developers to quickly bootstrap React Native projects (https://www.npmjs.com/package/create-rn-expo-starter). While developing this boilerplate (and the projects I built on it), I added an ai_guidelines.md file to my Github repository to ensure that the AI does not deviate from my standards. I also got help from an AI while writing this ruleset myself. I do not claim that this ruleset is complete and flawless. What I want to tell you in this article is to draw attention to how important rulesets are and that this is a crucial area where we need to improve ourselves.
While developing this ruleset, I thought about the following. If I am working with the trio of React Native (Expo), TypeScript, and NativeWind, a summary of the rules I need to impose on my AI assistant should be as follows:
1. Single Responsibility and Folder Structure
AI is generally inclined to suggest massive components of 150-200 lines. I draw a clear line against this in my ruleset: "Never keep business logic and UI code in the same place. Move all data fetching or manipulation operations to custom hooks. Components should only be responsible for displaying (rendering) data."
2. State Management Boundaries
AI can sometimes try to construct a Context API even for a simple state. My rules are clear: "Always use TanStack Query for server-state. Use only Zustand for global states on the client side (client-state). Do not mix the two."
3. Strict Type Safety (Strict TypeScript)
"Using any or ts-ignore in the code you generate is strictly forbidden. Always explicitly make interface or type definitions. Provide precise type conversions for data coming from the API."
4. Naming Standards (Meaningful Names)
"Boolean values must always take the is, has, or should prefixes (for example: isLoading, hasError). Do not use temporary and meaningless (temp, data1, val) naming."
A small note:
Of course, this set of rules I shared was constructed specifically for the React Native ecosystem and the libraries I use myself. If you are working in a different software environment (for example, in a Python backend project, .NET architecture, or game development), you must remember that you need to prepare custom rulesets specific to your own technology stack and project dynamics to guide the AI correctly.
Being the System Designer, Not the Code Writer
In these days when I am transitioning to cloud computing architectures (AWS), I better understand how universal the Clean Code philosophy is, not only on the front-end or mobile side but across the entire software ecosystem. The issue is not just about writing the right code; it is about being able to build the right system with the right rules.
In the era where AI writes code, our value will be measured not by how fast we use the keyboard, but by how we manage that speed within a high-quality and sustainable framework. Adding a ruleset to your own projects could be your strongest first step on this path.