DEV Community

Cover image for Golden-Rules-To-One-Shot-Projects-with-AI
Clifford Otieno
Clifford Otieno

Posted on

Golden-Rules-To-One-Shot-Projects-with-AI

Tired of AI coders wasting tokens, hallucinating, or blindly following instructions without considering consequences? This guide empowers programmers and coders to effectively utilize AI agents, ensuring high accuracy, efficient debugging, and seamless integration for project success with minimal corrections and maximum efficiency.

Getting started

  1. Install your preferred AI agent. We recommend Trae AI for Windows/Mac operating systems or GitHub Copilot for their advanced language models and code suggestions.
  2. Start a new project or open an existing one in your code editor.
  3. Configure your AI agent with the project's requirements and dependencies. This setup varies by agent; for instance, GitHub Copilot uses github-instructions.md to specify project dependencies.
  4. Begin using your AI agent to assist with coding tasks. You can prompt it to generate code, suggest modifications, fix errors, or explain existing code.

Things to note

A. RULESET

The recipe to influencing AI accuracy is the precision of the instructions provided. More detailed and explicit instructions lead to higher accuracy in the AI's responses.

Utilizing a dedicated ruleset helps maintain control over the AI, preventing unwanted edits.
Different AIs offer various methods for defining these rulesets:

  • GitHub Copilot uses the github-instructions.md file to specify project dependencies.
  • Trae AI uses the project-rules.md file for project-specific rules.
  • Most other AIs allow configuration through their settings .

B. CONTEXT LENGTH

AI models have a limited context length. This means if a codebase is 2000 lines long but the AI can only process up to 500 lines, it may time out or provide poor responses.

What to do:

  • Keep your code modular and short(about 500 lines).
  • Organize your code effectively, avoiding filling single files with excessive, unrelated functionalities.

C. CHAT DURATION

This involves a trade-off: longer chat durations generally lead to more accurate AI responses. However, excessively long conversations can cause the AI to lose context, necessitating a restart.

What to do:

  • After successfully implementing a feature or functionality, always begin a new chat session.
  • Commit frequently after each successful step.
  • If the AI consistently makes incorrect changes, revert to a previous state or start a new session with clearer instructions.

Quick Start

Check out this repository for a template and instructions to get started .

1. Core AI Principles

The following are example directives to give the AI to follow for utmost accuracy.

1.1. Accuracy & Precision

  • Understand Goal: Fully grasp user requests and project objectives. Ask clarifying questions if unsure.
  • Use Context: Leverage all available information (code, files, interactions) for decisions.
  • Verify: Confirm assumptions and solutions against existing code and requirements.

1.2. Debugging & Error Handling

  • Root Cause: Address the source of errors, not just symptoms.
  • Logging: Implement clear logging for debugging.
  • Testing: Use tests to find errors early and cover edge cases.
  • Prevent Errors: Anticipate and handle potential failure points.

1.3. Suggesting Changes

  • Assess Impact: Evaluate changes on project dependencies, performance, and maintenance.
  • Justify: Clearly explain proposed changes, benefits, and trade-offs.
  • Refine: Be open to feedback and new information for optimal solutions.

1.4. Integration

  • Check Interrelated Code: Ensure new code integrates smoothly with existing components.
  • Manage Dependencies: Be aware of libraries and frameworks; avoid conflicts.
  • API Consistency: Maintain consistency with existing APIs.

1.5. Staying Current & Avoiding Duplication

  • Latest Updates: Always check for recent file changes before modifying.
  • No Duplication: Reuse existing functions and logic.

1.6. Project Structure

  • Maintain Integrity: Don't change core project structure unless instructed.
  • Conventions: Follow established naming conventions.

1.7. Code Quality & Review

  • Check Syntax: Rigorously check for errors and incomplete code.
  • Readability: Write clean, well-commented, understandable code.
  • Consistency: Adhere to project's code style. Avoid inline comments unless standard.
  • Naming: Use meaningful variable names.
  • Self-Correction: Fix issues immediately until tests pass.

2. Project Setup

2.1. Define Scope

  • Clearly define project purpose and structure.
  • For unknown structures, ask AI to create a basic project structure with scalability as the core concern.

2.2. Separate Concerns

  • Clearly define frontend, backend, or server locations.

3. Development Workflow

  • Write tests (before or after).
  • Commit small, focused changes.
  • Document as you go.
  • Use meaningful commit messages.

4. AI Interaction

  • Ask specific questions.
  • Break down complex tasks.
  • Validate AI-generated code.
  • Review all suggested changes.
  • Keep context window focused.

5. Code Quality

  • Consistent coding style.
  • Follow SOLID principles.
  • Small, focused functions.
  • Meaningful variable names.
  • Comment complex logic.

6. Testing & Documentation

  • Write unit tests.
  • Document API endpoints.
  • Include setup instructions.
  • Maintain updated README.

7. Version Control

  • Use feature branches.
  • Regular commits.
  • Meaningful commit messages.
  • Pull request reviews.

8. Error Handling

  • Handle edge cases.
  • Log errors appropriately.
  • Provide meaningful error messages.
  • Graceful Degradation: Allow applications to fail gracefully with informative messages.
  • Centralized Logging: Use consistent error logging with context.
  • Specific Exceptions: Use precise exception types.

9. Security

  • Sanitize user input.
  • Secure API endpoints.
  • Handle sensitive data carefully.
  • Regular dependency updates.

10. Documentation

  • README: Keep README.md updated with setup, usage, and contribution instructions.
  • Comments: Explain why code does something, not just what. Keep them updated and short.
  • API Docs: Document all public APIs, functions, and classes.
  • Design Docs: Create for larger features or architectural changes.
  • Project Details: Suggest updates for missing project details in documentation.
  • Usage/Setup: Ensure documentation clearly explains how to use and set up the project.

11. Code Style & Conventions

  • Consistency: Adhere to a consistent coding style.
  • Linters/Formatters: Use automated tools (ESLint, Prettier) to enforce style.

11.1. Naming Conventions

  • Descriptive: Use clear names for variables, functions, classes, and files.
  • Case: Follow language-specific case conventions (e.g., camelCase, snake_case).
  • No Abbreviations: Avoid excessive abbreviations.

11.2. Comments

  • Why, Not What: Explain the reason for the code.
  • Updated: Keep comments current with code changes.
  • TODOs/FIXMEs: Use for planned work or known issues.

11.3. Modularity

  • Separation of Concerns: Design components with distinct responsibilities.
  • Loose Coupling: Minimize dependencies between modules.
  • Short Files: Limit code files to under 500 lines for readability.

12. Project-Specific Rules

  • Tech Stack: Define and adhere to chosen technologies.
  • Dependency Management: Establish guidelines for managing dependencies.
  • Configuration: Define how settings are managed and accessed.

13. Code Review

  • Mandatory: All code changes require review by another team member.
  • Checklist: Use a checklist for consistency and key areas.
  • Constructive Feedback: Provide actionable feedback focused on the code.

By following these principles, AI will operate responsibly, leading to accurate, robust, and successful project outcomes with minimal intervention.

Top comments (0)