GitHub Copilot - Patterns & Exercises
GitHub 🌟
en 🇬🇧
en 🇬🇧
  • Introduction
  • Contributing to the Project
  • General
    • Code completion
    • Comment to code
    • Code to comment
    • Quick Q&A
    • Regular expression
    • Language translation
    • Type hinting
    • Code to document
    • Object generation from structured data
    • Showing examples
  • Client Side Tips
    • Copilot snnipet handling
    • GitHub Copilot Shortcuts
    • Go to definition
    • Pin the files you need
  • Design Patterns
    • AI readable naming convention
    • Consistent coding style
    • High-level architecture first
    • Working on small chunks
    • Context-less Architecture
    • Eliminating a tiny OSS dependency
  • Collaboration
    • AI friendly documentation
    • Coaching on prompts
  • Test
    • Creating unit tests
    • Specify how to generate test code
    • Writing failure case first
    • Writing test cases in natural language first
    • Test only what is necessary
  • Refactoring
    • Writing test code before refactoring
    • Making the calculation part independent
    • Asking with open-ended questions
  • Archived
    • GitHub Copilot Patterns & Exercises Guide
    • Translations
      • German 🇩🇪
      • Spanish 🇪🇸
      • French 🇫🇷
      • Italy 🇮🇹
      • Japanese 🇯🇵
      • Portuguese 🇵🇹
      • Chinese 🇨🇳
Powered by GitBook
On this page
  • Description
  • Example
  • Exerecise
  • Checklist for Further Learning
Edit on GitHub
  1. General

Comment to code

Generate code from comments with GitHub Copilot

Last updated 1 year ago

Description

GitHub Copilot is capable of generating new code based on the specific text provided by a developer. By defining conditions in the form of comments, GitHub Copilot can create code that responds to the requirements.

Example

Here's a way to instruct GitHub Copilot to create a function through comments:

// Function name: calculateAverage
// Function arguments: numbers (array)
// Return type of the function: number

Based on these comments, Copilot might suggest the following code:

function calculateAverage(numbers: number[]): number {
    // calculate the average of the array
    const sum = numbers.reduce((a, b) => a + b);
    return sum / numbers.length;
}

Exerecise

  • Exercise 1: Write the comments to instruct Copilot to create a function that calculates the maximum number in an array. Use the following specification:

// Function name: calculateMax
// Function arguments: numbers (array)
// Return type of the function: number
  • Exercise 2: Test the generated function with different sets of numbers and verify if it returns the correct maximum value.

Checklist for Further Learning

  • Is the generated code based on the specified conditions?

  • Is the functionality of the code correctly implemented?

  • What is the appropriate way to write comments for more complex condition definitions?

  • To give more context, try using the OS dictation functionality as well as the keyboard.