A Software Developer’s Guide to Generative AI

Accelerate your code development and automate repetitive tasks with generative AI tools like GitHub Copilot. Here’s how.

Written by Jason Beres
Published on Jun. 13, 2023
Image: Shutterstock / Built In
Image: Shutterstock / Built In
Brand Studio Logo

We have officially entered the era of generative AI with tools like GitHub Copilot, ChatGPT and DALL-E gaining more prominence everyday. And while some people are still asking, “What is generative AI?” there’s just as many asking, “How will this affect my job?” and “How do I get started with it?” And of course, there’s those who have already fully hopped on the bandwagon. At Infragistics, we currently have multiple ongoing experiments testing new features from OpenAI and ChatGPT.

3 Software Development Tasks Generative AI Can Help You With Right Now

  1. Code generation and completion: Generative AI tools can automatically suggest or generate sections of code.
  2. Software testing: Generative AI is able to create tests and identify mistakes.
  3. Code optimization: AI tools can recommend code modifications to boost performance or streamline the architecture.

At first glance, generative AI can seem like a beast. There’s so many features to access and different ways to use it, but you don’t have to do everything all at once. Here’s how software developers can get started with generative AI.

 

Introduction to Generative AI and Coding

Microsoft released GitHub Copilot two years ago. It’s an AI-based programming assistant that autocompletes code and helps developers accelerate hard tasks. It utilizes OpenAI Codex and GPT-3, which are large language models optimized to generate code in response to natural language questions. 

Microsoft also recently released a preview of GitHub Copilot Chat, which is more of a ChatGPT-like interface that lets developers ask questions about code, convert code from one language to another, explain code and more.

With GitHub Copilot, you have multiple options to accelerate your development:

  • AI-powered Intellisense.
  • Comment prompts and questions. 
  • Interactive Chat.

No matter what type of applications you are building, you’ll be able to find a Copilot-enabled feature that will help you write code easier and faster, driving efficiencies for your software developer team.

A screenshot of GitHub Copilot
An example of GitHub Copilot Chat. Copilot is explaining what the highlighted code is doing, and then offering a suggestion on a possible next step example that the developer might need. | Screenshot: Jason Beres

More on Software DevelopmentWhat No-Code Can Tell Us About the Future of AI in Workplaces

 

How to Use Generative AI to Automate Repetitive Tasks

With GitHub Copilot, developers can accelerate various development processes, and as a result, dial back their time spent on repetitive tasks. Developers using Visual Studio, Visual Studio Code, Neovim or JetBrains can try Copilot directly in that IDE today. When GitHub Copilot is enabled, you get real-time autocomplete style suggestions for every aspect of your code that you are typing. In addition, using the comment prompt (//), you can ask GitHub Copilot for anything from help on syntax to writing entire code blocks or pages. 

For non-Visual Studio developers, ChatGPT is an excellent tool for the same accelerators. The only difference is that GitHub Copilot operates in-context with your code as you are typing, while ChatGPT is an interactive chat experience. This means you would ask ChatGPT questions about your code, for testing purposes or potential security issues, or just ask it to write entire functions for you. You can give ChatGPT information about your data schema, and then ask it to write a create, read, update, delete (CRUD) web API for you in Java or Python. It will do this in less than a minute, where it might take you several hours based on the complexity of your code. This is where generative AI shines, and where developer productivity can skyrocket. 

A tutorial on how to use generative AI tools like ChatGPT to write code. | Video: Fireship

 

What Can You Use Generative AI Tools to Do? (With Examples).

Developers can use generative AI tools like Github Copilot to accelerate repetitive tasks like:

 

Code Generation and Completion 

As a developer types, software can automatically suggest or generate sections of code. This can cut down on errors and considerably speed up the development process. Microsoft reports that 46 percent of a developer’s code is created by GitHub Copilot, which is a significant number that will only increase with more adoption and more sophisticated models behind the AI.

 

Example

Ask GitHub Copilot questions by prefixing your question with //, like this:

// can you write a function that takes an array of CompanyName and loops through the array and returns a new array with the company names reversed

GitHub Copilot will return this as an example of the output:
 

// example: ['Apple', 'Microsoft', 'Google'] => ['elppA', 'ftroproC', 'elgooG']

As you hit the “Tab” and “Enter” key, GitHub Copilot will continue to write the lines of code that you asked for. It generates the new code commented out so it won’t run and return errors as it types, just remove the // and run your new code.

// function reverseCompanyName(array) {
//   let newArray = [];
//   for (let i = 0; i < array.length; i++) {
//     let reversed = array[i].split('').reverse().join('');
//     newArray.push(reversed);
//   }
//   return newArray;
// }

 

Software Testing

AI can increase the effectiveness of automated testing, a significant component of contemporary software development. AI is able to create tests, recognize when a system is doing improperly and give precise feedback on the types of mistakes that may have occurred.

 

Example

We’ll continue to ask GitHub Copilot to work with the code it just generated to create different types of unit tests.

// write a unit test for reverseCompanyName
// describe('reverseCompanyName', () => {
//   it('should return an array with the company names reversed', () => {
//     let array = ['Apple', 'Microsoft', 'Google'];
//     let result = reverseCompanyName(array);
//     expect(result).toEqual(['elppA', 'ftroproC', 'elgooG']);
//   });
// });

// write a unit test for reverseCompanyName that fails
// describe('reverseCompanyName', () => {
//   it('should return an array with the company names reversed', () => {
//     let array = ['Apple', 'Microsoft', 'Google'];
//     let result = reverseCompanyName(array);
//     expect(result).toEqual(['elppA', 'ftroproC', 'elgooG', 'test']);
//   });
// });

// write a unit test for reverseCompanyName that checks that the type of the return value is an array
// describe('reverseCompanyName', () => {
//   it('should return an array with the company names reversed', () => {
//     let array = ['Apple', 'Microsoft', 'Google'];
//     let result = reverseCompanyName(array);
//     expect(result).toEqual(['elppA', 'ftroproC', 'elgooG']);
//     expect(typeof result).toEqual('object');
//   });
// });

 

Code Optimization 

AI can also recommend code modifications or implement them automatically to optimize the code. This can involve anything from minor adjustments that boost performance to significant adjustments that streamline the code’s architecture, or simply cut down on the amount of code required.

 

Example 

In this example, we’ll switch gears to try something in ChatGPT. This code is a perfectly acceptable approach to count the number of items in an array:

function countNumbersPoorPerforming(arr, target) {
  var count = 0;
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      count++;
    }
  }
  return count;
}

