Move past the basic code bugs, testing and templating - AWS Code Whisperer

Move past the basic code bugs, testing and templating - AWS Code Whisperer

At this point, even people living under rocks are probably aware that all things “AI” are the soup du jour of the tech world. The AI/ML revolution that’s been going on has produced some Large Language Models (LLMs), which have done some incredible things. Things like passing law school exams, completing coding interviews for six-figure jobs, and making Bing relevant, to name just a few. This is all exciting, but if you’re like me, you’re wondering what you should be doing with all this technology as a software engineer. To that end, we will take a trip to Amazon’s coding companion called?Amazon CodeWhisperer.

The first question we must answer often sounds like, “I know how to write code; why should I waste my time with an AI code companion?”. Let’s discuss a few significant benefits of using a tool like CodeWhisperer.

Enhanced Productivity

CodeWhisperer has been trained on massive amounts of code. This means that when you’re trying to solve a problem, it has likely seen it, or something very similar, before and has some ideas on how to help you.?What this means is that, as an engineer, you can focus on novel and creative work, fostering innovation, instead of spending time on boilerplate tasks like Googling how to center a div for the three millionth time, ultimately enabling you to solve business problems more effectively.

Turbocharged Learning

Maybe Android just added another official language, or you’re a backend engineer with a friend with a “killer app” idea who just needs someone to “code some HTML” to make it a hit. Either way, it can be daunting to pick up a new technology while still maintaining your expected level of productivity. This is a sleeper candidate for the best feature of a tool like CodeWhisperer. The tool knows the language, and you know the problem domain. So go ahead and rewrite your Android app in Kotlin, or tell your friend you’re in to help him build “Twitter, but for bipedal birds.” CodeWhisperer has your back through the struggle of learning new syntax and keywords.

Time Savings

Let’s face it, there are many what I call “knucklehead” tasks that we have to deal with in our day-to-day work. You may be writing a DTO class, calling that repository method for the 9,000th time, writing a test case, or any other relatively simple task. This is the area where CodeWhisperer starts to shine through once you’re comfortable, as the tool will chew through the annoying boilerplate almost without you having to think about it.

Now I can hear you all screaming into your screens that excellent talk, “Talk is cheap; show me the code.”. I couldn’t agree more. Let’s make a quick application to demo some of the benefits. For the sake of brevity, we’ll do a short Rust program that will:

  1. Make a GET request to an API
  2. Retrieve the JSON
  3. Deserialize it to a struct
  4. Tell us how many humans are currently in outer space

Feel free to follow along in your favorite language and IDE. Currently, CodeWhisperer has the strongest support for Python, Java, JavaScript, TypeScript, and C#. It also supports code generation for Go, Rust, PHP, Ruby, Kotlin, C, C++, Shell scripting, SQL, and Scala. As for IDEs, it supports VSCode and JetBrains at the time of writing.

Prerequisites

You’ll need a supported IDE (VSCode or JetBrains) and the AWS Toolkit extension (VSCode,?JetBrains) to get started. Once you’ve set that up, we’ll open it up by opening the extension menu (this will default to your left sidebar if you’re on VSCode). Expand the CodeWhisperer menu, and give Start a click.

This will open a window asking us how we want to log in. I’m going to use my AWS Builder ID. You can find information about the AWS Builder ID here:?LINK

You’ll see a prompt to copy a code and log in or create your AWS Builder ID. You’re probably familiar with this authorization process already.?

Once the link has opened, you see a prompt to log in with your Builder ID or create one. Go ahead and go through whichever process is most appropriate for your situation. You’ll then need to allow the proper permissions for the Toolkit extension:

Hands-on Example

Congratulations, you’ve now successfully configured CodeWhisperer—time to get coding. Feel free to use any language you like if you’re going to follow along. We will let CodeWhisperer write almost all the code, so choose whichever supported language you want to see. We will start when our application shows us our favorite “Hello, world!” output to stdout. If you would like a copy of the program I ended up with, you can find it here:?https://github.com/caylent/codewhisperer-demo.?

Unless I specifically mention manually modifying things, any code written outside of comments, or ending braces, is all generated by CodeWhisperer.?The ending braces are essential to note because there are times when CodeWhisperer will continue generating lines until the cows come home, so you’ll want to use your judgment on when to cut it off.

Alright. So let’s see what CodeWhisperer can do. The first thing we wanted to do was retrieve a JSON response from an API endpoint. Let’s write a comment indicating that.

// Send a GET request to https://api.spacebits.net/people and return the response JSON string
        

When we add a new line after our comment, CodeWhisperer makes itself known with a suggestion for what it thinks our function signature should look like

This output looks ok, so we’ll hit TAB to insert this suggestion. Oh, look, we have more suggestions:

That’s the URL we want to use. Excellent. Let’s accept the next few and see how CodeWhisperer intends to finish this out

Well, this looks good at a glance, but as we know, we will only know if we test it. Let’s see what CodeWhisperer can do for us here.

This block above shows us that?sometimes?you might not get suggestions – you can force the suggestion to show up using opt + c (alt + c for my Windows friends), as I had to here, but once the cobwebs cleared, we get some valuable suggestions. Let’s see how it finishes out my test function.

