Identify duplicate code using GitHub Copilot

Completed

GitHub Copilot is an AI-powered tool that assists developers by providing code suggestions and answering questions. It can analyze code, identify patterns, and offer insights to improve code quality. By using GitHub Copilot, developers can streamline their workflow and enhance productivity.

GitHub Copilot operates in different modes, each tailored to specific tasks.

  • Ask Mode: Designed for querying and understanding code without making changes.
  • Edit Mode: Allows GitHub Copilot to suggest code edits and modifications directly in the editor.
  • Agent Mode: Enables GitHub Copilot to perform complex tasks by executing a series of actions based on user instructions.

Why use Ask mode?

In Ask mode, GitHub Copilot analyzes the codebase and answers questions without altering the code. Developers can use Ask mode to gain insights into the code, understand its structure, identify potential issues, and explain how different parts of the codebase interact

Identifying duplicate code with Ask mode

Ask mode can be a powerful tool for identifying duplicate code. Developers can ask specific questions to pinpoint areas of duplication. For example: "Review the class file open in the editor. Which functions have similar logic? Explain the opportunities to consolidate any duplicate code." GitHub Copilot analyzes the code, identifies potential instances of duplicate code, and explains the options for consolidation/refactoring.

Consider the following strategies when using Ask mode to identify duplicate code:

  • Comparative questions: For example, "Compare class A and class B for similar methods." GitHub Copilot can outline differences and similarities.
  • Code search queries: For example, "Where else in this project is CalculateShipping defined?" GitHub Copilot can quickly identify all instances of any search term, such as method names, variable names, code comments, or specific logic patterns.
  • Understanding output: If a project generates verbose logs, you could ask GitHub Copilot to review associated code. For example, "I see both OrderProcessor and ReturnProcessor printing ‘Calculating shipping…’. Is that code duplicated?" Copilot can connect the dots between log output and code.

Interpreting Copilot’s Answers

When using Ask mode, it's important to critically read and interpret the responses generated by GitHub Copilot. Developers should verify the suggestions provided by GitHub Copilot and ensure that they align with the overall code structure and design principles. By combining GitHub Copilot’s insights with their own expertise, developers can make informed decisions about consolidating duplicate code.

Consider the following guidelines:

  • If GitHub Copilot says two methods are similar, it’s probably right – but verify by looking at those methods or asking follow-up questions.
  • If GitHub Copilot misses something you strongly suspect, try rephrasing the question or opening the relevant files to give it more context.

Beyond Duplication – Other Ask Mode Uses

While Ask mode is valuable for identifying duplicate code, it can also be used for other purposes. Developers can ask questions about code functionality, dependencies, and best practices. Copilot Chat can provide insights into code optimization, performance improvements, and potential refactoring opportunities. By using Ask mode, developers can gain a deeper understanding of their codebase and improve overall code quality.

Summary

GitHub Copilot's Ask mode is a powerful tool for identifying duplicate code and gaining insights into code quality. By asking targeted questions, developers can use GitHub Copilot's analysis to pinpoint duplication, understand code structure, and explore refactoring opportunities. Critical evaluation of GitHub Copilot's responses ensures that developers make informed decisions about consolidating duplicate code and improving their codebase.