Skip to content

1. Getting Started with an AI Coding Assistant: First Steps with a Simple Example

A simple example is the best way to start using a new tool.
In your favorite IDE, create a Java project named maths, and add a Calculator class with the following implementation:

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }

    public int multiply(int a, int b) {
        return a * b;
    }

    public int divide(int a, int b) {
        if (b == 0) {
            throw new IllegalArgumentException("Cannot divide by zero");
        }
        return a / b;
    }
}

1.1 Generating Code Suggestions from Comments (Describing Intended Behavior)

  • Place your cursor at the end of the class, just before the closing brace }.
  • Write a comment that expresses the intended behavior you want to implement, for example:
    // Method to calculate the square root
    Then press Enter and watch what happens…

  • The AI coding assistant will suggest an implementation for the behavior described in your comment.

  • If the suggestion fits your needs and you want to insert the entire suggested code, press the Tab key.

  • Now try a new comment for a different method, for example:
    // Method to calculate the logarithm
    Press Enter to view the code generated by the assistant.

  • You don’t have to accept the entire suggestion. You can insert the code partially:

  • Press Ctrl+Right Arrow to insert one word at a time.
  • Press Ctrl+Alt+Right Arrow to insert one line at a time.

  • Place the cursor on a new line and wait. Even without a comment (i.e., no explicit request), the assistant may suggest new methods to enhance your class. It’s up to you to decide whether or not to include the suggested code based on your needs.

Key Takeaways

  • For each task, start by writing a comment that describes what you want to do. Then let the AI coding assistant suggest an implementation. Remember, it's a support tool—you should always review, understand, and, if needed, adjust the code it generates.

  • If the suggestion doesn’t fit your needs, just press Enter and write your own code.

1.2 Generating Unit Test Code

  • Use your IDE to create a CalculatorTest class in the appropriate location of your project. This class will contain the unit tests for the Calculator class.

  • Open the new class (make sure it’s empty for now) and wait for your assistant’s suggestion.

  • The AI coding assistant should propose a complete set of unit tests for each method in the Calculator class. Press Tab to insert the suggested code.

  • Run the tests to make sure they all pass, and check the code coverage.

  • If the assistant doesn't suggest anything automatically, you can use the same approach as before: write a comment describing the behavior you want to test. For example, to create a test for division by zero:

  • Write the comment: // test divide by zero
  • The assistant will likely start by suggesting the @Test annotation. Confirm it with Tab, then press Enter to let it continue generating the method.
  • The assistant will then suggest an implementation of the test method, which you can either accept by pressing Tab, or dismiss by pressing Enter allowing you to take over and continue manually in your IDE.

1.3 Explaining How Code Works (/explain)

  • In the Calculator class, select the entire divide method.

  • With the code highlighted, open your assistant’s chat panel.
    To open Copilot Chat, right-click and look for something like (GitHub) Copilot, then choose Open Chat Panel. The chat window should open on the right side of your IDE. In some IDEs, you can also use Inline Chat, which appears directly in the code editor. For this tutorial, we recommend using the standard side chat.

  • The assistant chat works like a typical AI chat. Once the code is selected, just ask a question like: How does this code work? The assistant will then provide an explanation of the selected code.

  • In software development, developers often ask: How does this code work? To save time, Copilot offers a shortcut for this frequent question: the /explain command. Make sure the divide method is still selected, then type /explain in the chat and press Enter. As before, Copilot will return a detailed explanation of what the code does.

1.4 A Few Words About Prompts

It’s now time to introduce the concept of a prompt. In the field of artificial intelligence (AI), a prompt is an instruction or a set of input data that you give to an AI system to generate a response. In other words, it’s how you interact or communicate with your AI coding assistant.

Prompts can be short phrases, questions, or keywords. The relevance and quality of the assistant’s responses (or any AI chatbot) depend directly on the quality of your prompts, that is, how clear and precise your instructions are.

1.5 Copilot Slash Commands: Prompting Support for Common Developer Tasks

→ Copilot offers shortcuts known as slash commands, which help you perform common and repetitive software development tasks. By typing a forward slash (/) in the chat, Copilot suggests a list of available commands to interact with it more efficiently.

We’ve already used /explain, and we’ll explore more slash commands throughout this tutorial.

The table below shows the current list of slash commands available in Copilot:

Commande slash Description
/explain Explain how the code works
/feedback Steps to provide feedback
/fix Fix problems and compile errors
/help Get help on how to use Copilot chat
/tests Generate unit tests
/doc Document current selection of code
/simplify Simplify the code

