2.1 AI-Assisted Software Development#

In this section, we will explore how to develop software with the help of an AI assistan. One of the things I like the most about AI assisted software development is the fact that it allows me to stay in the flow. I can focus on the problem I am trying to solve and let the AI take care of syntax details, the boilerplate code, remebering conventions, generating solutions to simpler intermediate problems.

Before jumping into it, let’s check the prerequisites.

Installation and Configuration#

We will use two Visual Studio Code extensions: GitHub Copilot and Github Copilot Chat.

  • GitHub Copilot is a code completion tool that uses AI to provide suggestions for code completion, inline documentation, and more.

  • GitHub Copilot Chat is an extension that allows you to interact with GitHub Copilot using a chat interface.

Getting access to GitHub Copilot#

To follow along with this section, you will need a Github account with access to the GitHub Copilot Individual or Business plan. If you activated your Student Developer Pack, you can access the GitHub Copilot Individual plan for free. Note that it usually takes a few days for the activation to be completed.

Installing the GitHub Copilot and Github Copilot Chat extensions#

You can install both extensions by searching for them in the Visual Studio Code Extensions Marketplace. To make sure that you install the correct extensions, you can install them from the terminal by following these steps:

  1. Open the terminal.

  2. Run the following commands:

code --install-extension github.copilot
code --install-extension github.copilot-chat

Once the extensions are installed, you will see a pop up message asking you to sign in to use Github Copilot. Once you sign in, you will see that a Copilot icon appears in the bottom right corner of the screen. This means that you are able to use the extensions and you are ready to go.

Keep in mind

  • The results you get from the AI are not deterministic and depend on the context and the prompt you provide, the version of the AI and other factors. You will probably get different results than the ones shown here.

A real-world example#

Imagine that you are building a planning solution. The solution requires at some point to implement a function that given two time intervals returns their intersection. If you create the pseudo-code of you solution, then the AI will be able to help you with the implementation of the function.

Getting in line suggestions#

Let’s open VSC, create a new directory ch2 in the explorer and use the option “File” >> “Open Folder” to open this directory as the root of your peoject. Now, create a new file time_intervals.py.

Querying Copilot with the inline chat#

As soon as we create the new Python file, we see the message “Press Ctrl+I to ask Github Copilot Chat to do something. Start typing to dismiss”.

message

Go ahead and press Ctrl+I. You will see a chat window open. Write “create a function that given two time intervals returns their intersection” and press enter. You will see that the AI will start to provide suggestions for the implementation of the function. If you are satisfied with the suggestion, you can press Accept to accept it.

This is what I got:

def find_intersection(interval1, interval2):
    start1, end1 = interval1
    start2, end2 = interval2

    # Check if there is no intersection
    if end1 < start2 or end2 < start1:
        return None

    # Calculate the intersection
    intersection_start = max(start1, start2)
    intersection_end = min(end1, end2)

    return (intersection_start, intersection_end)

Explaining code#

To understand the code, we can use Copilot to explain it. We need to select the code, press Ctrl+I and the use the command “/explain” and press enter. Copilot will open the Chat window and will provide an explanation of the code.

func_explanation

What to notice

  • In the chat window, we can see which references were considered for the explanation.

  • We can open the chat in the editor for a better view of the explanation.

  • Copilot suggest the next question.

We accept the suggested question and click on “Can you provide an example usage of the find_intersection function?”

how_to

Generating documentation#

Now that we have a better understading of how the function works. We can ask copilot to add some documentation to the function. We can select the function, press Ctrl+I and use the command “/doc” and press enter. Copilot will open the Chat window and will provide a docstring for the function.

Creating tests#

With out newly documented function, we can now use the /tests command to create tests for our function. This time, Copilot does not add code to the current file but instead it suggest the creation of a new file, test_time_intervals.py. This suggested file name follows a common naming convention for test files which is to use the same name as the file to be tested, but with the prefix test_.

test

You can also combine the command with more detailed instructions. For example, you can use the command /tests with pytest.

generated_test

Generating sample code#

Copilot is also great for generating sample code. To test this functionality, let’s ask Copilot “create a new file named syntax.py with two sample dataframes that contain a common column.”

