10 ways to build applications faster with Amazon CodeWhisperer

April 26, 2023 By Mark Otto Off

Amazon CodeWhisperer is a powerful generative AI tool that gives me coding superpowers. Ever since I have incorporated CodeWhisperer into my workflow, I have become faster, smarter, and even more delighted when building applications. However, learning to use any generative AI tool effectively requires a beginner’s mindset and a willingness to embrace new ways of working.

Best practices for tapping into CodeWhisperer’s power are still emerging. But, as an early explorer, I’ve discovered several techniques that have allowed me to get the most out of this amazing tool. In this article, I’m excited to share these techniques with you, using practical examples to illustrate just how CodeWhisperer can enhance your programming workflow. I’ll explore:

Before we begin

If you would like to try these techniques for yourself, you will need to use a code editor with the AWS Toolkit extension installed. VS Code, AWS Cloud9, and most editors from JetBrains will work. Refer to the CodeWhisperer “Getting Started” resources for setup instructions.

CodeWhisperer will present suggestions automatically as you type. If you aren’t presented with a suggestion, you can always manually trigger a suggestion using the Option + C (Mac) or Alt + C (Windows) shortcut. CodeWhisperer will also sometimes present you with multiple suggestions to choose from. You can press the → and ← keys to cycle through all available suggestions.

The suggestions CodeWhisperer offers are non-deterministic, which means you may receive slightly different suggestions than the ones shown in this article. If you receive a suggestion that doesn’t suit your needs, try cycling through all available suggestions using the arrow keys or try rephrasing any code comment prompts slightly. Even punctuation changes can produce fresh suggestions.

The examples in this article are written in JavaScript and TypeScript. However, the techniques I’ll demonstrate are equally suited to many of the programming languages supported by CodeWhisperer. Now, onto the examples!

Typing less

At the most basic level, CodeWhisperer can be used as a supercharged auto-complete, significantly reducing your typing when writing individual lines or blocks of code. To use CodeWhisperer in this way, just write code as you normally would. CodeWhisperer will make automatic suggestions as you type. Accept suggestions when they’re helpful, and ignore suggestions when they’re not. In my experience, using CodeWhisperer in this way alone reduces my typing by 50% or more.

Generating functions

Automatically generating function bodies from signatures

Skilled programmers use clear, descriptive names for their functions and parameters. If you follow this practice in your own programming, CodeWhisperer will have all the context it needs to implement many functions for you.

After I enter the function signature below, CodeWhisperer generates the function body for me.

User input (JavaScript):

function standardDeviation(numbers)

Result:

function standardDeviation(numbers) { let mean = numbers.reduce((a, b) => a + b) / numbers.length; let variance = numbers.reduce((a, b) => a + Math.pow(b - mean, 2)) / numbers.length; return Math.sqrt(variance);
}

Generating functions from comments

Sometimes a good function signature alone isn’t enough to convey the desired behavior of a function. In those cases, you can use a detailed code comment as a prompt to CodeWhisperer. I recommend using your language’s doc comment delimiter when using this technique.

User Input (JavaScript):

/** * Given a list that contains some numbers and strings, format them into a string * in which the numbers are prepended with "#" and the strings are wrapped in * double quotes. */

Result:

/** * Given a list that contains some numbers and strings, format them into a string * in which the numbers are prepended with "#" and the strings are wrapped in * double quotes. */
function formatList(list) { return list .map((item) => { if (typeof item === "number") { return `#${item}`; } else { return `"${item}"`; } }) .join(", ");
}

Generating classes

You can generate full or partial class implementations from a single code comment. The comment should describe the key properties and behaviors of the class. Simple descriptions are often all that is required.

User input (TypeScript):

/** * A folder has a name, children, and an optional parent. A child can't be added * twice. */

Result:

After I write the above block comment, CodeWhisperer provides multiple suggestions, including the following implementation, complete with documentation comments for each method!

/** * A folder has a name, children, and an optional parent. A child can't be added * twice. */
export class Folder { name: string; children: Folder[]; parent?: Folder; constructor(name: string, parent?: Folder) { this.name = name; this.children = []; this.parent = parent; } /** * Add a child to the folder. * @param child The child to add. * @returns True if the child was added, false if it was already present. */ addChild(child: Folder): boolean { if (this.children.includes(child)) { return false; } this.children.push(child); child.parent = this; return true; } /** * Remove a child from the folder. * @param child The child to remove. * @returns True if the child was removed, false if it wasn't present. */ removeChild(child: Folder): boolean { const index = this.children.indexOf(child); if (index === -1) { return false; } this.children.splice(index, 1); child.parent = undefined; return true; }
}

Implementing algorithms

CodeWhisperer enables you to implement well-known algorithms with ease. Simply tell CodeWhisperer which algorithm you want to apply to a particular coding problem, and it will oblige.

User input (JavaScript):

const actors = [ ... ]; // Sort the actors by age using the merge sort algorithm.

Result:

