AI Quality Gate: How ChatGPT and AI improve code quality

A software solution should always remain in motion. That is why, in this blog article, we take a closer look at the topic of Quality Gates and AI. Maintenance and continuous development are just as important as day-to-day operation. Through various approaches (team organization, company mindset), teams often try to keep delivery cycles as short as possible. Such dynamics can only work reliably if there is a high level of automation and transparency. An essential part of this is the Quality Gate, which helps improve code quality.

What does Quality Gate mean?

A Quality Gate defines in advance whether the next project steps are approved based on clearly defined quality criteria. But how can code quality be assessed in this context? In general, many techniques grouped under the term “Clean Code” revolve around one key question: how can a team of developers write code in a way that allows new team members to get started immediately, without a long onboarding phase? In addition to static code analysis (complexity, bug heuristics, etc. – keyword SonarQube), code reviews are another possible solution.

Who is responsible for code reviews?

Code reviews enable knowledge transfer from experienced colleagues to new team members.

However, they are often very time-consuming. For this reason, software architects should primarily be responsible for this task.

Let’s take a look at a typical deployment pipeline:

  1. Write code & commit
  2. Trigger build (unit tests, acceptance tests, integration tests, ...)
  3. Quality Gate (clean code standards, security, licenses, ...)


Only after these steps does the code review take place.

Code Review & Quality Gate

The role of AI

Let’s try out a code completion interface from OpenAI that can support code quality checks. Our sample project contains two different methods: one in TypeScript and one in Java. The TypeScript example is a simple addition function:

function add(x, y) { return x + y; }

The Java class, on the other hand, is rather hard to understand and uses naming conventions that do not clearly convey their intent:


import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SomeClass {

    static boolean sA(final String s) {

        String bKram = s;
        String sFe = fo(bKram);
        return bKram.equals(sFe);
    }

    static String fo(String bKram) {
        List<String> faef = Stream.of(bKram)
            .map(c -> c)
            .collect(Collectors.toList());

        StringBuilder dram = new StringBuilder(bKram).reverse();

        if (Objects.nonNull(faef)) {
            return dram.toString();
        }
        return null;
    }
}
                

Let’s try our luck:

const question = "Can you write unit tests for this code:";

We use the createCompletion API from OpenAI:


const completion = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: question + fileContents,
  temperature: 0,
  max_tokens: 3600,
});
                

And here we are:


@Test
public void testSA() {
    String s = "test";
    assertTrue(SomeClass.sA(s));
}

@Test
public void testFo() {
    String s = "test";
    assertEquals("tset", SomeClass.fo(s));
}
                

While it is still not entirely clear what the class actually does, its functionality has been extracted – and within seconds.


describe('add function', () => {
  it('should add two numbers', () => {
    expect(add(1, 2)).toEqual(3);
  });
});
            

In this process, OpenAI – ChatGPT recognizes that the example involves palindromes. As a result, the artificial intelligence understands the intention of the function:

AI and its value for Quality Gates

AI Quality Gate

Let’s return to the main topic and analyze the benefits of AI for Quality Gates. Does new code automatically mean that AI can write tests for it?
If a method is concise and has few side effects, it is likely interpretable and testable by AI.
This also means that AI can act as a filter to highlight areas that are particularly relevant for manual code review.
With artificial intelligence, our pipeline now looks like this:

  1. Write code & commit
  2. Trigger build (unit tests, acceptance tests, integration tests, ...)
  3. AI code review
  4. Quality Gate (clean code standards, security, licenses, ...)

AI in deployment pipelines

In this way, AI can be integrated into deployment pipelines and also applied to security aspects of the code we write. As a result, stability, maintainability, and overall software quality are improved. The most important outcome of using AI in Quality Gates is a significant increase in productivity within the development cycle.

Back to top

Sprache: