Chapter 3: Best Practices and Limitations
Using AI Code Responsibly
AI code generation is powerful but requires responsible use. Understanding limitations and best practices ensures quality and security.
When to Trust AI Code
Higher Confidence ✅
- Common patterns (CRUD operations)
- Well-documented libraries
- Standard algorithms
- Boilerplate code
- Simple utility functions
Lower Confidence ⚠️
- Security-critical code
- Complex business logic
- Concurrency/threading
- Edge cases
- Performance-critical sections
Security Concerns
Common Issues
Examples to Watch For
SQL Injection Risk:
# AI might generate:
query = f"SELECT * FROM users WHERE id = {user_id}" # UNSAFE!
# Should be:
query = "SELECT * FROM users WHERE id = %s" # Parameterized
cursor.execute(query, (user_id,))
Hardcoded Secrets:
# AI might include:
api_key = "sk-abc123..." # NEVER commit secrets!
Security Best Practices
- Review all security-related code
- Never commit secrets
- Run security linters
- Test edge cases manually
- Use security scanning tools
Code Review Importance
Always Review AI Code For:
- Logic errors
- Edge cases
- Security vulnerabilities
- Performance issues
- Code style consistency
- Test coverage
Review Process
Licensing and Attribution
Potential Issues
- AI may reproduce training code
- License compatibility concerns
- Attribution requirements
Best Practices
- Don't use for copyrighted code
- Check unique/critical functions
- Document AI assistance if required
- Follow company policies
Limitations
What AI Code Generation Can't Do Well
| Limitation | Example |
|---|---|
| Novel algorithms | New research implementations |
| Domain expertise | Industry-specific rules |
| Perfect optimization | Performance-critical paths |
| Understanding intent | Ambiguous requirements |
| Maintaining state | Complex stateful systems |
Signs AI Code Needs Work
- Overly complex solutions
- Unnecessary dependencies
- Copy-paste patterns
- Missing error handling
- Inconsistent naming
Productivity Balance
AI Enhances:
- Speed of initial implementation
- Boilerplate reduction
- Learning new technologies
- Exploring solutions
Human Essential For:
- Architecture decisions
- Code review
- Security verification
- Performance tuning
- Domain logic
Team Guidelines
Recommended Policies
- Disclosure: Note significant AI assistance
- Review Requirements: All AI code must be reviewed
- Security Rules: No AI for auth/crypto without expert review
- Testing Standards: AI code needs tests
- Training: Team knows limitations
Avoiding Over-Reliance
Healthy Habits
- Understand generated code fully
- Write critical code yourself
- Use AI for learning, not avoiding learning
- Regular coding without AI
- Stay current with fundamentals
Warning Signs
- Can't code without AI
- Accepting without understanding
- Skipping reviews "because AI wrote it"
- No longer learning fundamentals
Key Takeaways
✅ Always review AI-generated code
✅ Security code requires extra scrutiny
✅ AI excels at common patterns, struggles with edge cases
✅ Human judgment remains essential
✅ Maintain coding skills independent of AI
✅ Establish team guidelines for AI use
Module Summary
You've completed Module 7. You understand:
- Major Tools: Copilot, Cursor, Codeium, others
- Effective Use: Completion, generation, debugging
- Responsible Use: Security, review, limitations
Next Steps
Take the Module 7 Quiz, then continue to Module 8: Multimodal AI.