10 ways to build applications faster with Amazon CodeWhisperer

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:

Typing less
Generating functions using code
Generating functions using comments
Generating classes
Implementing algorithms
Writing unit tests
Creating sample data
Simplifying regular expressions
Learning third-party code libraries faster
Documenting code

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 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.

Why Do Some Programmers Say Frontend Is Easier Than Backend?

So, you’re wondering if frontend development is easier than backend development. Truth be told, the question is rather challenging. Frontend and backend development are two somewhat complicated aspects of web development in 2023. Fortunately for you, we’ll determine which type of development is more challenging in this article: frontend or backend! 

Do you ever wonder why so many backend developers say that frontend is easier? Discover the answer to this and many other related questions in this article! Have you ever asked yourself questions like What makes frontend easier than backend? What skills are needed to become a successful frontend developer? or What techniques do developers use to make the frontend development process easier?  

It is well known that backend development is more difficult than frontend development. A study by the University of Oxford found that “Backend developers tend to have a higher workload than frontend developers, due to the complexity of the programming language used”. The same study also noted that “The complexity of the backend language also means that backend developers need to have a higher level of technical knowledge than frontend developers”. 

In this article, you will learn why so many backend developers say that frontend is easier, what skills are needed to become a successful frontend developer, and what techniques are used to make the frontend development process easier. After reading this article, you will have a better understanding of why frontend is easier than backend and why it is important to learn both. 

Is Frontend Development Easier Than Backend?​​

During the past decade, frontend development has grown in popularity as more engineers switch from backend development to frontend. Due to its greater availability and perception as being “easier” than backend development, frontend programming has the propensity to be used more frequently. The primary reason so many developers like the frontend are its simplicity. Frontend development has a lower learning curve and calls for less technical knowledge than backend programming. This makes it possible for developers to get started straight away even with just a basic understanding of HTML, CSS, and JavaScript.

Moreover, several frontend frameworks, such as React and Vue, have made it simpler for developers to create working prototypes of websites fast. The tools available are another reason frontend development is perceived as being simpler. Website development is made simpler for developers by the abundance of tools, libraries, and frameworks available. As an illustration, CSS preprocessors like Sass and LESS may significantly cut down on the time required to develop and maintain CSS code. The same is true for JavaScript build tools like webpack and gulp, which may assist developers in writing task automation and optimized code.

The fact that frontend development is more visible and tangible than backend development is a last consideration. As a result, developers can more easily comprehend and interact with the code they write since they can view the results of their labors in real-time in the browser. Developers may be highly motivated by this and debugging and troubleshooting are also much facilitated. In conclusion, many backend engineers assert that the frontend is simpler since it is more approachable, has access to tools, and is more visible and concrete. Because of this, a lot of developers are switching from the backend to the frontend, and this trend is probably going to continue.

What is Frontend & Backend Development?

Frontend development (client-side development) refers to the development of the parts of a website that the user can see and interact with. This includes code that is responsible for the look, feel, and behavior of the website and includes HTML, CSS, and JavaScript. 

Backend development (server-side development) is the creation of sections of a website that the user does not directly view or interact with. This contains program code for databases, servers, and APIs that manage and handle the website’s data.

What’s the Difference?

The main distinction between frontend and backend development is that the former concentrates on the external components of the website, whilst the latter does so for its internal components. Backend development is in charge of data processing and storage, whereas frontend development is in charge of the appearance, feel, and functionality of the website.

Frontend developers build the aesthetics, style, and interaction of the user interface using HTML, CSS, and JavaScript. The logic, databases, and APIs that power the user interface are created by backend developers in languages like PHP, Python, Java, and Ruby. Backend development is concerned with how the user interface works and interacts with the server-side logic and data, whereas frontend development is concerned with how the user interface appears and feels.

Why Is Frontend Harder Than Backend?

Why is it that some claim that frontend development is more difficult than backend development these days? There are several reasons why this is so, let’s look at them.

Keeping up with a rapidly changing environment

The rapid advancements in frontend development have given it a reputation for being challenging. Every few months, new frameworks and technologies like React, Angular, and Vue are released to improve development. These continual updates mean that staying up-to-date requires constant learning of new lessons and courses. Once Angular was the most popular frontend framework, but now React is the preferred choice for many companies. Even Netflix has gone back to using the original JavaScript due to performance concerns. With no indication that these advances will soon slow down, it’s important to remember how quickly the industry is developing the next time someone claims that frontend development is easy.

More information to consider

Frontend development may prove to be equally challenging in 2023 as backend development. With opinionated frameworks, state management systems, and intricate logic, there should be no assumption that the workload for backend developers is greater than that of frontend developers. However, frontend development entails more than just programming, as it demands creativity, aesthetics, and an understanding of user experience. This includes being adept with design techniques, creating prototypes, and making sure the design looks professional. Furthermore, it necessitates taking into account how users will interact with the software to deliver the best user experience.

More tools to learn

As the workplace evolves, so too must your skillset. Keeping up with the latest tools, such as Webpack, React, Yarn, and NPM can be a challenge, as you may find yourself constantly learning new technologies, leaving less time to learn other programming topics, such as different paradigms, languages, and best practices. Nevertheless, it is important to remain up-to-date and not be discouraged by the ever-changing landscape.

Test suites and testing

Testing the frontend of a web application is more difficult and tedious than the back end. In addition to checking for the theoretical soundness of functions and objects, and assessing edge scenarios, frontend testing requires tests for design components, logical operations, and state changes. As such, manual testing is often preferred over creating a unit test suite, which is more time-consuming and frustrating. All in all, frontend testing is more complex, laborious, and frustrating than backend testing.

Why Is Backend Harder Than Frontend?

Both backend and frontend development have specific explanations for why they are more difficult.

The higher learning curve for beginners

Compared to frontend development, learning backend programming can be more difficult. To build a website’s frontend, only HTML and CSS are needed. However, the backend requires a deep understanding of programming languages. This can be daunting for newcomers and lead them to believe that frontend development is easier. In reality, the learning curve for the backend is much steeper than for the frontend.

Frontend is less visually appealing than the backend

Just knowing where to look can help you find the backend, which can be just as aesthetically pleasing as the frontend. However, with frontend development, you can often see the effects of your changes in real time. The response time for the backend can be unpredictable, making it more challenging for a beginner.

Many backend languages

The complexity of learning backend languages can be attributed to their variety and the need to comprehend multiple languages. While frontend development only requires knowledge of JavaScript, HTML, and CSS, backend development involves mastering three languages to work with the various methods available. Although the concepts are generally the same, transitioning between languages can be challenging, leading many to stick with the language they are most comfortable with or switch only when necessary for a better career opportunity.

Summary 

So, which is harder, the backend or the frontend? The truth is that both types of development are equally difficult, but for different reasons. Frontend development necessitates comprehension of design concepts and user experience, as well as the ability to produce an aesthetically beautiful user interface. Awareness of server architecture, security, and strong technical language and framework knowledge are all necessary for backend development. In the end, both styles of development are essential for a successful product, and they each call for a unique set of talents. The distinctions between the two and the many tasks that each may be utilized for must be understood. You can more readily pick which form of growth is best for you if you are aware of the distinctions.

The post Why Do Some Programmers Say Frontend Is Easier Than Backend? appeared first on Flatlogic Blog.

Flatlogic Admin Templates banner