We will see that copilot: First, propose a code snippet that creates two sample dataframes with a common column. If we hover over the code, we can see that there will be a symbol with three dots that will allow us to access the option “Insert into New File”.

import pandas as pd

# Create the first dataframe
df1 = pd.DataFrame({
    'A': ['A0', 'A1', 'A2', 'A3'],
    'B': ['B0', 'B1', 'B2', 'B3'],
    'common': ['C0', 'C1', 'C2', 'C3']
})

# Create the second dataframe
df2 = pd.DataFrame({
    'C': ['C0', 'C1', 'C2', 'C3'],
    'D': ['D0', 'D1', 'D2', 'D3'],
    'common': ['C0', 'C1', 'C2', 'C3']
})

Second, it will explain how to create a new file using VSC. Finally, it will remaid us to ensure that we have Pandas installed and share the command to install it.

Let’s go ahead and create the new file and name it syntax.py.

Getting help with syntax#

Even seasoned developers sometimes forget the syntax for a specific operation. Copilot can help with that. For example, if you forget the syntax for a specific operation, you can ask Copilot for help.

If we now move to the end of the file and add the following comment:

# Merge the two DataFrames on the column 'common' and add a suffix to the columns of the second data frame.

If we set the cursor below the comment, we will see that Copilot will provide a suggestion for the syntax of the operation. This time we can accept the operation with Tab.

df = df1.merge(df2, on='common', suffixes=('_df1', '_df2'))

Some tips for effective prompting#

When using GitHub Copilot, it is important to keep in mind that the quality of the suggestions you receive depends on the quality of the prompts you provide. Here are some general tips that can help you get the best results from an AI assistant like GitHub Copilot:

  • Clearly define the problem or task you need help with.

  • Provide relevant context and background information.

  • Break down complex problems into smaller, manageable steps.

  • Use descriptive variable and function names.

  • Include sample inputs and expected outputs for clarity.

  • Consider suggesting alternative approaches or variations.

  • Provide feedback to help improve the AI’s understanding and suggestions.

  • Experiment and iterate with the AI to find the best solution.

Integration in other parts of the development process#

Github copilot can also support other parts of the software development process. For example, it can suggest commit messages in the commit message box in the source control view.

Gitlab Duo, the AI assistant from Gitlab, is experimenting with features like issue description generation, merge request, template population, suggested reviewers, merge request summary, code review summary, vulnerability resolution, value stream forecasting, and product analytics among others.

Data Privacy with Github Copilot Individual and Business.#

At the time of writing, GitHub Copilot is available in two versions: GitHub Copilot Individual and GitHub Copilot Business. The main difference between the two versions is how user engagement data, prompts, and suggestions are used and shared.

The Business plan is characterized by its stringent data policies, underscoring a commitment to data minimization and the protection of privacy. This commitment is evidenced by explicit guidelines on the retention and subsequent disposal of data, practices that resonate with the privacy expectations of business entities and align with the global trend towards rigorous data governance and minimal data retention mandates.

Concretely, in the case of the Business plan,

  • user engagement data is retained by GitHub for 24 months,

  • prompts are discarded once a suggestion is returned, and

  • suggestions are not retained by GitHub.

Conversely, the Individual plan adopts a more expansive strategy towards data utilization. This approach is rooted in the goal of harnessing user interactions to continuously refine and enhance Copilot’s functionalities, foster the development of new developer tools, and fuel academic and product research. This broad use of data serves as a catalyst for innovation within Copilot’s ecosystem, facilitating significant service enhancements and valuable contributions to the technological domain. However, it also prompts a closer examination of the plan’s data retention and disposal protocols, inviting users to consider how their data is managed over time.

References#

https://code.visualstudio.com/docs/copilot/overview https://www.upwork.com/resources/prompt-engineering-with-github-copilot https://github.blog/2023-06-20-how-to-write-better-prompts-for-github-copilot/ https://github.blog/2023-05-12-how-i-used-github-copilot-to-build-a-browser-extension/ GitHub Copilot Business Privacy Statement About privacy for GitHub Copilot Individual