Well, that’s not bad, it’s certainly what I asked for, but it’s not what we?intended?to test. Let’s reword that comment to indicate we want to know that the string has some value rather than simply checking a type.?

We end up with this, and it looks much more promising. Let’s run it and see what happens.

Hot dang, it passes. We have JSON. Next, we want to deserialize that JSON to a struct that matches. For our initial record, here’s a bit of the JSON structure we’re working with.

Very simple but also niche. Let’s see what CodeWhisperer does with this.

I cut this one off as it was more than happy to keep generating fields. This miss is understandable. The nuances of the specific data we’re using are a bit outside the scope of what I thought would work (and it’s?very likely?that since we’re using this data now, it’ll get anything related to it correct in the future). It did get a lot of the boilerplate stuff right, though, so simple enough to correct.

Alright, I’ll fix that manually since I know the data. Then I’ll create the struct which will hold the collection of people in space. This time I’ll provide a bit more data.



Well, this is cool. I did need to update the struct name manually, but the more information we provide, the more accurate the output will be. I’ll make a mental note of that.

We have the JSON string and structs so now we are ready to deserialize things. Let’s write a function that will do that. CodeWhisperer is suggesting a comment now, too.

Well, this could be cool. Is it going to write everything for us? Let’s see.?



Hmm. Well, this looks good. But, of course, you know what we need to do next. Write a test!


CodeWhisperer nails it again. Doing a unit test for a function that depends on another function isn’t great, but can we marvel for a second at the fact that CodeWhisperer understood the context of how we were using the data? One thing that I did change was the assert statement. CodeWhisperer wanted a len() check, and Clippy (a Rust linter) prefers the is_empty() check, so we’re going with that. Otherwise, this is all CodeWhisperer’s code. The test passes; you can take my word for it this time.

So at this point, we’ve retrieved the JSON and deserialized it into the structs. The last thing we want to do is have it output how many people are in space according to our data.?

CodeWhisperer has gotten us this far, let’s see if it can take us across the finish line.

Everything looks right. I know that, at this moment, there are ten people in space. Let’s run it and see what happens

Hah! Look at that. Let’s get greedy and ask it to print the names of each person, just for fun.

Once again, this passes the smell test. Let’s run it.?

Check that out. Our application does everything we set out to do. There were some language-specific things in the background we needed to handle ourselves, and we fixed the items specific to our data, but outside of those, we wrote no code ourselves save for a few closing braces. That’s pretty wild.

Additional Food For Thought

I would be remiss if I didn’t mention that CodeWhisperer has some features we didn’t hit on during this brief demo that are worth discussing. First, I want to call out that CodeWhisprerer will let you know if you’re using code that closely resembles the open-source data that it was trained on (https://aws.amazon.com/codewhisperer):

This great notification helps us avoid improperly copying entire blocks of code from other repositories. CodeWhisperer also has built-in security scanning to look for OWASP vulnerabilities in your code. We didn’t test this in our demo because of language support and the oversimple aim. If you’re using Python, Java, or JavaScript, you can use this feature.

Conclusion

We’ve done a simplistic example program to understand how to leverage CodeWhisperer in our workflows. We may be feeling pumped to jump right into our next app. Let’s take a second to talk about what we should consider with these tools.

Most importantly, remember the?Programmer’s Oath. Specifically, remember that you’re the engineer, and you are ultimately responsible for the code you push. Treat everything that CodeWhisperer writes as if it was written by someone you’ve never met. Trust, but verify.?

Additionally, always keep a weather eye out for potential security issues in code. One thing I’ve noticed, for example, is that some tools have a tendency to suggest?very?permissive policies in CDK projects. Be present when using these tools.

Lastly we want to understand, at least at a high level, how tools like CodeWhisperer are trained. As we mentioned earlier CodeWhisperer has been trained on massive amounts of valid, working code in many different languages. The large mix of languages is a big benefit to us as users regardless of our personal language choice because a model trained on many languages actually outperforms a model trained on a single language. They can also potentially solve problems in languages that they weren’t specifically trained on thanks, in part, to?few-shot (or even zero-shot) learning. If you’re interested in learning more about this, there’s an?Amazon research paper?that covers it. The training process is complex but learning more about it will better inform your expectations for how these tools will augment your coding practices.

With those caveats in mind, every software engineer, developer, architect, etc., should become familiar with AI tools such as Amazon CodeWhisperer. You will reap the benefits of enhanced productivity, turbocharged learning, and time savings. Just based on our simple example above, CodeWhisperer helped me complete the solution in about half the time it would normally take. For more complex examples I could see the potential for CodeWhisperer to reduce my time requirement by 70-80%. These are rough guesses at time saved, and your personal experience will very much depend on your knowledge of the language you’re using, as well as the specific problem you’re working on. In any case, with these productivity increases and time savings you will spend less time bogged down in the minute details of the code and more time providing value to your customers. Also, at least for the time being, CodeWhisperer is free to use. So go check it out, and let us know what you think!

If you like this edition, do share it with your network, sharing is caring.

If you want to connect with me one on one, I also do pro-bono and here is the link for your reference - https://topmate.io/rohan_girdhani

Happy learning.



要查看或添加评论,请登录

社区洞察

其他会员也浏览了