Code generation using comments

You can generate code suggestions based on a comment that you enter within a program.

  1. Open your program in the editor.
  2. At the location where you want to insert new code, enter a comment that describes the code you want.
  3. Use the keyboard shortcut:

    • For Mac: option + , (comma)
    • For Windows: Alt + , (comma)
  4. IBM® watsonx Code Assistant™ for Z adds code suggestions based on the comment that you entered.

    Observe that the application uses the previous code in the program to set context for the code suggestions.

  5. Click Accept from the context menu to accept the entire code suggestion. Alternately, click Accept Word from the context menu to accept the code suggestions word by word.

    You can always edit the code in the editor even after accepting the entire suggestion.

  6. Press the esc key if you do not want to accept the code suggestion. Continue to type more code in the same line for improved code suggestions.
Note: When using comment to generate code suggestions, ensure that you have placed the cursor on the next line of the comment where no prior code lines exist. You can also indent the cursor position if required.

Best practices to write effective comments

When you use comment-to-code in the editor, write a comment that describes the intended behavior, similar to how you add comments for code written by you. For example, use * paragraph that returns even numbers.

Do not write your comment as an instruction, such as * write a paragraph that returns even numbers. IBM watsonx Code Assistant for Z uses models that are trained to complete code on data that contains many typical, descriptive comments, so these types of comments yield better results.

Cursor placement may sometimes cause odd results from in-line milch-line generation from comments. It is recommended to use comments on column 7.