
Amazon CodeWhisperer is a machine learning (ML)–powered service that helps improve developer productivity by generating code recommendations based on their comments in natural language and code in the integrated development environment (IDE).
This service is currently in preview and I was fortunate and privileged to get access to try the service. The blog at https://aws.amazon.com/blogs/machine-learning/introducing-amazon-codewhisperer-the-ml-powered-coding-companion/ covers all the high level details to get started.
Hence, the following comments and findings are based on my own understanding of the service at this point of time, and any shortcomings are mine alone ;-) Things may/will change when CodeWhisperer becomes GA
I am not a prolific/regular coder, but I when do code, I make copious use of “Google” search, GitHub repos and other places to look for code that I can reuse. I don't have any statistics to back this, but I am pretty sure that's how a lot of software projects gets done. CodeWhisperer currently supports Java, JavaScript and Python and can be used as part of the AWS Toolkit extension for major IDEs, including JetBrains, Visual Studio Code, and AWS Cloud9. On the AWS Lambda console, CodeWhisperer is available as a native code suggestion feature. You can request for preview access at https://pages.awscloud.com/codewhisperer-sign-up-form.html
Enough talk, let me do some coding in Java and see what CodeWhisperer throws up as suggestions. I am using Visual Studio Code with the AWS toolkit — https://aws.amazon.com/visualstudiocode/ The documentation on setting up CodeWhisperer for VS Code is available at https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/codewhisperer.html
From the docs at https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/codewhisperer-overview.html, you just need to use the following to get the code suggestions ..

my experiments with truth ;-)
Lets start with creating a hello world program ;-) I entered the following comment in the VS code editor ..
// a Hello world program
// name as input parameter and prints name
and lo and behold, CodeWhisperer started giving me code recommendations line by line !!

OK, now I wanted a function to print Java exceptions ..
// function to print java exceptions
and this was the code that was generated ..

Lets try a couple of fairly more complex examples .. (I imported a couple of Java libraries for AWS SDK and the Java Spring framework in the code)
import software.amazon.awssdk.*;
import org.springframework.web.*;// function to read items from an dynamodb table
I got the following recommendation for reading items from DynamoDB table.
// function to read items from an dynamodb table
public static void readItem(DynamoDbClient ddb, String tableName) {
try {
String result = ddb.getItem(request -> request.tableName(tableName).key(key)).item().get("name").s();
System.out.println("GetItem succeeded: " + result);
} catch (DynamoDbException e) {
printException(e);
System.exit(1);
}
}
But wait, after this, it gave me recommendations for write items, delete items and update items as well !! wow !!
// function to write items to an dynamodb table
public static void writeItem(DynamoDbClient ddb, String tableName) {
try {
ddb.putItem(request -> request.tableName(tableName).item(item));
System.out.println("PutItem succeeded");
} catch (DynamoDbException e) {
printException(e);
System.exit(1);
}
}
// function to delete items from an dynamodb table
public static void deleteItem(DynamoDbClient ddb, String tableName) {
try {
ddb.deleteItem(request -> request.tableName(tableName).key(key));
System.out.println("DeleteItem succeeded");
} catch (DynamoDbException e) {
printException(e);
System.exit(1);
}
}
// function to update items in an dynamodb table
public static void updateItem(DynamoDbClient ddb, String tableName) {
try {
ddb.updateItem(
request -> request.tableName(tableName).key(key)
.updateExpression("set info.rating = :r, info.plot=:p, info.actors=:a"),
result -> System.out.println("UpdateItem succeeded:\n" + result.attributes()));
} catch (DynamoDbException e) {
printException(e);
System.exit(1);
}
}
I now wanted to read an item in Dynamodb as an async call, and this was the result ..
// function to read items using dynamodb async client
// parameter will be the dynamodb endpoint to invoke and the tablename
// returns the response from the REST endpoint
public static String readItemAsync(String endpoint, String tableName) {
try {
DynamoDbAsyncClient ddbAsyncClient = DynamoDbAsyncClient.builder().build();
CompletableFuture<Response> response = ddbAsyncClient.getItem(request -> request.tableName(tableName).key(key));
return response.get().toString();
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}
and finally, a function to store a message in a queue
// function to store a message in SQS
// pass the sqs queue and message as parameters
// should return true for success
public static boolean storeMessage(SqsClient sqs, String queueUrl, String message) {
try {
sqs.sendMessage(SendMessageRequest.builder().queueUrl(queueUrl).messageBody(message).delaySeconds(5).build());
return true;
} catch (SqsException e) {
printException(e);
return false;
}
}
CodeWhisperer also has an “Run Security Scan” feature, which should be super useful to weed out critical security issues in the code ..

that’s it for now. I have not tried CodeWhisperer with JavaScript or Python, and I am sure the recommendations would be awesome for those languages too.
I don’t think CodeWhisperer is going to make programmers or programming jobs redundant, but it can act as a force multiplier and ensure that we concentrate on the more important pieces of coding and leverage the best practices/reusable code out there ..
Bye for now !!