→ By default, Copilot Chat refers to the open file or the selected code.
However, you can explicitly specify which file you want to reference in the chat. To do so, right-click on the desired file, select GitHub Copilot, then click on Reference File in Chat. The file will then be explicitly linked to the chat.

Disclaimer: In the examples you’ll explore later in this tutorial, slash commands are often used as concise but precise prompts to interact with the assistant.

If you're using an AI coding assistant other than Copilot, your tool likely offers an equivalent to Copilot’s slash commands. If it doesn’t, whenever a slash command is used in this tutorial, simply write out the full sentence described in the table above which explains the assistant’s task in detail. Or try teaching your favorite AI assistant that shortcut yourself 😊

1.6 Making Code Easier to Fix, Refactor, and Improve

Fixing Issues: /fix

If the active file contains an error, you can use the /fix slash command to ask Copilot how to correct it.

  • For example, go to the CalculatorTest class and delete all the import statements.
  • This will cause compilation errors in the file.
  • Open the chat and type the /fix command.
    The assistant will suggest how to resolve the compilation errors.

Before moving on, restore the import statements and make sure the code compiles and all tests pass.

Reminder: /fix is a shortcut for the full prompt "Fix problems and compile errors."

Refactoring a Piece of Code

  • Go back to the Calculator class and select (highlight) the entire divide method. This will be the code to refactor.

  • While reviewing this code, you notice a code smell related to the parameter names. Ask the assistant for help by typing in the chat: use better names for parameters.

  • The assistant will suggest a revised version of the method with clearer and more meaningful parameter names addressing the issue as expected.

  • Move your cursor to the top-right corner of the code block suggested by the assistant. You should see two icons appear (the order may vary depending on your IDE). Typically:

  • One icon lets you copy the code.
  • The other (with an arrow) lets you insert the code directly into your editor.

  • Click the arrow icon to insert the new code directly into your editor.

  • You should now see that the originally selected code has been replaced in the Calculator class with the assistant’s version.

  • As with any refactoring, always re-run your tests to confirm they still pass and that the behavior hasn’t changed.

Earlier, we saw that writing a comment directly in the code to describe the desired behavior is a practical way to prompt the AI coding assistant and receive suggestions in the right place.

However, once the suggestion has been accepted, the comment often remains in the code, as is currently the case in the Calculator class for the squareRoot and logarithm methods generated earlier.

But keeping comments that simply explain what the code does is considered a code smell, especially when the method names are already self-explanatory. A quick refactoring can help clean this up.

  • To remove these comments, select both methods (including the comments) and ask the assistant to remove them by typing in the chat: remove comments

  • Then click the arrow icon to accept the assistant’s suggestion and insert the cleaned-up version directly into the source file, replacing the selected code.

Note: You could also have selected the entire class. This step simply shows that the assistant’s refactoring isn’t limited to a single method—it can be applied to a larger block of code. 😉

  • And as with any refactoring, don’t forget to re-run your tests at the end.

Asking How to Improve Code Quality

Of course, you can always use the assistant chat like any standard AI chat by asking various questions, more or less specific, to help improve your code.

  • For example, type a question like: how would you improve this code?

For now, simply explore the conversation and see what your AI coding assistant suggests. Its first response may satisfy your curiosity.
But if you’re looking for a more detailed answer or a suggestion focused on a specific goal, feel free to keep the conversation going to refine its responses.

During the previous prompt about code quality, your assistant may have suggested that "Using Javadoc comments to document public methods" is, in its view, a quality criterion.

1.7 Easily Generate Documentation: /doc

Javadoc comments are different from regular inline comments in code. To better understand (and be convinced of) this difference, you can ask your AI coding assistant the following question: What is the difference between Javadoc comments and regular comments?

Your assistant will likely explain that Javadoc comments are used to generate API documentation.

