1+ <ai_code_generation_best_practices >
2+ <overview >
3+ Best practices for AI-assisted code generation based on Google GenAI guidelines
4+ and industry standards for producing high-quality, maintainable code.
5+ </overview >
6+
7+ <general_principles >
8+ <principle priority =" critical" >
9+ <name >Code Clarity and Readability</name >
10+ <description >Generate code that is self-documenting and easy to understand</description >
11+ <rationale >Clear code reduces maintenance burden and improves team productivity</rationale >
12+ <guidelines >
13+ <guideline >Use descriptive variable and function names</guideline >
14+ <guideline >Keep functions focused on a single responsibility</guideline >
15+ <guideline >Add comments for complex business logic</guideline >
16+ <guideline >Follow consistent formatting and style conventions</guideline >
17+ </guidelines >
18+ </principle >
19+
20+ <principle priority =" critical" >
21+ <name >Defensive Programming</name >
22+ <description >Generate code that handles errors gracefully and validates inputs</description >
23+ <rationale >Robust error handling prevents runtime failures and improves user experience</rationale >
24+ <guidelines >
25+ <guideline >Validate all inputs at function boundaries</guideline >
26+ <guideline >Use appropriate error handling mechanisms for the language</guideline >
27+ <guideline >Provide meaningful error messages</guideline >
28+ <guideline >Handle edge cases explicitly</guideline >
29+ </guidelines >
30+ </principle >
31+
32+ <principle priority =" high" >
33+ <name >Test-Driven Development</name >
34+ <description >Generate comprehensive tests alongside implementation code</description >
35+ <rationale >Tests ensure correctness and enable safe refactoring</rationale >
36+ <guidelines >
37+ <guideline >Write tests for all public interfaces</guideline >
38+ <guideline >Test both success and failure scenarios</guideline >
39+ <guideline >Use meaningful test descriptions</guideline >
40+ <guideline >Ensure tests are deterministic and isolated</guideline >
41+ </guidelines >
42+ </principle >
43+ </general_principles >
44+
45+ <code_quality_standards >
46+ <standard category =" naming" >
47+ <rule >Use clear, descriptive names that express intent</rule >
48+ <examples >
49+ <good >calculateTotalPrice(items: Item[])</good >
50+ <good >isValidEmailAddress(email: string)</good >
51+ <bad >calc(x: any[])</bad >
52+ <bad >check(s: string)</bad >
53+ </examples >
54+ </standard >
55+
56+ <standard category =" functions" >
57+ <rule >Keep functions small and focused on a single responsibility</rule >
58+ <guidelines >
59+ <guideline >Aim for functions under 20-30 lines</guideline >
60+ <guideline >Extract complex logic into helper functions</guideline >
61+ <guideline >Use pure functions when possible</guideline >
62+ <guideline >Minimize side effects</guideline >
63+ </guidelines >
64+ </standard >
65+
66+ <standard category =" error_handling" >
67+ <rule >Implement comprehensive error handling</rule >
68+ <patterns >
69+ <pattern language =" typescript" >
70+ <description >Use Result types for operations that can fail</description >
71+ <example ><![CDATA[
72+ type Result<T, E> = { success: true; data: T } | { success: false; error: E };
73+
74+ function parseJson<T>(json: string): Result<T, string> {
75+ try {
76+ const data = JSON.parse(json) as T;
77+ return { success: true, data };
78+ } catch (error) {
79+ return { success: false, error: error.message };
80+ }
81+ }
82+ ]]> </example >
83+ </pattern >
84+ </patterns >
85+ </standard >
86+
87+ <standard category =" documentation" >
88+ <rule >Provide clear documentation for public APIs</rule >
89+ <requirements >
90+ <requirement >Document function parameters and return values</requirement >
91+ <requirement >Explain complex algorithms or business logic</requirement >
92+ <requirement >Provide usage examples for non-trivial functions</requirement >
93+ <requirement >Document any side effects or preconditions</requirement >
94+ </requirements >
95+ </standard >
96+ </code_quality_standards >
97+
98+ <security_guidelines >
99+ <guideline priority =" critical" >
100+ <rule >Validate and sanitize all user inputs</rule >
101+ <rationale >Prevents injection attacks and data corruption</rationale >
102+ <implementation >
103+ <step >Use type-safe validation libraries</step >
104+ <step >Sanitize inputs before processing</step >
105+ <step >Use parameterized queries for database operations</step >
106+ <step >Escape output when rendering to prevent XSS</step >
107+ </implementation >
108+ </guideline >
109+
110+ <guideline priority =" high" >
111+ <rule >Follow principle of least privilege</rule >
112+ <rationale >Minimizes potential damage from security breaches</rationale >
113+ <implementation >
114+ <step >Grant minimal necessary permissions</step >
115+ <step >Use role-based access control</step >
116+ <step >Validate authorization at each access point</step >
117+ <step >Log security-relevant events</step >
118+ </implementation >
119+ </guideline >
120+
121+ <guideline priority =" high" >
122+ <rule >Protect sensitive data</rule >
123+ <rationale >Prevents data breaches and maintains user privacy</rationale >
124+ <implementation >
125+ <step >Encrypt sensitive data at rest and in transit</step >
126+ <step >Use secure random number generation</step >
127+ <step >Implement proper session management</step >
128+ <step >Avoid logging sensitive information</step >
129+ </implementation >
130+ </guideline >
131+ </security_guidelines >
132+
133+ <performance_considerations >
134+ <consideration category =" algorithmic_efficiency" >
135+ <rule >Choose appropriate algorithms and data structures</rule >
136+ <guidelines >
137+ <guideline >Understand time and space complexity</guideline >
138+ <guideline >Use efficient data structures for the use case</guideline >
139+ <guideline >Avoid premature optimization</guideline >
140+ <guideline >Profile before optimizing</guideline >
141+ </guidelines >
142+ </consideration >
143+
144+ <consideration category =" resource_management" >
145+ <rule >Manage resources efficiently</rule >
146+ <guidelines >
147+ <guideline >Close resources properly (files, connections, etc.)</guideline >
148+ <guideline >Use connection pooling for database access</guideline >
149+ <guideline >Implement proper caching strategies</guideline >
150+ <guideline >Avoid memory leaks</guideline >
151+ </guidelines >
152+ </consideration >
153+ </performance_considerations >
154+
155+ <common_pitfalls >
156+ <pitfall >
157+ <description >Generating overly complex solutions</description >
158+ <why_problematic >Complex code is harder to understand, test, and maintain</why_problematic >
159+ <correct_approach >Start with simple solutions and refactor when complexity is justified</correct_approach >
160+ </pitfall >
161+
162+ <pitfall >
163+ <description >Ignoring existing codebase patterns</description >
164+ <why_problematic >Inconsistent patterns make the codebase harder to navigate</why_problematic >
165+ <correct_approach >Analyze existing code to understand and follow established patterns</correct_approach >
166+ </pitfall >
167+
168+ <pitfall >
169+ <description >Insufficient error handling</description >
170+ <why_problematic >Unhandled errors lead to poor user experience and debugging difficulties</why_problematic >
171+ <correct_approach >Implement comprehensive error handling for all failure modes</correct_approach >
172+ </pitfall >
173+
174+ <pitfall >
175+ <description >Missing or inadequate tests</description >
176+ <why_problematic >Untested code is prone to bugs and difficult to refactor safely</why_problematic >
177+ <correct_approach >Generate comprehensive test coverage alongside implementation</correct_approach >
178+ </pitfall >
179+ </common_pitfalls >
180+
181+ <quality_checklist >
182+ <category name =" before_implementation" >
183+ <item >Requirements are clearly understood</item >
184+ <item >Existing patterns and conventions are identified</item >
185+ <item >API design is planned and reviewed</item >
186+ <item >Testing strategy is defined</item >
187+ </category >
188+
189+ <category name =" during_implementation" >
190+ <item >Code follows established patterns</item >
191+ <item >Error handling is comprehensive</item >
192+ <item >Security considerations are addressed</item >
193+ <item >Performance implications are considered</item >
194+ </category >
195+
196+ <category name =" before_completion" >
197+ <item >All tests pass</item >
198+ <item >Code is properly documented</item >
199+ <item >Security review is completed</item >
200+ <item >Performance requirements are met</item >
201+ </category >
202+ </quality_checklist >
203+ </ai_code_generation_best_practices >
0 commit comments