💡 Pro tip: Amazon Code Whisperer

TOBB AI
7 min readAug 1, 2023

--

Do programmers memorize every function in the languages they work with? Is their knowledge sufficient to code effectively in their specialized languages under all circumstances? And what happens when a programmer switches to a new programming language? Do they memorize everything?

In the technology age, where libraries are constantly updated, requirements change, and entirely new languages emerge, it is not always possible for a modern programmer to keep track of all these changes and independently produce effective code in every required circumstances. That’s why universities teach us, engineering students, not to rely on ready-made functions or code but to do researches. It’s great; we can code correctly and efficiently by researching, but when we search something in the internet, it takes serious time to find out up-to-date, effective, and correct solutions inside numerous, maybe even hundreds of solutions. Moreover, we may not even notice the mistakes we made as long as the code runs.

In such situations, we need something to guide us. We can definitely say that nowadays, the most significant guide for a modern programmer is artificial intelligence. In this article, we will discuss the machine learning powered real time code generator: Amazon Code Whisperer.

CodeWhisperer, released by Amazon in April 2023, is designed to serve as a powerful tool for software developers. Its primary purpose is to suggest real-time single-line or fully functional code snippets based on the written code and comments, significantly accelerating the software development process and resulting in relatively qualified code.

CodeWhisperer relies on large language models (LLMs) trained on billions of lines of code, including both Amazon’s proprietary code and various open-source projects. This rich data pool enables it to offer well informed recommendations, and if the suggested code snippets are linked to open source projects or has similarities with code segments from those projects, relevant information such as the repository’s URL, the file path reference, and the project’s licensing details are presented. Additionally, CodeWhisperer is able to scan the written code, swiftly identify, and highlight potential security issues, ensuring developers can work with confidence.

CodeWhisperer is free for individual users, while it provides membership to organizations through a monthly per-user subscription method. With organization membership, users can receive code suggestions based on training data consisting of selected open source projects.

Currently, CodeWhisperer supported by some popular IDEs such as JetBrains, Visual Studio Code, AWS Cloud9, and AWS Lambda console. CodeWhisperer supports a wide range of programming languages, including Python, Java, JavaScript, TypeScript, C#, Rust, Go, Ruby, Scala, Kotlin, PHP, C, C++, Shell Scripting, and SQL. In Visual Studio Code and JetBrains IDEs, both code generation and security scanning are available for Python, Java, and JavaScript languages.

First Impression

Below, you will find some simple examples related to the coding process with CodeWhisperer. Before diving into the examples, you can check the user actions’ shortcuts. When preparing my examples, I preferred writing some basic Java code using IntelliJ Idea. If you are also using IntelliJ Idea, you can watch the installation video.

AWS Toolkit keyboard shortcuts for JetBrains and Visual StudioCode:

User Actions — Keyboard shortcuts table for JetBrains and Visual StudioCode

For JupyterLab and more you might want to check the documentary.

Single Line Suggestions

In this example, I wrote a Java code snippet where I defined two integer variables named ‘a’ and ‘b’. Additionally, I included a comment line indicating the task: “Multiply a and b then print the result”

It is important to note that the tool provided this particular suggestion as the second recommendation. The recommended line was “System.out.println(a*b);”, which effectively prints the product of ‘a’ and ‘b’ on the console.

Example of a single line code suggestion: print the product of two numbers on the screen

Whole Function Suggestion

In the example below, you can see that CodeWhisperer suggested a whole function that implements the BubbleSort algorithm based on the comment line.

I asked CodeWhisperer to implement the BubbleSort algorithm for array tyupe of input

Line by Line Suggestion

In some cases, it may not be possible to present an entire function within a single suggestion. Instead, as shown in the example below, progressive recommendations can be made, and ask the user for their approval when necessary.

  • Reversing a string:
1 — suggestion of function signature

After accepting one of the listed suggestions, the cursor moves to the next line, and CodeWhisperer suggests different code approaches to reverse a string.

