Explaining code
watsonx Code Assistant
Use generative AI to analyze and summarize your code to understand what the code does.
The following table lists the type of explanation for each plan.
Plan | Explanation | Description |
---|---|---|
Trial plan Essentials plan | Basic | Uses generative AI to provide a basic explanation. No extra code analysis is required. |
Standard plan | Enhanced | Requires a built application, and uses a code analysis and generative AI to provide an enhanced code explanation for Java methods/classes and enterprise Java applications. |
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
When you reference a method, or use CodeLens on a method, watsonx Code Assistant supports certain code languages. Referencing a full file works for all languages. For more information, see Language support when you work with methods.
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.
When you reference a file, the size limit is 50 KB. If you reach this limit, split the file into individual functions and reference each function. Or, split the file at 49 KB, taking care of function boundaries, and reference the file at each split. With either approach, you need to merge the results.
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.
Known issue If you are using the watsonx Code Assistant for Enterprise Java Applications extension, upon startup it might take a few seconds for the enhanced Java capabilities to be available from the CodeLens.
-
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 theprotected void
keywords.CodeLens example -
The watsonx Code Assistant chat window opens, displays the
/explain @<*item name*>
command, runs the command, and displays the explanation.
Disabling CodeLens
If you want to disable the CodeLens options, you can change the setting for the extension or plug-in.
In Visual Studio Code:
-
Open the settings for the extension.
-
Clear the
Enable CodeLens
setting.
In Eclipse:
-
Open the settings for the Eclipse IDE.
-
In the watsonx Code Assistant Settings entry, clear the
Enable CodeLens
setting. -
Click Apply and close.
Explaining Java applications
Standard plan Application explanation is only available with the watsonx Code Assistant for Enterprise Java Applications extension.
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
.
For large applications, the explanation considers the 40 entry points with the highest cyclomatic complexity. If the application exceeds 40 entry
points, the explanation contains the section The following methods were not considered:
that provides details about which entry points were not considered.
To request and view an explanation for an application:
-
In your IDE, right-click 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.
-
Watsonx Code Assistant scans the application to generate an overview and a list of main services with a description of functions for each method.
-
Click Save to retain a copy, or the explanation is discarded.
-
Click Open explanation and review, and then you can click Save application explanation to store in a local file.