DEV Community

Cover image for How an AI coding companion keeps my code DRY
Jenna Pederson for AWS

Posted on • Originally published at community.aws

How an AI coding companion keeps my code DRY

One of the challenges with working in a large code base is that you have remember all the things: coding styles, best practices, your own company's patterns, your custom libraries and their APIs. If you aren't able to memorize it all (because most people can't), you're left digging through code or docs trying to figure it out, relying on tools to remind you about styles, or using pre-commit hooks to fix issues for you. Maybe you're waiting for that dreaded code review. They're either going to remind you that you're not using the established pattern and you have more work to do. Or they'll drop a comment in the pull request "LGTM" and merge it with barely a glance and then bugs are found later in production.

Instead of relying on that that grumpy code reviewer, you can use an AI coding companion (who's also a little less grumpy) customized to your unique code base, company's coding styles, best practices, patterns, and custom libraries and APIs. In this article, I will show how your company's Amazon Q Developer customizations will help you create more consistent code so you can keep your code DRY (the Don't Repeat Yourself principle of software development), improve on-boarding and knowledge transfer, and improve collaboration among your team.

Customization prerequisites

You'll need to have an Amazon Q Developer Pro subscription and your administrator will need to have already configured your access to the customizations. You can read more about the setup here (or just pass it over to your admin to handle).

In this article, I'm using a customization created based on the spring-petclinic projects and libraries. By creating the customization with your own unique coding style, libraries, and APIs, even different languages, Amazon Q will have more sample data to learn from and will be more equipped to match your intent to what it knows so it can provide better suggestions as you encounter various coding problems.

Let's check out a couple examples of how customizations work right in your IDE!

Creating a new entity using chat

I've been tasked with implementing a new entity, Order. Doing this without an AI coding companion means I'll need to look for existing patterns, copy/paste, find APIs we need, or code it up from scratch. This has the potential to create more code that is inconsistent with the rest of the code base. Instead, I'll use Amazon Q Developer to generate a code suggestion. I ask:

Create an implementation for a new entity called Order, in Java. Make sure to create the entity class and the repository implementation using JPA.

The response I get back is pretty generic:

import jakarta.persistence.*;
import java.time.LocalDate;

@Entity
@Table(name = "orders")
public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private LocalDate orderDate;

    @Column(nullable = false)
    private String status;

    // Constructors, getters, and setters

    // Additional fields and relationships as needed
}
Enter fullscreen mode Exit fullscreen mode

I know that my team has established patterns for this code base that I should be following. This suggestion doesn't adhere to these patterns or use our libraries because Amazon Q doesn't have the full context. In this case, I know that the Order class should be extending the BaseEntity class that defines some base properties and logic that all of this project's entities should have. Imagine you're a developer new to this project. You may not have the same context I have.

This is where a Amazon Q Developer customizations can come in to help us. If we have a customization created and the chat customization feature (now in Preview) is enabled, we can ask Amazon Q again and get more customized information. I've written before about asking Amazon Q chat for code examples and we'll do the same here after selecting a customization to use.

When getting started with our work, we can select our code base's customization. First, select Amazon Q in the lower menu.

Orange arrow pointing to Amazon Q item in lower menu in VSCode.

Then in the popup menu, select "Select Customization".

Amazon Q command pane in VSCode with Select Customization highlighted in orange

And then select your customization to use.

spring-petclinic customization highlighted in orange

Now when I ask Amazon Q the same question, I get the following suggestion, which is using our established pattern of extending BaseEntity.

@Entity
@Table (name = "orders")
public class Order extends BaseEntity {

    @Column (name = "order_date")
    private LocalDate orderDate;

    @Column (name = "total_amount")
    private Double totalAmount;

    // Constructors, getters, and setters

    public Order() {
        // Default constructor
    }

    public Order(LocalDate orderDate, Double totalAmount) {
        this.orderDate = orderDate;
        this.totalAmount = totalAmount;
    }
    // Getters and setters

}
Enter fullscreen mode Exit fullscreen mode

This is where Amazon Q Developer takes the selected customization and uses it to match my intentions to what it has learned so that it can generate a customized suggestion for me.

Creating a new entity using inline code suggestions

In the example below, we could also create the shell of our class file and use a comment to get an inline code suggestion from Amazon Q Developer to create this Order entity.

package com.anycompany.order;

// class representing an order entity using JPA
Enter fullscreen mode Exit fullscreen mode

After triggering Amazon Q, the following code might be suggested if we're not using a customization:

package com.anycompany.order;

// class representing an order entity using JPA
import javax.persistence.GenerationType;
import java.util.Date;

@Entity
@Table(name = "orders")
public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "order_date")
    private Date orderDate;

    // constructors, getters and setters
}
Enter fullscreen mode Exit fullscreen mode

If we select a customization and then trigger Amazon Q inline again, we would get a customized suggestion like the one we got using chat, including the use of the BaseEntity superclass.

As with other interactions with Amazon Q, if the suggestions aren't what you're looking for, you can either accept one and tailor it to your needs (you probably will have to do this at some point) or you can provide more context to nudge it in the right direction. You could import libraries you know you want to use, add some additional comments, and ensure you're using descriptive function and variable names. doStuff isn't going to cut it here!

Now that we've checked out how this works in action, let's review some of the benefits we get.

Keep your code DRY

Using a customization could be really powerful in the context of a large, quickly moving code base with many developers working in it. In the first example, it helps us keep our code DRY so that if the underlying implementation ever changes, the calling code doesn't also need to be changed.

When keeping our code DRY, we write less code. Our code becomes more readable (assuming the code we're calling uses descriptive names) and is easier to maintain and test.

It also means less can go wrong.

Improve on-boarding and knowledge transfer

Imagine if I'm new to this code base and don't yet know about all the existing libraries and APIs. I can ask Amazon Q to explain code snippets from a code base I'm just discovering or ask it to provide example code. I can describe my intent in a comment or function signature and Amazon Q will suggest code based on examples it learned from my company's private code base.

I've been using Amazon Q Developer's chat feature quite a bit to learn more about how a piece of code works, ask questions about my options for design and architecture or how to implement something, but now I can get suggestions based on my selected customization.

Improve collaboration

I mentioned earlier that grumpy code reviewer (we've all experienced one, right?!). Writing consistent code like we've covered here can also improve collaboration during code reviews and pair programming. Not only does my code reviewer or pair not have to remind me to use our already written, maintained, and tested library, but I have easy access to coding styles, standards, and best practices. Code suggestions while using a customization will already incorporate these and it's less likely that we'll argue over two spaces or four or whether or not we need to implement getOrderTotal three different ways.

Wrapping up

If you're working in a large code base, you might benefit from using a customized AI coding companion, like Amazon Q Developer. Today, I showed how using a customization based on your company's patterns, best practices, style guide, libraries, and APIs can help with readability and maintenance, on-boarding and knowledge transfer, and collaboration.

The Amazon Q Developer customizations feature for inline code suggestions just left Preview and the chat customization feature was released in Preview, both on July 10th. You can create your own customization (or ask your admin to set it up) by following these steps. Both features are continuously being improved based on your feedback, so if something isn't working as you expect, you find a bug, or even have suggestions to improve how it works in your development flow, let me know in the comments!

If you haven't yet tried out Amazon Q Developer in your development workflow, check out how to get started in VSCode or JetBrains IDEs.

Top comments (0)