AI Prompt Guides: Page 5 (Utilizing Copilot Prompts)

This page covers how to effectively use prompts for code-generating AIs like GitHub Copilot.


21. Code Autocompletion

Copilot understands the context of the code you're writing and automatically suggests the next line or block. Clearly define variable names, function calls, and comments to help Copilot provide more accurate suggestions.

Examples:

def calculate_area(radius):
    # Function to calculate the area of a circle using the radius
    return 3.14159 * radius * radius
# Now, using this function
area_of_circle = calculate_area(

(Copilot might suggest `10)` as the next input)


22. Function and Class Generation

When creating functions or classes for a specific purpose, writing clear comments about their functionality, required parameters, and return values helps Copilot suggest the entire structure.

Examples:

# Create a User class for a user management system
# Fields: id (int), username (str), email (str)
# Methods: display_info()
class User:

23. Prompting through Comments

Copilot heavily relies on comments as powerful prompts. Leaving comments that describe your code, the next task, or even ideas for algorithms or logic will guide Copilot to generate code based on those comments.

Examples:

# Write a Python function to find the largest number in an array (using linear search)
def find_max(arr):

24. Writing Test Cases

You can use Copilot to write test cases for your functions. By outlining the testing framework and basic test structure, you can prompt Copilot to suggest specific test cases.

Examples:

import unittest
# Write test cases for the calculate_area function
class TestCalculateArea(unittest.TestCase):
    def test_positive_radius(self):
        # Verify that the correct area is calculated when the radius is positive
        self.assertAlmostEqual(calculate_area(5), 78.53975)
    def test_zero_radius(self):
        # Verify that the area is 0 when the radius is 0
        self.assertEqual(calculate_area(0), 0)
    def test_negative_radius(self):
        # Verify that a ValueError is raised when the radius is negative

(Copilot might suggest `with self.assertRaises(ValueError): self.calculate_area(-1)` for the next line)


25. Refactoring and Improvements

When you want to improve code readability or performance, you can ask Copilot to refactor it. Select the specific code and indicate in comments how you want it improved.

Examples:

# Refactor this code to be more Pythonic and use a list comprehension
def filter_even_numbers(numbers):
    result = []
    for num in numbers:
        if num % 2 == 0:
            result.append(num)
    return result

(Copilot might suggest `return [num for num in numbers if num % 2 == 0]` as a shorter, more efficient alternative)