-
Notifications
You must be signed in to change notification settings - Fork 78
Cody prompting guide #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Cody prompting guide #367
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
55ef4dd
cody prompting guide
chris-sev 9b12e7d
Apply suggestions from code review
chris-sev 1ac1c96
removing conclusion heading
chris-sev e006efd
Merge branch 'main' into prompting-guide
chris-sev 47f11e8
Merge branch 'main' into prompting-guide
MaedahBatool File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| # Cody Prompting Guide | ||
|
|
||
| In this guide, we'll explore the best practices and tips for effective techniques to maximize your Cody experience. | ||
|
|
||
| ## Overarching Recommendation: Treat Cody Like a New Team Member | ||
|
|
||
| When collaborating with Sourcegraph Cody, it's helpful to approach it as if you were working with a new engineer who is unfamiliar with your project. Just as you would provide a new team member with detailed information and context about the codebase and the tasks at hand, you should do the same with Cody. | ||
|
|
||
| Always lean towards providing more specific information about a certain task to ensure Cody understands your requirements clearly. The more context and details you provide, the better Cody can assist you in generating relevant and accurate code. | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Consider the following examples: | ||
|
|
||
| ❌ Instead of a vague prompt like: | ||
|
|
||
| ``` | ||
| How can I filter products in JavaScript? | ||
| ``` | ||
|
|
||
| ✅ Provide a more specific prompt with details: | ||
|
|
||
| ``` | ||
| I have an array of product objects in JavaScript, where each object has the following properties: id, name, category, and price. How can I write a function to filter the products by category? | ||
| ``` | ||
|
|
||
| By treating Cody as a new team member and providing detailed information, you'll establish a more effective collaboration and get better results. | ||
|
|
||
| We will split this guide into two parts: Preparation and Prompting. | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - Preparation: This part will focus on preparing your code for Cody. | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - Prompting: This part will focus on crafting effective prompts for Cody. | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Part 1: Preparation | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Before diving into prompting techniques, it's crucial to set the stage for a successful collaboration with Cody. Here are some key preparation steps: | ||
|
|
||
| ### Choose Descriptive VariNames | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Using clear and descriptive names for variables, functions, and classes is essential for making your code readable and understandable, not only for yourself but also for Cody. Avoid abbreviations or ambiguous names that may confuse your AI assistant. | ||
|
|
||
| ✅ Good example: | ||
|
|
||
| ```php | ||
| function calculateTotalPrice($items, $taxRate) { | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| ❌ Bad example: | ||
|
|
||
| ```php | ||
| function calc($i, $t) { | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| ### Write Clear Comments and Docstrings | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| In addition to variable names, comments and docstrings play a crucial role in guiding Cody's understanding of your code. Treat them as a way to communicate with Cody, just like you would with a new engineer. Explain complex logic, algorithms, or project-specific concepts to give Cody the necessary context. | ||
|
|
||
| ✅ Good example: | ||
|
|
||
| ```javascript | ||
| /** | ||
| * Calculate the shipping cost based on the order total. | ||
| * - For orders under $50, the shipping cost is $5. | ||
| * - For orders between $50 and $100, the shipping cost is $10. | ||
| * - For orders above $100, shipping is free. | ||
| * | ||
| * @param {number} orderTotal - The total amount of the order. | ||
| * @returns {number} The shipping cost, determined by the following rules: | ||
| */ | ||
| function calculateShippingCost(orderTotal) { | ||
| // Cody will autocomplete here | ||
| } | ||
| ``` | ||
|
|
||
| A bonus here is that Cody can generate these docstrings for you, so you don't have to write them manually. You can use the **Document Code** command to do this. | ||
|
|
||
| IMAGE HERE: cody-document-code.jpg | ||
|
|
||
| ## Part 2: Prompting | ||
|
|
||
| Now that your code is well-prepared, it's time to focus on crafting effective prompts for Cody. Consider the following best practices: | ||
|
|
||
| ### Provide Specific Information | ||
|
|
||
| When asking Cody for assistance, provide as much detail as possible. Include information about the problem, expected behavior, constraints, and any project-specific requirements. | ||
|
|
||
| Be sure to include comprehensive details about the problem, what you expect to happen, any limitations, and specific requirements related to your project. | ||
|
|
||
| ❌ Bad example: | ||
|
|
||
| ``` | ||
| How do I calculate discounts based on loyalty points in Laravel? | ||
| ``` | ||
|
|
||
| ✅ Good example: | ||
|
|
||
| ``` | ||
| I need to calculate discounts based on customer loyalty points. | ||
|
|
||
| - If the customer has loyalty points above 500, apply a 10% discount. | ||
| - If the customer has loyalty points between 200 and 500, apply a 5% discount. | ||
| - If the customer has loyalty points below 200, no discount is applied. | ||
|
|
||
| Create a function that takes the total amount and loyalty points as input and returns an object with the discount percentage and discount amount. | ||
| ``` | ||
|
|
||
| ### Provide Specific Context | ||
|
|
||
| Providing detailed context is the most effective way to enhance Cody's assistance. Approach this as if you are explaining the situation to a new engineer. | ||
|
|
||
| You should: | ||
|
|
||
| - Reference important files and symbols | ||
| - Provide examples from other similar functions | ||
|
|
||
| To provide specific context from your codebase, you can use the `@-` mention feature to provide syntax. You can mention specific files, sections of code, and symbols to help Cody understand your code. | ||
|
|
||
| Here is an example of bringing in specific lines of code. The shortcut to do this is to highlight code and press `Opt+L` on Mac or `Ctrl+L` on Windows. | ||
|
|
||
| IMAGE HERE: cody-mention-snippet.jpg | ||
|
|
||
| Here is an example of `@-` mentioning a specific file: | ||
|
|
||
| IMAGE HERE: cody-mention-file.jpg | ||
|
|
||
| ### Provide Examples and Test Cases | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Include examples and test cases when applicable to clarify your expectations. Demonstrate edge cases or handling of special conditions to guide Cody in generating robust code. | ||
|
|
||
| ❌ Bad example: | ||
|
|
||
| ``` | ||
| I need to validate email addresses in JavaScript | ||
| ``` | ||
|
|
||
| ✅ Good example: | ||
|
|
||
| ``` | ||
| Create a function to validate email addresses in JavaScript. Return true for valid email addresses and false for invalid ones. Here are some example test cases: | ||
|
|
||
| Valid: | ||
| - "[email protected]" | ||
| - "[email protected]" | ||
|
|
||
| Invalid: | ||
| - "john@example" | ||
| - "jane.doe@example." | ||
| - "invalid.email" | ||
|
|
||
| Please write the function | ||
| ``` | ||
|
|
||
| ### Iterate and Refine | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Start with a general prompt and incrementally add more details based on Cody's responses. Take advantage of the fact that you can chat with Cody. Bring the back and forth conversation, especially if you didn't like Cody's first response. Review the generated code or suggestions, provide feedback, and refine your prompts to get the desired results. | ||
|
|
||
| Initial prompt: | ||
|
|
||
| ``` | ||
| I need to calculate the total price of an order. Help me write the function | ||
| ``` | ||
|
|
||
| Refined prompt: | ||
|
|
||
| ``` | ||
| Thanks. I forgot to mention: | ||
|
|
||
| - The function should take an array of order items as input | ||
| - We need to apply a 10% discount if the total price exceeds $100 | ||
| - Final total should be rounded to two decimal places | ||
|
|
||
| Please update the function | ||
| ``` | ||
|
|
||
| ### Leverage Cody's Strengths | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Utilize Cody's capabilities for generating boilerplate code, common patterns, and repetitive tasks. Prompt it to assist with unit tests, documentation, and error handling to save time and ensure code quality. | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ✅ Good example: | ||
|
|
||
| ``` | ||
| Help me write tests for the `calculateAverageRating` function. Here are the test cases I have in mind: | ||
|
|
||
| - Empty ratings array should return 0 | ||
| - Array with one rating should return that rating | ||
| - Array with multiple ratings should calculate the average correctly | ||
|
|
||
| Make sure the tests cover any edge cases or potential issues. Thanks! | ||
| ``` | ||
|
|
||
| You can also use the **Generate Tests** command to generate tests for your code. | ||
|
|
||
| ## Conclusion | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| By following these best practices for preparation and prompting, you can effectively collaborate with Sourcegraph Cody and leverage its AI capabilities to boost your coding productivity. Remember, treating Cody like a new team member and providing detailed information is key to a successful collaboration. By following these best practices and examples, you'll be able to leverage Cody's AI capabilities effectively and streamline your coding workflow. | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Happy coding with Cody! | ||
chris-sev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.