← Back to Lessons
  • coding standard guidelines

  • ai-assisted-development

  • software-architecture

  • development-best-practices

Keep Your Code Consistent: Writing Rules for Key Files with AI

Two Awesome Ways to Write Your Context Rules

Here is the problem: You're #vibecoding with AI, things are humming along, and then BAM! 💥 The AI spits out code that's totally different from what it did yesterday. Or it duplicates stuff, or suddenly decides to try out a brand-new design pattern without asking. Super frustrating! 😡

🔥 Here's a killer trick I've found to keep things on track: take a little time to have the AI help you write some rules for your most important files.

Think about it – you probably have a handful of files that are the real MVPs of your project. Maybe you call them "Managers," "Controllers," "Coordinators," "Services," or even "Core Modules." Whatever the name, these are the files that pull the strings, connect the dots, and make sure everything runs smoothly.

For each of these VIP files, you'll create a rule file. This rule file will spell out its responsibilities, the design patterns it should follow, how it integrates with other parts of your code, naming conventions, and all that good stuff.

🤩 And here's the best part: you don't even have to write these rules yourself! The AI can do the heavy lifting, or you can snag a template you like. Easy peasy!

Two Awesome Ways to Write Your Context Rules

So, when should you get these rules down? You've got a couple of options:

  1. Before You Start Building: Lay down the law from day one.
  2. In the Middle of Your Build: Get some code down, then make rules based on what's working.

If you're just diving into vibe-coding, I'd totally recommend option #2. Why? Because having some actual code already written makes it WAY easier to come up with smart rules. It's like trying to write a recipe – it's much simpler once you've actually cooked the dish a few times, right?

For example:

Let's say you're building an API in Python. You'll probably have a routes.py file. You can ask the AI to whip up a rule that describes how that routes.py file is structured and written. The AI will check out your existing file and describe it as a rule. That way, any new code added to routes.py tomorrow will follow the same awesome standards. Consistency for the win! ✨

How to Write Rules When You're Already Building (The Nitty-Gritty)

⚠️ Listen up, 'cause this is probably the second most important thing you can do when coding with AI (right after planning, of course!).

When should you pause and think about your rules? Keep an eye out for these moments – they're your cue to do a "rules audit":

  • New Coding Patterns Emerge: Anytime you're adding features that bring in new ways of doing things (like adding WebSockets or GraphQL).
  • Big Refactors or Updates Happen: After you've reorganized a bunch of code or upgraded a major library (hello, React 19!).
  • AI Suggestions Get Weird: If Cursor starts giving you suggestions that don't quite fit your project's style.
  • Sprint/Release Cycles: Make it a habit to review rules every sprint or two, or definitely before a big release.
  • New Teammates Onboard: Perfect time to get your conventions written down clearly for new folks joining the team.
  • After Generating Rules: Whenever you use a command like /Generate Cursor Rules, take a moment to double-check what the AI came up with.

How to Re-Write Rules or Create New Ones (Let's Get Practical!)

Okay, ready to make some rules? Here's how you do it:

  1. Use /Generate Cursor Rules (Your AI Helper!):
    • In Cursor, type /Generate Cursor Rules to get the AI to help you out. For example, you could prompt it with something like: "Generate a rule for how we handle WebSocket errors."
    • The AI might spit out something like this (saved as websocket-handling.mdc):
      1## WebSocket Handling 2- Always include error handling and reconnection logic. 3- Here's an example:
      1const socket = new WebSocket('ws://example.com'); 2 3socket.onerror = (error) => { 4 console.error('WebSocket error:', error); 5 // Don't forget to actually call your reconnect function! 6 attemptReconnect(); 7};
  2. Keep Your Rules Organized:
    • Store your rule files in a .cursor/rules/ directory in your project.
    • Give them clear, descriptive names (e.g., naming-conventions.mdc, api-endpoint-structure.mdc).
    • Want to get fancy? You can use subdirectories and glob patterns (like *.sql) so Cursor automatically knows which rules apply to which files. Super handy!
  3. Version Control is Your Friend:
    • Yep, commit those .mdc rule files to git!
    • Treat changes to rules like any other code change – review them in pull requests.
    • It's a good idea to have a README.md file inside your .cursor/rules/ directory that explains what each rule file is for.
  4. Validate Those Rules (Don't Skip This!):
    • The AI is smart, but it's not perfect. Always manually review the rules it generates to make sure they're accurate and make sense for your project.
    • Try them out! Apply the rules to some sample code and see if Cursor uses them correctly.
  5. Integrate with CI/CD (Optional Pro Move):
    • If you want to get really serious, you can add a step to your CI/CD pipeline to check the syntax of your .mdc files (e.g., using a Markdown linter).
  6. Monitor and Refine (Keep 'Em Fresh!):
    • Listen to your team! If developers are finding Cursor's suggestions unhelpful or off-base, it might be time to update your rules.
    • Rules aren't set in stone. If a rule becomes outdated or isn't relevant anymore, don't be afraid to tweak it or remove it.
  7. Document, Document, Document:
    • Keep that .cursor/rules/README.md up-to-date. Include guidelines on how to update rules and best practices for using /Generate Cursor Rules.

Example Workflow: Let's See It In Action! 🎬

Imagine you're adding a new chat feature to your app that uses WebSockets. Here's how you might use rules:

  1. Fire up Cursor and run /Generate Cursor Rules with a prompt like: "Generate a rule for WebSocket error handling in our new chat feature."
  2. The AI gives you some output. Save it to a new file, say .cursor/rules/chat-websocket-handling.mdc.
  3. Now, put it to the test! See how Cursor applies this rule to your actual chat feature code. If the suggestions aren't quite right, tweak the rule file until they are.
  4. Once you're happy, commit the new .mdc file to git. Make sure it gets reviewed in a pull request, just like any other code.
  5. Finally, update your main .cursor/rules/README.md to let everyone know about this new rule and what it does.

Creating rules before your project starts

I only recommend this for very specific situations in which you are building something that you already know exactly how to build and you have built it before, for example: If you are building a simple REST API in a very similar way that you did it yesterday.

Here you will find a bunch of pre-written cursor rules that may work for that purpose.

If you are a software or marketing agency you will probably re-use lots of pre-made rules, and I strongly recommend doing so.

Best Practices for Rule Nirvana 🧘‍♀️

  • Focus on Impact: You don't need a rule for everything. Focus on rules for features or patterns that really impact your coding standards and consistency.
  • AI + Human Brain = Best Combo: Let the AI do the initial drafting, but always use your human brain to validate and refine the rules.
  • Keep 'Em Lean and Mean: Concise, focused rules are the easiest to understand and maintain. Don't be afraid to Marie Kondo your ruleset and ditch any that no longer spark joy (or provide value).
  • Train Your Team: Make sure everyone on your team knows how to use /Generate Cursor Rules effectively and understands why these rules are important.

Good to Know 🤔

  • Rules Can Inherit: Just like CSS, rules can build on each other. You can have base rules for global standards (like general naming conventions) and then more specific rules for particular modules or file types.
  • Feedback Loop: The more you use /Generate Cursor Rules and refine the output, the better your prompts will become, and the better the AI will get at helping you!

You've got this! By setting up these rules, you're well on your way to smoother, more consistent coding with your AI partner. Go crush it! 🚀