Prompt Engineering Step 2: Specifying Constraints (Copilot)
Help GitHub Copilot generate more suitable code by clearly stating limitations, requirements, and standards in your prompts.
How to Specify Constraints
Include constraints directly in your natural language prompts within Copilot Chat or in comments intended to trigger inline suggestions.
Examples of Constraints:
- Language Version/Features: "Generate a Python function using features available in version 3.9." / "Write JavaScript using ES6 classes."
- Library/Framework Usage: "Implement the API call using the native 'fetch' API, do not use 'axios'." / "Create a React component using only functional components and hooks."
- Coding Standards/Style: "Refactor
#selection
to follow PEP 8 guidelines." / "Add JSDoc comments that adhere to the Google Style Guide."
- Performance: "Optimize this function for speed, prioritizing lower time complexity."
- Things to Avoid: "Generate the setup code, but avoid using third-party libraries for configuration."
Example Prompt (Copilot Chat):
Refactor the selected C# code (#selection
) to use LINQ expressions where appropriate, targeting .NET 6. Ensure no external libraries are introduced.
Why Constraints Matter
- Reduces Ambiguity: Helps Copilot understand the specific requirements beyond just the basic functionality.
- Improves Relevance: Ensures suggestions fit within project standards, specific framework versions, or performance needs.
- Saves Time: Reduces the need for significant refactoring of generated code that doesn't meet implicit requirements.
Key Takeaway
Be explicit about technical requirements, libraries to use or avoid, style guides, and target versions directly in your prompts to guide Copilot effectively.