const actors = [ ... ]; // Sort the actors by age using the merge sort algorithm.
function mergeSort(arr) { if (arr.length < 2) { return arr; } const middle = Math.floor(arr.length / 2); const left = arr.slice(0, middle); const right = arr.slice(middle); return merge(mergeSort(left), mergeSort(right));
} function merge(left, right) { const result = []; while (left.length && right.length) { if (left[0].age < right[0].age) { result.push(left.shift()); } else { result.push(right.shift()); } } while (left.length) { result.push(left.shift()); } while (right.length) { result.push(right.shift()); } return result;
} console.log(mergeSort(actors));

Writing unit tests

CodeWhisperer accelerates the implementation of unit tests. Not only is it able to implement the logic for most of the explicit unit tests I ask for, but it often suggests unit tests for edge cases that hadn’t even occurred to me.

When I want CodeWhisperer to implement a unit test, I start with a declaration of the condition I’m testing and the expected result as in the following example.

User input (JavaScript):

describe("Folder.addChild()", () => { it("when adding an existing folder should not add duplicate"

Result:

describe("Folder.addChild()", () => { it("when adding an existing folder should not add duplicate", () => { const folder = new Folder("root"); const child = new Folder("child"); folder.addChild(child); folder.addChild(child); expect(folder.children.length).toBe(1); });
});

After you’ve written a test or two, CodeWhisperer is likely to suggest more tests, even without explicit prompting.

Tip: You’ll notice that my test description includes 1) the function I’m testing, 2) the condition I’m testing, and 3) what I expect the result to be. Following this three-part pattern is a great way to ensure CodeWhisperer has the context it needs to implement high quality test logic.

Creating sample data

One of the delightful discoveries I’ve made is that CodeWhisperer can generate dummy data—or really any code that has a repeating pattern. As an illustration, for the following list I entered only the first “user” object as an example, and CodeWhisperer helpfully continued the pattern.

User input (JavaScript):

const fakeUsers = [ { name: "User 1", id: "user1" },

Result:

const fakeUsers = [ { name: "User 1", id: "user1" }, { name: "User 2", id: "user2" }, { name: "User 3", id: "user3" }, { name: "User 4", id: "user4" }, { name: "User 5", id: "user5" }, { name: "User 6", id: "user6" }
]

It even works for generating random but plausible values like the names seen below.

User input (JavaScript):

const fakeUsers = [ { name: "Sally", id: "user1" },

Result:

const fakeUsers = [ { name: "Sally", id: "user1" }, { name: "Bob", id: "user2" }, { name: "Joe", id: "user3" }, { name: "Jane", id: "user4" }, { name: "Jill", id: "user5" }, { name: "Jack", id: "user6" }
]

Simplifying regular expressions

While CodeWhisperer can’t translate natural language into custom regular expressions (yet), it does know a lot of commonly used regular expressions. This saves you time by putting these regular expressions at your fingertips whenever you need them.

Let’s look at one notoriously complex but common regular expression.

User input (JavaScript):

/** * Function that validates whether a string is a valid email address. */

Result:

/** * Function that validates whether a string is a valid email address. */
function validateEmail(email: string): boolean { const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(String(email).toLowerCase());
}

Learning third-party code libraries faster

CodeWhisperer helps accelerate my learning when I’m using an unfamiliar library, module, or framework. In the example below, I needed to write a utility function that would take a gzip compressed value coming from an API and translate it into a value that the rest of my code could understand. I knew the “pako” library would help me, but I had no idea how to use it. I just prompted CodeWhisperer with an import statement and a comment describing my goal.

import pako from "pako"; /** * Accepts a base64-encoded, gzip-compressed JSON string and returns * a parsed JSON object. */

Result:

import pako from "pako"; /** * Accepts a base64-encoded, gzip-compressed JSON string and returns * a parsed JSON object. */
export function decodeGzipJson(base64String) { const compressed = atob(base64String); const uncompressed = pako.inflate(compressed, { to: "string" }); return JSON.parse(uncompressed);
}

Documenting code

CodeWhisperer is capable of generating docstrings and comments for the code it generates, as well as for your existing code. For example, let’s say I want CodeWhisperer to document the matches() method of this FavoritesFilter TypeScript class I’ve implemented (I’ve omitted some implementation details for brevity).

class FavoritesFilter implements IAssetFilter { ... matches(asset: Asset): boolean { ... }
}

I can just type a doc comment delimiter (/** */) immediately above the method name and CodeWhisperer will generate the body of the doc comment for me.

Note: When using CodeWhisperer in this way you may have to manually trigger a suggestion using Option + C (Mac) or Alt + C (Windows).

class FavoritesFilter implements IAssetFilter { ... /** * Determines whether the asset matches the filter. */ matches(asset: Asset): boolean { ... }
}

Conclusion

I hope the techniques above inspire ideas for how CodeWhisperer can make you a more productive coder. Install CodeWhisperer today to start using these time-saving techniques in your own projects. These examples only scratch the surface. As additional creative minds start applying CodeWhisperer to their daily workflows, I’m sure new techniques and best practices will continue to emerge. If you discover a novel approach that you find useful, post a comment to share what you’ve discovered. Perhaps your technique will make it into a future article and help others in the CodeWhisperer community enhance their superpowers.

Kris's profile picture

Kris Schultz (he/him)

Kris Schultz has spent over 25 years bringing engaging user experiences to life by combining emerging technologies with world class design. In his role as 3D Specialist Solutions Architect, Kris helps customers leverage AWS services to power 3D applications of all sorts.