However, what if I have a very large number of items? This function is iterating over the array arr using a for loop, and then it increments the count variable whenever we find a match between the current element and the target value. The problem is as the size of the array increases, the function’s execution time will grow linearly.

Let’s ask ChatGPT how it can help. In the prompt, I asked:

If I have a very large array, is this the most optimized code to count numbers in the array? 

function countNumbersPoorPerforming(arr, target) {
  var count = 0;
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      count++;
    }
  }
  return count;
}

ChatGPT replied with a comprehensive explanation with a new function to try:

“The code you provided is a straightforward approach to counting the occurrences of a specific number in an array. It iterates through each element of the array and increments the count if the element matches the target value. While this code will work correctly, its performance can be improved for very large arrays.

If you have a very large array, there are more efficient ways to count the numbers. One approach is to use a hash table or an object to store the counts of each number in the array. This way, you only need to iterate through the array once, and for each element, you can increment its count in the hash table. Here’s an example:”

function countNumbers(arr, target) {
  const countMap = {};
  
  for (let i = 0; i < arr.length; i++) {
    const num = arr[i];
    countMap[num] = (countMap[num] || 0) + 1;
  }
  
  return countMap[target] || 0;
}

More on Software DevelopmentCan Low-Code Tools End the Developer Shortage?

 

Advantages of Using Generative AI Tools in Software Development

When developers can automate their more repetitive tasks, they have more time to focus on complex and creative projects. Developers can go back to the projects that made them start as developers in the first place–with creativity leading the way. In fact, according to Github Copilot, 74 percent of developers using the tool reported that they felt less frustrated when coding and that they were able to focus on more satisfying work.

Some of the generative AI capabilities that developers can tap into for these more complex and creative projects, include:

 

Database Optimizations

AI tools can create data schemas, suggest schema improvements and generate data based on schema information. 

 

Advanced or Predictive Analytics 

AI tools like ChatGPT can very quickly answer analytics questions about your data. For example, if you pass it a data set, it can find outliers, correlations, regressions, future predictions and other deep data-related insights. A word of caution: Anything you upload to open tools like ChatGPT will be informing its model. Do not pass confidential information as you experiment.

 

Image Generation 

While some are better than others, developers that need images generated for projects can get them in a snap with AI tools. Building a website selling exotic surfboards? With the right prompts, you can get high-quality digital images that can be used in your applications. 

 

UX/UI Design 

AI can help in the UX design process by suggesting or creating user interface designs, predicting how users will interact with the design and suggesting improvements based on user feedback and usage data. While this is a very sophisticated expectation of AI, the more information you give the model, or the better the prompt, the more useful the outcome will be.

Instead of looking at AI as “taking” work away, look at it as a new partner in your workflow. Start experimenting with different AI tools that map to your needs, and work with your team on how to integrate them into your pipelines. While some of this is just individual accelerators (like GitHub Copilot to help write code faster), other AI capabilities like natural language processing (NLP) in custom Chatbots are enterprise-level capabilities that can be game changers for an organization and your customers. 

More on Software DevelopmentGetting Started With OpenAI Gym

 

Common Mistakes to Avoid While Using Generative AI

While generative AI is a game changer for productivity, there are caveats that every developer should be aware of. The code generated is based on what the model is aware of – it is learning from the millions of GitHub repositories and other public code repositories and sources. 

If there is not a large body of publicly available code using a library, Copilot may not be able to give meaningful code suggestions. Python, the world’s most popular language, has a very high (40 percent) accepted code rate using Copilot, along with other popular languages that have billions of lines of code publicly available like JavaScript, TypeScript, C# and Go. You may not have as high of a success rate if you are using something newer that does not have a large body of work available to the models.

Other items to consider are:

  • Because neither GitHub Copilot or ChatGPT are testing the code that they recommend, the results they suggest may not work or make sense.
  • The AI tooling does not necessarily know the latest versions of frameworks or libraries, so it may suggest code that is out of date.
  • Your prompt is the most important part of a successful output from the AI tools. Experiment by asking questions in different ways, and researching how others are getting results.
  • Non-English language prompts won’t always get the best results, as the majority of public repositories used are in English.

The best practice with any AI tool is to treat AI generated content just like you’d treat code from any public source that you did not create yourself. This means thoroughly testing for functionality and performance and ensuring there aren’t security vulnerabilities or maliciously generated code.

Explore Job Matches.