How can I make the AI follow my coding style and conventions?
Be explicit about your style
AI models don't inherently know your preferences. You need to state them clearly: indentation (tabs vs. spaces, size), naming conventions (camelCase, snake_case), quote style, and whether you use semicolons. You can include a short 'style guide' in your prompt or, better, in a persistent project file.
Many AI coding assistants support a special file (e.g., .aider.conf.yml, .cursorrules, or a custom instructions file) where you can define rules that apply to all interactions. Check your tool's documentation for how to set this up. If your tool doesn't support it, prepend your style rules to each prompt.
- Specify indentation, quotes, semicolons, and trailing commas.
- Mention preferred libraries or frameworks (e.g., 'use React with TypeScript and functional components').
- State naming patterns for variables, functions, and files.
- Include examples of your preferred code structure.
Use examples and corrections
If the AI deviates from your style, correct it and ask it to remember the rule. For instance: 'Use single quotes for strings, not double quotes.' Repeating this a few times can help the model adapt within a session.
You can also paste a snippet of your existing code as a reference. The AI will often mimic the style it sees. This is especially effective for complex patterns like error handling or logging.
Common mistakes
- Assuming the AI will automatically infer your style from context; it usually needs explicit instructions.
- Not using persistent configuration files, so you have to repeat style rules in every prompt.
- Giving contradictory style rules (e.g., 'use 2 spaces' and 'use tabs') which confuses the model.
