---

This documentation applies to the on-premises installation of watsonx Code Assistant on IBM Software Hub. For IBM Cloud documentation, see Explaining code.

Explaining code

Use generative AI to analyze and summarize your code to understand what the code does.

Eclipse IDE only: For best results with a multimodule Java application, ensure that you import the aggregator module and each submodule as separate projects within the Eclipse workspace. Watsonx Code Assistant is unlikely to successfully introspect your project if you import the top-level directory as a single project.

Language support

Code explanation is available for the following languages:

  • C
  • C++
  • Go
  • Java
  • JavaScript
  • Python
  • TypeScript

Using a chat command to explain code

You can use the /explain command in chat to explain code for a referenced class, file, function, or method in the active workspace.

Use this syntax:

/explain <code reference> [additional instructions]

For <code reference>, type the @ symbol to see a list of classes, files, functions, and methods from your workspace. Use one class, file, function, or method reference at a time.

The [additional instructions] are optional. Add instructions if you want specific details.

Example prompts:

  • /explain @file.java
  • /explain @method1

Using the CodeLens in the editor to explain code

In the IDE editor, the CodeLens shows a line of generative AI options that precedes code blocks and snippets.

  1. Click the Explain option that immediately precedes a code block to generate an explanation.

    In the following code example, the Explain | Document | Unit Test options immediately precede the protected void keywords.

    CodeLens example
    CodeLens example

  2. The watsonx Code Assistant chat window opens, displays the /explain @<*item name*> command, runs the command, and displays the explanation.

Using the Explorer context menu to explain code

To generate an explanation from a context menu:

  1. In the Explorer view, expand your application to the code that you want to generate an explanation for.

  2. Right-click the code, click watsonx Code Assistant, then click Explain.

  3. The watsonx Code Assistant chat displays the /explain command for the code that you selected. The following syntax is used in the command:

    /explain <code syntax>

  4. Watsonx Code Assistant processes the request and in the chat displays the explanation for the code that you selected.

To request and view an explanation for code changes:

  1. In your Eclipse IDE, right-click on the project or file or class, and then click Explain Code Changes. Or in your Visual Studio Code context menu, right-click and select watsonx Code Assistant, and then click Explain.
  2. The code assistant performs a scan and provides a code change explanation.
  3. After you review the explanation, you can click Save code change explanation to store in a local file.

Java programming languages output a Java-specific explanation. All other languages output a generic explanation.-->

Explaining Java applications

Application explanation is only supported for Java applications that contain one or more classes that extend javax.servlet.http.HttpServlet. These classes need to implement any one of the following methods: doGet, doPost, or doPut.

To request and view an explanation for an application:

  1. In your IDE, right-click on any item in the hierarchy in the directory of the application that you want to explain, and then click Explain Application.

    In the Eclipse IDE, if you have a multimodule application with a peer multimodule root, the application explanation applies only to the selected module.

  2. Watsonx Code Assistant scans the application to generate an overview and a list of main services with a description of functionality for each method.

  3. Click Save to retain a copy, or the explanation is discarded.

  4. Click Open explanation and review, and then you can click Save application explanation to store in a local file.