Iron Academy Logo
C# and AI

Copilot C# Tutorial: Revolutionizing Renaming in Visual Studio with AI

GitHub Copilot revolutionizes coding by providing context-aware code suggestions and snippets directly in editors like Visual Studio Code and Visual Studio. Leveraging generative AI, Copilot in Visual Studio assists with repetitive tasks, offers function signatures, and supports numerous programming languages, including JavaScript. By installing the GitHub Copilot plugin, developers can access real-time assistance through a chat interface, making code writing more efficient and intuitive.

Finding the perfect name for a variable or class in your code can be challenging. Visual Studio's integration with GitHub Copilot Chat introduces an innovative AI-powered renaming tool "Copilot Chat", as explained in Tim Corey's video tutorial, "Renaming in Visual Studio Using AI." In this article, we'll explore how to enable and utilize this feature effectively using GitHub copilot extension, insights from Tim Corey's video.

Introduction

GitHub Copilot offers a wide range of features that significantly enhance the development process. It provides intelligent code completions based on context, including function signatures and code snippets, which streamline coding and reduce errors. The tool also aids in bug detection and suggests fixes, improving code reliability. For developers working with multiple languages, Copilot can translate code from one programming language to another, and it can generate comments and documentation, making the code more understandable and maintainable.

It offers intelligent code completions, suggested code snippets, and real-time assistance, making writing code more efficient and intuitive. By leveraging publicly available code, Copilot provides context-aware suggestions to automate repetitive tasks and improve code quality. The tool is easily accessible through a GitHub account, and its features are activated via the Copilot icon and chat interface within the editor environment. This innovative assistant transforms the coding experience, helping developers enhance productivity and streamline their workflows.

In his video (0:00), Tim Corey begins by addressing the common struggle developers face when naming variables and classes especially when working in JavaScript file with var and let variable types. He introduces the AI-powered rename tool available in Visual Studio environment version 17.9, which provides context-aware suggestions to streamline the renaming process. The latest version as of now is 17.10.3.

Enabling the Rename Feature

Before diving into the tool's functionalities, it's crucial to ensure the feature is enabled in Visual Studio or Visual Studio code. To start using GitHub Copilot, first open Visual Studio Code or Visual Studio and install the GitHub Copilot extension from the marketplace or extensions respectively. (Image much better?)

GitHub account is required to use GitHub Copilot. To access its features, you need to sign in with your GitHub account and install the GitHub Copilot extension or plugin in your code editor, such as Visual Studio Code or Visual Studio. This integration allows Copilot to provide personalized code suggestions and other functionalities based on your coding context and preferences.

Once it is installed, let's now have a look at the step-by-step process that Tim demonstrate in his video:

  1. Access the Options Menu: Navigate to Tools > Options in Visual Studio (1:21): (Image quality low resolution?)

  2. Locate GitHub Copilot: Under the Environment section, find GitHub (1:25) and select Copilot chat interface: (Image quality & info can be displayed better?)

  3. Enable Rename Suggestions: Check the box labeled Enable rename suggestions to activate the AI-powered renaming feature (1:32).(Image quality & info can be displayed better?)

Using the Rename Tool

With the feature enabled, Tim showcases how to use it through a practical example give in the below code snippet image. He starts with a Person model instantiated with the variable P, which is not an ideal name. By right-clicking the variable and selecting Rename, he accesses the AI suggestions. A small icon appears, indicating the AI's involvement in generating context-aware names (2:00).

Context-Aware Suggestions

Tim highlights how the AI provides a list of relevant suggestions, such as Person, PersonData, PersonInfo, PersonDetails, and PersonProfile. These suggestions are tailored based on the context of the code, making them more accurate and useful (2:45). (Image quality & info can be better?)

Refining Suggestions

One of the advantages of this tool is the ability to refine AI suggestions. Tim demonstrates this by selecting a suggested name and further customizing it to better fit his needs. He renames a variable to userProfile but decides to adjust it further to userProfileInfo (3:31). This flexibility ensures that developers can start with a solid AI-generated name and tweak and modify it as necessary.

Renaming Classes

The AI-powered rename tool isn't limited to variables; it also works for class names. Tim illustrates this by renaming a Demo Class that contains properties like ID, Title, Author, and YearPublished. The AI suggests names such as Book, Publication, Literature, WrittenWork, and PublicationDetail. Tim selects Publication, and then further enhances it by setting it to PublicationModel which better describes the class's purpose and functionality (4:05). This also renames the class name which is awesome.(Image quality & info can be displayed better?)

Benefits and Considerations

Tim concludes the tutorial by emphasizing the benefits of using AI for renaming:

  • Efficiency: The tool saves time by providing relevant suggestions quickly.

  • Context-Awareness: AI-generated names are based on the context, making them more appropriate.

  • Flexibility: Developers can start with AI suggestions and customize them as needed.

However, he also notes that AI is not infallible and may occasionally produce unexpected results. Therefore, while AI suggestions are a great starting point, developers should always review and refine the names to ensure they fit the intended purpose (4:47).

Conclusion

The AI-powered rename tool in Visual Studio, integrated with GitHub Copilot Chat, is a valuable addition to any developer's toolkit. By following Tim Corey's tutorial, you can enable and effectively use this feature to enhance your coding efficiency and maintain cleaner, more readable code. For a quick introduction and practical demonstration, be sure to watch Tim Corey's video, "Renaming in Visual Studio Using AI," available on his YouTube channel.