

# Code examples
<a name="inline-suggestions-code-examples"></a>

Amazon Q can suggest code in different scenarios. To understand how it can help you as you write code in your programming language of choice, view the following code examples.

**Topics**
+ [Using Amazon Q Developer for single-line code completion](single-line-completion.md)
+ [Using Amazon Q Developer for full function generation](full-function-generation.md)
+ [Using Amazon Q Developer for block completion](code-block.md)
+ [Using Amazon Q Developer for Docstring, JSDoc, and Javadoc completion](docstring-javadoc.md)
+ [Using Amazon Q Developer for line-by-line recommendations](line-by-line-1.md)

# Using Amazon Q Developer for single-line code completion
<a name="single-line-completion"></a>

When you start typing out single lines of code, Amazon Q makes suggestions based on your current and previous inputs.

------
#### [ C\$1\$1 ]

![\[An example of the single-line completion feature using C++.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/single-line-completion-c-plus.gif)


------
#### [ JavaScript ]

In this example, Amazon Q completes a line of code that the developer begins.

![\[An example of the single-line completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/javascript_vscode_SingleLine.gif)


------
#### [ TypeScript ]

In this example, the user enters a full comment, and then Amazon Q supplies the code that goes with it.

![\[An example of the single-line completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/typescript_vscode_single-line.gif)


------
#### [ C\$1 ]

In this example, Amazon Q provides a single-line recommendation based on a comment.

![\[An example of the single line completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/c-sharp-single-line.gif)


------
#### [ Shell ]

In the image below, Amazon Q offers recommendations on how to complete a single line of code.

![\[An example of the single-line completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/jb-single-line-shell.gif)


------
#### [ Java ]

When you start typing out single lines of code, Amazon Q makes suggestions based on your current and previous inputs.

In the example below, in Java, a user enters the string `public` into an existing class.

Based on the input, Amazon Q generates a suggestion for the signature of the main method. 

![\[An example of the single-line completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/cw-c9-single-line-completion.gif)


------
#### [ Python ]

In this example, Amazon Q recommends a single line of code, based on the developer's comment. 

![\[An example of the single-line completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/python_sagemakerstudio_singleline.gif)


------

# Using Amazon Q Developer for full function generation
<a name="full-function-generation"></a>

Amazon Q can generate an entire function based on a comment that you've written. As you finish your comment Amazon Q will suggest a function signature. If you accept the suggestion, Amazon Q automatically advances your cursor to the next part of the function and makes a suggestion. Even if you enter an additional comment or line of code in between suggestions, Amazon Q will refactor based on your input.

------
#### [ C ]

![\[An example of the full function completion feature using C.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/full-function-completion-c-plus.gif)


------
#### [ C\$1\$1 ]

![\[An example of the full function completion feature using C++.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/full-function-completion-c-plus.gif)


------
#### [ JavaScript ]

In the following example, the user generates, and then edits, a full function based on a set of comments.

![\[An example of the full-function generation feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/javascript_lambda_FullFunctionGeneration.gif)


In the following image, a user has written a function signature for reading a file from Amazon S3. Amazon Q then suggests a full implementation of the `read_from_s3` method.

![\[\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/whisper-function-read-from-s3.png)


**Note**  
Sometimes, as in the previous example, Amazon Q includes `import` statements as part of its suggestions. As a best practice, manually move these `import` statements to the top of your file.

As another example, in the following image, a user has written a function signature. Amazon Q then suggests a full implementation of the `quicksort` method.

![\[\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/whisper-function-quicksort.png)


Amazon Q considers past code snippets when making suggestions. In the following image, the user in the previous example has accepted the suggested implementation for `quicksort` above. The user then writes another function signature for a generic `sort` method. Amazon Q then suggests an implementation based on what has already been written.

![\[\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/whisper-function-from-context-sort.png)


In the following image, a user has written a comment. Based on this comment, Amazon Q then suggests a function signature.

![\[\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/whisper-comment-binary-search.png)


In the following image, the user in the previous example has accepted the suggested function signature. Amazon Q can then suggest a complete implementation of the `binary_search` function.

![\[\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/whisper-function-binary-search.png)


------
#### [ Java ]

The following list contains examples of how Amazon Q makes suggestions and advances you through the entire process of creating a function.

1. In the following example, a user inputs a comment. Amazon Q suggests a function signature.

   After the user accepts that suggestion, Amazon Q suggests a function body.  
![\[An example of a function generated from a comment.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/cw-c9-function-from-comment.gif)

1. In the image below, a user inputs a comment in the body of the function prior to accepting a suggestion from Amazon Q. On the following line, Amazon Q generates a suggestion based on the comment.  
![\[An example of a function generated from a comment inside an existing block of code.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/cw-c9-function-from-comment-within-block.gif)

------
#### [ C\$1 ]

In the following example, Amazon Q recommends a full function.

![\[Function declaration for ListTables with AmazonDynamoDBClient parameter in code editor.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/c-sharp-full-function.gif)


------
#### [ TypeScript ]

In the following example, Amazon Q generates a function based on the user's docstrings.

![\[An example of the full function completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/typescript_vscode_function.gif)


------
#### [ Python ]

Amazon Q can generate an entire function based on a comment that you've written. As you finish your comment, Amazon Q will suggest a function signature. If you accept the suggestion, Amazon Q automatically advances your cursor to the next part of the function and makes a suggestion. Even if you enter an additional comment or line of code in between suggestions, Amazon Q will refactor based on your input.

In the following example, Amazon Q generates both a full function and the corresponding unit test.

![\[An example of the full function completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/python_pycharm_fullfunction_unittests.GIF)


The following list contains examples of how Amazon Q makes suggestions and advances you through the entire process of creating a function.

1. In the image below, a user has input a comment. The function signature, located below the comment, is a suggestion from Amazon Q.  
![\[alt_text\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/cw-jb-comment-062022.png)

1. In the image below, the user has accepted the Amazon Q suggestion for a function signature. Accepting the suggestion automatically advanced the cursor and Amazon Q has made a new suggestion for the function body.  
![\[alt_text\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/cw-jb-commentfunctionadvance-062022.png)

1. In the image below, a user input a comment in the body of the function prior to accepting a suggestion from Amazon Q. On the following line, Amazon Q has generated a new suggestion based on the content of the comment.  
![\[generateing a new suggestion based on the content of a comment\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/cw-jb-commentfunction-062022.png)

In this example, Amazon Q recommends a full function after the user types part of the signature. 

![\[An example of the full function feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/python_sagemakerstudio_fullfunction.gif)


------

# Using Amazon Q Developer for block completion
<a name="code-block"></a>

Block completion is used to complete your `if/for/while/try` code blocks.

------
#### [ C ]

![\[An example of the block completion feature using C\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/block-completion-c.gif)


------
#### [ C\$1\$1 ]

![\[An example of the block completion feature using C++.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/block-completion-c-plus.gif)


------
#### [ Java ]

In the example below, a user enters the signature of an `if` statement. The body of the statement is a suggestion from Amazon Q.

![\[An example of the block completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/cw-c9-block-completion.gif)


------
#### [ C\$1 ]

In the image below, Amazon Q recommends a way to complete the function.

![\[An example of the block completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/c-sharp-block.gif)


------
#### [ TypeScript ]

In the image below, Amazon Q recommends a way to complete the function.

![\[An example of the block completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/typescript_vscode_block-completion.gif)


------
#### [ Python ]

In this example, Amazon Q recommends a block of code, based on the context. 

![\[An example of the code block feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/python_sagemakerstudio_codeblock.gif)


------

# Using Amazon Q Developer for Docstring, JSDoc, and Javadoc completion
<a name="docstring-javadoc"></a>

Amazon Q can help you generate or complete documentation inside your code.

------
#### [ C\$1\$1 ]

![\[An example of the docstring completion feature using C++.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/docstring-completion-c-plus.gif)


------
#### [ Javascript ]

In this example, Amazon Q fills in JSDoc parameters based on existing constants.

![\[An example of JSDoc completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/javascript_vscode_JSDocCompletion.gif)


------
#### [ C\$1 ]

In this example, Amazon Q fills in JSDoc parameters based on existing constants.

![\[An example of C3 with DocString completion.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/c-sharp-docstring.gif)


------
#### [ Java ]

The following example is adapted from [an example on the Oracle website](https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html).

In the image below, the user has started entering a docstring. Amazon Q has suggested words to add to the docstring.

![\[suggesting a function to complete a docstring\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/jb-docstring-java.png)


The following example is adapted from [an example on the Oracle website](https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html).

In the example below, in Java, the user enters a docstring. Amazon Q suggests a function to process the docstring.

![\[An example of code completion based on a Javadoc.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/cw-c9-javadoc.gif)


------
#### [ Python ]

In this example, Amazon Q recommends a Docstring, based on the surrounding context. 

![\[An example of the Docstring feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/python_sagemakerstudio_commentblock.gif)


------

# Using Amazon Q Developer for line-by-line recommendations
<a name="line-by-line-1"></a>

Depending on your use case, Amazon Q may not be able to generate an entire function block in one recommendation. However, Amazon Q can still provide line-by-line recommendations.

------
#### [ Go and GoLand ]

In this example, Amazon Q provides line-by-line recommendations.

![\[An example of the line-by-line completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/jb-line-by-line-go.gif)


Here is another example of line-by-line recommendations, this time with a unit test.

![\[An example of the line-by-line completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/jb-line-by-line-unit-test-go.gif)


------
#### [ C\$1\$1 and CLion ]

In this example, Amazon Q provides line-by-line recommendations.

![\[An example of the line-by-line completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/jb-line-by-line-cpp.gif)


------
#### [ Python ]

In the following image, the customer has written an initial comment indicating that they want to publish a message to an Amazon CloudWatch Logs group. Given this context, Amazon Q is only able to suggest the client initialization code in its first recommendation, as shown in the following image.

![\[\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/whisper-line-by-line-cwlogs-1.png)


However, if the user continues to request line-by-line recommendations, Amazon Q also continues to suggest lines of code based on what's already been written.

![\[\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/whisper-line-by-line-cwlogs-2.png)


**Note**  
In the example above, `VPCFlowLogs` may not be the correct constant value. As Amazon Q makes suggestions, remember to rename any constants as required.

Amazon Q can eventually complete the entire code block as shown in the following image.

![\[\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/whisper-line-by-line-cwlogs-3.png)


In this example, Amazon Q provides recommendations, one line at at time.

![\[An example of the line-by-line completion feature.\]](http://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/images/python_sagemakerstudio_linebyline.gif)


------