ad
ad

Using the new VS Code GitHub Copilot "Code Review"

Science & Technology


Introduction

As a seasoned Microsoft MVP with 16 awards under my belt, I'm excited to showcase a newly introduced capability of GitHub Copilot in Visual Studio Code: the code review feature. This feature simplifies the process of generating and refining code through AI-driven suggestions, and it can substantially enhance your development workflow. Let’s dive into a practical demonstration of how this works.

Step 1: Generating Code

To kick things off, I started with a comment in a PowerShell file, stating my need for a function to find the largest number in an array. This initial step encouraged the AI model to generate code based on the comment provided.

## Introduction
function Get-LargestNumber($numbers) (
    # Function implementation will go here
)

After running the AI suggestion, I accepted the code by hitting the ‘Tab’ key to integrate it into my file. The output seemed generally correct, although I hadn’t performed any unit tests yet.

Step 2: Initiating the Code Review

Next, I selected all the generated code, right-clicked to access the context menu, and opted for the Copilot feature to review and comment on the code. The AI provided four insightful suggestions related to the code I had written.

Suggestions Provided:

  1. Parameter Naming: The suggestion was to modify the function name for greater descriptiveness.
  2. Camel Case for Parameter Names: It pointed out the need to use camel casing for the parameter name.
  3. Handling Empty Arrays: The AI requested adding a check to manage the scenario where the array might be empty.
  4. Output Method Recommendation: It suggested replacing ‘return’ with ‘write output’ for better clarity and consistency.

Step 3: Implementing the Suggestions

I proceeded to review each suggestion, applying them one by one:

  1. Improved Function Naming: The parameter name was changed from numbers to fromArray for clearer communication of its purpose.
  2. Camel Case Check: I accepted the recommendation to update the parameter name to adhere to camel case standards.
  3. Empty Array Handling: I added a conditional check to handle cases when the array could be empty, returning null as needed.
  4. Output Method Adjustment: I changed the output method to write output for improved readability in line with the suggestion.

This demonstration emphasized how a simple comment led to the generation of a functional piece of code, followed by an AI-driven review that guided further refinements, ultimately enhancing code quality and maintainability.

This code review feature works exceptionally well even with larger scripts, allowing you to select segments of code for assessment. Moreover, it supports deeper reviews across multiple files before committing changes, ensuring comprehensive quality checks.

Conclusion

Overall, the GitHub Copilot code review feature streamlines the process of writing and refining code efficiently, making it an invaluable tool for developers seeking to elevate their coding practices.


Keywords

  • GitHub Copilot
  • Visual Studio Code
  • Code Review
  • AI-driven Suggestions
  • PowerShell
  • Function Naming
  • Camel Case
  • Empty Arrays
  • Code Quality

FAQ

What is GitHub Copilot?

GitHub Copilot is an AI-powered code completion tool that assists developers in writing code by suggesting whole lines or blocks of code as they type.

How does the code review feature work?

The code review feature in GitHub Copilot analyzes your code and provides suggestions for improvements in areas like naming conventions, handling edge cases, and overall code readability.

Can GitHub Copilot work with large code files?

Yes, GitHub Copilot can work on large script files. You can review segments of code or conduct comprehensive reviews across multiple files.

Is it necessary to implement all suggestions provided by Copilot?

While the suggestions offered by Copilot enhance code quality, it is ultimately up to the developer to decide which amendments to implement based on their specific use case and coding standards.

Are there any limitations to using Copilot?

GitHub Copilot's suggestions may not always be accurate or optimal, so it acts as an assistant rather than a replacement for a programmer’s judgment and expertise.