2 — Function body suggestion after approving the function signature

To put CodeWhisperer’s suggestions to the test, I have accepted one of the listed recommendations. Below, you will find the main function and the relevant code suggestions offered by CodeWhisperer.

Example main function to test “reverse()” method

Let’s see how these suggested approaches perform in action.

The expected output is displayed, confirming the successful implementation of the ‘reverse()’ method that CodeWhisperer suggested to us

Docstring Based Suggestions

CodeWhisperer goes beyond simple suggestions and also provides us DocString based recommendations. To explore this feature, I will attempt to implement the ‘reverse()’ function in a new file within a separate class.

I have created a new class named ‘StringReverser.’ The basic docstring structure will be as follows.

An example docstring for the “reverse()” function, intelligently suggested by CodeWhisperer

After completing the docstring, CodeWhisperer suggested a function signature.

The example function signature based on our docstring

I’ve accepted this suggestion to go forward and see the function body suggestion.

The suggested function body with respect to our docstring

Result of the ‘reverse()’ function execution using StringReverser class:

Main function execution and console output displaying the successful implementation of the ‘reverse()’ function based on CodeWhisperer’s recommendations
  • Palindrome Number Checking Example:

For you, in addition to the string reverser example, I wanted to seek CodeWhisperer’s insight on the Palindrome number detection problem, one of the common problems in Jr. Developer interviews.

Firstly, I created a new class named PalindromeChecker and defined a docsting within this class. Then, I’ve selected the 3rd suggestion using the right arrow key.

PalindromeChecker class, docstring, and 3rd suggested function.

I wasn’t satisfied with the initial suggestion, so I modified the docstring and made a specific request for a function that determines whether the input string is a palindrome, utilizing the previously defined StringReverser class and returning ‘YES’ or ‘NO’ accordingly. As a result, CodeWhisperer adjusted its suggestions to align with the updated docstring.

Modified docstring and updated suggestion

Since the first suggestion satisfies my requirements, I accepted it. Here are two test cases and the results of our newly implemented function.

A non-palindrome input and an output that indicates this input is not a palindrome number
A palindrome input and an output that indicates this input is a palindrome number

I tried to share my first experience with CodeWhisperer with you. It is exciting to see artificial intelligence technologies supporting us in the coding process. However, I felt uncomfortable with constant suggestions appearing while coding. Therefore, I believe it would be better to run CodeWhisperer only when it needed. Additionally, I don’t think such a technology is suitable for students as an normal person constantly receiving suggestions might not want to reject them and learn through their own efforts.

Bonus: Data Collection Preferences

  • Telemetry Metrics Permission
  1. From side bar, right click ol “AWS Toolkit” button
1 — Right click on “AWS Toolkit” button that located on bottom-left of the screen

2. From the menu select “Show AWS Settings” .

2 — The menu and “Show AWS Settings” button

3. You can change your preferences about your usage metrics by unchecking the checkbox that shown below.

3 — Uncheck the “Send usage metrics to AWS” box.
  • Managing Your Content Sharing Preferences in CodeWhisperer

4. Expand the AWS option located in the settings screen’s right panel and click on the CodeWhisperer option.

4 — From the right panel select “CodeWhisperer”

5. Then uncheck the “Share CodeWhisperer content with AWS” option and save your preferences by clicking “Apply” button.

REFERENCES

1.) What is CodeWhisperer?

2.) User Guide PDF

3.) Amazon CodeWhisperer Features

4.) Amazon CodeWhisperer FAQs

5.) Amazon CodeWhisperer Pricing

6.) Amazon CodeWhisperer Workshop

7.) About AWS — Whats New

Author: Elif Nur AFŞAR

Thanks to Mehmet GĂźr for providing me with the required environment to test my cases.

Contact for suggestions and complaints: tobb.yapayzeka.kulubu@gmail.com

--

--

TOBB AI

The future is artificial intelligence. We're here to make it happen.Stay update