2025年8月31日日曜日

Prompt Engineering for Copilot Chat (VS Code)

 

Prompt engineering is about how you ask Copilot for help — better prompts = better responses.


✅ Key Strategies for Better Prompts

1. Provide Context

  • Open relevant files in VS Code.

  • Use top-level comments and descriptive function names.

  • Set includes/imports explicitly to guide Copilot's suggestions.

  • Use meaningful variable names and function scopes.


2. Use Chat Participants & Slash Commands

Chat Participants

  • @workspace — Ask questions about your open project.

  • @vscode — Ask about VS Code features/extensions.

  • @terminal — Run or explain shell commands.

Slash Commands

  • /explain — Explain code.

  • /fix — Suggest fixes.

  • /tests — Generate test cases.

  • /optimize — Suggest improvements.

🔹 Example:
/fix why is #auth.service.ts throwing "undefined" on line 24?


3. Add Specific Context with # Variables

Use #filename, #folder, or #symbol to focus on specific areas of your code.

🔹 Examples:

  • “Summarize the changes in #changes.”

  • “What does the function #getUserData do?”

  • “Improve logging in #server.js using Winston.”


4. Break Down Tasks

Avoid huge multi-step prompts. Instead, guide Copilot one step at a time.

🔹 Bad:

“Create an Express app with Pug, TypeScript, MongoDB, and a product page.”

🔹 Better:

  1. “Set up a basic Express app using TypeScript and Pug.”

  2. “Add a /products route that renders a view.”

  3. “Connect to MongoDB using Mongoose and fetch product data.”


5. Use High-Quality Example Code

  • Paste small, relevant examples to prime Copilot’s suggestions.

  • Use current or desired libraries in the snippet.

  • Delete the sample after Copilot “gets it.”


6. Iterate on Results

You can always ask for:

  • A different approach (“avoid recursion”)

  • Better naming (“use camelCase”)

  • Performance improvements

  • Alignment with your coding style

🔹 Prompt:

“Rewrite this function using a loop instead of recursion.”


7. Maintain Quality

  • Copilot mimics your code quality.

  • Clean, idiomatic code = better suggestions.

  • Consider disabling Copilot during “quick and dirty” coding.


📚 Example Prompts

TaskPrompt
Add a route“Add a POST /products route in #api.ts using Express.”
Refactor code“Refactor #invoice.js to remove duplicate logic.”
Debug“/fix error in #auth.service.ts on line 24”
Learn code“/explain how JWT validation works in #auth.ts”
Generate tests“/tests for validateInput in #form-utils.js”

💬 Quick Reference: Prompt Formula

Intent + Context + Constraint

🧾 Example:

“Create a pagination helper in #utils.js that limits to 10 items per page using plain JavaScript.”


🔗 Learn More

0 件のコメント:

コメントを投稿