Why shared guidelines matter now
As teams adopt AI-assisted coding tools, the boundary between human-written and AI-generated code is blurring. Shared guidelines reduce churn, keep generated code secure and testable, and make reviews predictable. For background reading from the community, see the Stack Overflow post on building shared coding guidelines for AI (and people too).
Principles to include in your guidelines
- Specification-first: Prefer clear, executable requirements and tests before asking an AI to generate code.
- Deterministic outputs: Use prompt templates, style guides, and formatters so generated code is consistent and lintable.
- Safety and privacy: Define rules for handling secrets, external data, and model telemetry.
- Test coverage: Require unit/integration tests for generated code or a review gate that enforces test creation.
- Auditability: Tag generated files (metadata header) and keep generation arguments in version control.
Repository structure (example)
Keep guidelines and enforcement close to code:
- /guidelines/ — markdown and machine-readable rules
- /ai-templates/ — prompt templates and instruction sets
- pre-commit/ci — hooks and CI jobs that validate AI outputs
- /generated/ — optional generated-code directory with metadata
Example machine-readable rule (JSON)
{
"name": "team-ai-coding-rules",
"version": "1.0.0",
"rules": {
"maxLineLength": 120,
"requireTestsForGeneratedCode": true,
"banSecretsInOutputs": true,
"addGeneratedHeader": true
}
}Prompt templates: make AI predictable
Store strict prompt templates that define expected file layout, language version, tests to generate, and linting commands. Save these in /ai-templates so CI and devs use the same inputs.
Minimal prompt template (JSON metadata)
{
"templateName": "node-api-endpoint",
"description": "Generate an Express.js GET endpoint with input validation and Jest tests.",
"instructions": "Use Node 20, ES modules, include OpenAPI-style JSDoc, do not access any external secrets, and add a 'generated-by' header.",
"lintCommand": "npm run lint",
"testCommand": "npm test"
}Automating checks: CI and pre-commit patterns
Enforce the guidelines with a combination of local pre-commit hooks and CI gates. The simplest pattern:
- pre-commit: run formatter, linter, and a lightweight AI-output checker
- push/PR CI: run full static analysis, run tests, validate metadata and prompt inputs
Example pre-commit hook (bash)
#!/bin/sh
# pre-commit hook: format, lint, and run ai-output checks
npm run format || exit 1
npm run lint || exit 1
# verify no secrets in staged files (simple grep-based check)
if git diff --cached --name-only | xargs grep -I --line-number -E "API_KEY|SECRET|PASSWORD"; then
echo "Potential secret found in staged files. Aborting commit." >&2
exit 1
fi
# ensure generated files have header
python3 tools/check_generated_headers.py || exit 1Testing generated code
Treat generated code as you would any other production code: tests must exist and be runnable by CI. If your AI system produces code automatically (not just suggestions), add a harness that generates code into a disposable workspace and runs the full test suite there.
Example test harness workflow (overview)
- Use a fixed prompt template from /ai-templates.
- Run the model to generate code into /tmp/workspace.
- Install dependencies and run lint + tests in that workspace.
- Fail the pipeline on lint/test errors and record generation inputs/outputs for auditing.
Metadata for generated files
Include a standard header block so reviewers and tooling can quickly tell what is generated and what prompt produced it. Example header:
/*
Generated-by: ai-codegen/1.0
Template: node-api-endpoint
Prompt-hash: 7a3b2f
Generated-at: 2026-04-01T12:00:00Z
*/Tradeoffs and practical concerns
- Overhead: Adding rules and CI checks increases friction; start small and iterate.
- False positives: Grep-based secret detection will flag benign patterns. Tune and whitelist carefully.
- Model drift: As models change, generated style and quality change. Re-evaluate templates periodically.
- Privacy and IP: Decide whether prompts or outputs are stored long-term; minimize storage of sensitive data.
- Lock-in: Keep templates generic and model-agnostic where possible to avoid vendor lock-in.
Quick start checklist (first 2 weeks)
- Create /guidelines/README.md with the core principles listed above.
- Add 2–3 strict prompt templates to /ai-templates/ for common tasks.
- Implement a pre-commit hook that formats, lints, and checks for secrets.
- Add one CI job that runs generate->test for one canonical template.
- Tag generated files with metadata and record generation inputs in a secure audit log.
Conclusion
Shared coding guidelines that encompass both human and AI contributors help teams keep quality, safety, and predictability as AI becomes part of the development stack. Start with a few enforceable rules, codify prompts and metadata, and automate checks in pre-commit and CI. Iterate based on real failures (lint/test errors, secret leaks, or audit findings) and balance rigour with developer productivity.
Practical next step: add a single 'require-generated-header' rule to your pre-commit flow and create one template for a common task — that small investment lets you measure the impact quickly.
Was this helpful?
Share this post
Comments (0)
Want to join the conversation?
Log in or sign up to leave a comment and share your thoughts.
Log in to Comment