For example, the Javadoc for the Java SDK (available on Oracle’s website: https://docs.oracle.com) is generated from the Javadoc comments written in the SDK’s Java classes which you use in your own programs. So, if your Java class is intended to become an API used by other developers, then Javadoc comments are a key indicator of quality.

But as every developer knows, writing documentation is often tedious. Fortunately, your AI coding assistant can help automate this task and boost your productivity using the /doc slash command.

  • Go back to the Calculator class and type /doc in the chat (which stands for the full prompt Document current selection of code)

  • The AI coding assistant will then suggest a version of the class with generated Javadoc comments.

  • Select the entire Calculator class and click the insert icon (the one with the arrow) to replace the existing code with the documented version.

  • Before continuing, make sure to re-run your tests to confirm that everything still works. This ensures that the assistant didn’t change the behavior of your code—just added comments.

As you interact more with the assistant, you'll probably notice that it often suggests follow-up questions related to your request. These suggestions help you refine your prompts and explore the topic in more depth.**

After using the /doc command, your AI coding assistant generated the documented code as expected. If you take a closer look at the chat, you’ll probably notice that it also suggested a follow-up question to help you go further either in your actions or your understanding of documentation.
Depending on your curiosity or needs, you’re free to click the suggested question or simply ignore it.

In this part of the tutorial, it's time to explore these follow-up questions and use them as a way to learn more about documentation in a software project.

  • Your assistant may suggest something like:
    What other documentation styles can be used for Java code?
    If not, just type the question directly in the chat or choose the one you see.

  • Then you can continue with:
    What are some best practices for writing documentation in Java?

  • And finish with:
    What are some common mistakes to avoid when writing documentation in Java?

For quick research, these conversations let you explore the assistant’s knowledge directly inside your IDE without needing to search external resources.

1.9 But Copilot Is Still Just a Software Development Assistant

Like all language models, AI coding assistants (such as Copilot) are trained on specific datasets. It’s important to remember that the assistant you’re using in this tutorial, integrated into your IDE, is designed specifically for software development and has been trained with knowledge related to that field.

As a result, its chat is not meant to replace a more general-purpose language model like ChatGPT, even though both are based on OpenAI technology.

  • To see this for yourself, try asking your coding assistant the following question: What is the best documentation for visiting the city of Limoges?

You’ll likely get a response like:

I'm sorry, but I can only assist with developer-related questions. If you have any questions about programming or need help with your code, feel free to ask!

This clearly shows the limits of an AI coding assistant compared to a general-purpose model.

1.10 Step-by-Step Guidance for Using New Tools

You’ve just asked your AI coding assistant to generate Javadoc comments, but do you know how to generate the Javadoc for your project, and where or how to view it?

  • No worries, your AI assistant can also help you get started with this new tool (Javadoc). Just ask the following question in the chat:
    In IntelliJ, how do I generate the Javadoc for my project?
    (Of course, replace IntelliJ with the name of the IDE you’re actually using.) 😉

Notes

  • If you had asked a more general question, like: How do I generate the Javadoc for my project?

There’s a good chance (remember, LLMs are probabilistic!) that the assistant would explain how to use the javadoc command-line tool, the standard utility in the Java SDK to generate documentation. But if it doesn’t, try rephrasing your question more precisely: How do I generate the Javadoc for my project using the command line?

Being specific in your prompt, for example by clearly mentioning the environment (an IDE or the command line), helps the assistant tailor its answer to your context.

  • Since the AI coding assistant can explain how to generate Javadoc in a specific IDE, you might wonder: Can it do it for you? Try asking: Could you generate the Javadoc for this project?

What happens? What kind of response do you get from your assistant?

1.11 Improving Copilot's Responses by Targeting Prompt Context with References (Conversation Agents)

Earlier, we saw that Copilot offers slash commands, shortcuts that let you quickly trigger common tasks or actions.

In the same spirit of simplifying prompt writing and reading, while also making the context more precise, Copilot provides another set of shortcuts called conversation agents. These include variables (using #) and conversation participants (using @), which help make your interactions with the assistant smoother and more effective.

Conversation Variables: The # Notation

By default, Copilot Chat refers to the file you have open or the code you have selected.

Conversation variables (using #) allow you to narrow the scope of Copilot’s answers by making the context more specific.

Here are a few examples of how to use them:

  • Referencing a file by adding a # before the filename:
  • Reference a specific file:
    Where are the tests in #MyFile.java?
  • Reference multiple files:
    How are these files connected #MyFile.java #MyOtherFile.java?
  • Reference specific lines in a file:
    Explain this function #MyFile.java:66–72

  • Referencing a method, class, or function works the same way, just add # before the name of the item you want to refer to.

Conversation Participants: The @ Notation

To reference your entire active workspace in the IDE, you can use the @workspace conversation agent. This participant allows Copilot to take full advantage of all the information available in your currently open files, projects, and configurations, leading to more relevant suggestions.

Using @workspace in a prompt helps the assistant understand the broader context of your development environment.

Other participants like @terminal or @github are also available for interacting with those specific environments.

To learn more about slash commands and conversation agents, see:

Now that you’ve taken your first steps with the AI coding assistant, it’s time to explore and apply it in real-world scenarios using existing projects some simple, some more complex.