Watch our latest Product Showcase

Group 370

Meet us at WeAreDevelopers World Congress in Berlin

Group 370

Spotlight Whitepaper by IDC on Importance of Automated Code Review Technologies

Group 370

Best Practices for Coding with AI in 2024

In this article:
Subscribe to our blog:

Developers are adopting coding tools powered by artificial intelligence (AI) at a rapid pace. According to Zero to Mastery’s recent survey, 84% of programmers they surveyed have at least some experience coding with AI. 

More than half (58%) of the developers surveyed said they use AI coding assistants to help them write code, with 46% of front-end developers stating that AI tools help them with 30% or more of their work. 

Even the 51% of programmers who said they have never used AI coding tools plan on trying them soon. More than a third (36%) of these late adopters say that the biggest blocker for them is learning how to use these tools effectively. 

How AI Coding Tools Help Developers 

The primary benefit of AI-assisted coding is increased productivity. Stack Overflow’s 2023 developer survey found that a third of polled developers believe increased productivity is the most impactful benefit of introducing AI assistants into their workflow. 

“I'm a big fan of the idea that code should be generated, not crafted. ChatGPT nails the first draft for me, and then it's my turn to bring in the handcrafted polish,” said Jasiek Gajdowicz, Senior Developer at Survicate.

AI tools excel in rapid prototyping, meaning they can almost instantly generate functional code, allowing developers to easily create a starting point that helps them explore various permutations and combinations.

Another productivity benefit of AI coding tools is the speed at which they can create documentation for your code. Developers can give an AI coding assistant a function and ask the tool to generate documentation and comments for the function and receive a draft in seconds.  

Developers have always used resources like Stack Overflow to research and find solutions. AI tools make this process even easier, acting as a rapid and focused search engine that allows you to quickly find the algorithm you need and generate code using it. AI tools also enable you to find and add a single algorithm instead of adding an extensive library to your application, potentially bloating your codebase. 

But as with any new technology, potential pitfalls of using AI to code do exist. Overdependence on AI and the potential for introducing inaccuracies and security issues into your codebase are common concerns for companies hesitant to integrate AI coding tools. 

To get the most out of AI coding assistants, teams must understand these tradeoffs and implement best practices that amplify the benefits of AI while safeguarding their applications and minimizing the potential issues and drawbacks. 

Best Practices for Leveraging AI Coding Tools

While speed and efficiency are priorities for development teams today, nothing should trump code quality. Interestingly enough, 77% of developers surveyed by Zero to Mastery believe that AI coding tools will positively impact the quality of code they ship. AI coding tools don’t just help you write code; there are AI coding solutions that help you review your code and give suggestions on how to improve it. 

The first step to getting the most out of coding with AI is recognizing the benefits and risks involved. The next step is understanding how to responsibly integrate AI-generated code into your application by following AI coding best practices. 

Encapsulate AI-Generate Code

Encapsulate AI-generated code into defined modules or functions to improve readability and usability immediately. Code encapsulation means wrapping a piece of code, typically a set of statements or functions, within a higher-level structure such as a function, class, or module. The encapsulated code is then treated as a single unit with a well-defined interface and can be invoked or interacted with as a cohesive entity.

Encapsulation promotes modular design by breaking down complex systems into smaller, manageable units. Each encapsulated unit can be developed, tested, and maintained independently, making the codebase more modular and easier to understand.

Encapsulated units can also be reused in different parts of a program or other projects. 

Document AI Usage Thoroughly

Proper documentation is vital for any well-maintained coding project, but it’s imperative when introducing AI-generated code into your work. 

Documenting the AI-generated code helps make the project more transparent. It allows team members, stakeholders, and future developers to understand how AI models are utilized within the codebase. 

It also ensures that the process of generating AI models can be replicated. Future developers or researchers may need to reproduce the AI models for validation, improvement, or adaptation to new data, and thorough documentation simplifies this process. 

As projects evolve, code may need to be updated or modified. Documentation aids in the maintenance process by providing insights into the original design and implementation of the AI models. When issues or bugs arise, well-documented code allows developers to troubleshoot more efficiently. It serves as a reference for understanding the intended functionality and identifying potential areas for improvement.

Documentation is also valuable for knowledge transfer between team members. It helps in passing on expertise and insights about the AI components, reducing the risk of knowledge silos within the team.

Use documentation tools and methods like JSDocs or Python Docstrings to clearly explain the purpose of your AI-created code, its parameters, returns, exceptions, and even its perceived limitations.

Familiarize AI Tools with Your Coding Standards

Provide the AI coding assistant with information about your coding standards, style preferences, and project-specific guidelines to help tailor the generated code to match your team's established best practices. 

While it’s tempting to jump right in and immediately ask the large language model (LMM) to generate a function that does this or that, equipping the AI assistant with knowledge of the rules and coding standards you want it to follow first will yield better code that requires less rework down the line. 

Create Specific and Detailed Prompts 

Craft prompts that are specific and contextual. Clearly articulate your requirements, including specific libraries, frameworks, or constraints. 

This helps the AI understand and generate code that aligns closely with your needs. As an example, check out this AI coding experiment performed by Codacy CEO Jaime Jorge to see how vital the prompt is to the outcome. 

AI coding tools don’t have the human intuition and context needed to understand what you want to achieve unless you provide detailed instructions. 

Review and Test AI-Generated Code Thoroughly

Avoid mindlessly copying and pasting AI-generated code without understanding its functionality. Take the time to comprehend the logic and adapt it to your project's needs. This ensures that the code integrates seamlessly and meets your specific requirements.

Adjustments may be necessary to meet coding standards, performance considerations, or project-specific nuances. Use static code analysis to catch potential code errors and issues that aren’t obvious. To enforce code quality standards, require code reviews and approval of all auto-generated code before acceptance.

Be mindful of code coverage to ensure your test suite always covers newly created code so that it doesn't slip through the cracks and into a release without thorough testing. 

Validate AI Coding with Human Expertise

AI is a powerful tool, however, human intuition and experience are essential for ensuring the quality and appropriateness of the generated code. AI can offer valuable suggestions and insights but should complement, not replace, human expertise. While AI can aid in decision-making, complex or high-stakes decisions require human judgment.

Developers should always cross-reference AI-generated code with established solutions and industry best practices and validate the logic and efficiency of the code based on company coding standards and personal know-how.

By conscientiously validating and thoughtfully integrating AI-generated code, you can capitalize on the speed and insights offered by AI while upholding the stringent standards necessary for successful embedded software development.

Create an Iterative Process  

Consider the previously mentioned best practices to refine and improve the code generated by the AI iteratively. This involves creating an ongoing process of generating, reviewing, and refining code and repeating the process until the desired outcome is achieved.

An iterative process for using AI coding tools could look something like this: 

  • Input your coding standards and style preferences
  • Break down your task into smaller, logical steps
  • Create a thorough and specific prompt
  • Review and analyze the generated code

If you’re not satisfied with the results, go back and refine these steps. Once you start receiving code you’re happier with, develop a further iterative process to make the code even better: 

  • Perform code reviews both manually and using automated static code analysis tools
  • Test the code
  • Make manual, “hand-coded” refinements

Iterative processes for validating AI-generated code quality are essential if your team wants to build a development culture that prioritizes code quality above all else. 

Security Best Practices for Integrating AI-Generated Code Into Your Codebase 

Security issues associated with AI coding are top of mind for all companies interested in integrating these tools into their development workflow. A recent research study showed that developers using AI coding tools wrote less secure code. More worryingly, they were more convinced that their code was, in fact, secure than developers who didn't use AI tools. 

Shipping reliable and secure applications should be prioritized over speed and productivity. We’ve recently seen security prioritization in software development take center stage, with more focus on establishing DevSecOps teams and processes, encouraging shift left testing, and following security frameworks like the Open Worldwide Application Security Project (OWASP) Top 10.

AI coding is new territory for everyone. That’s why security best practices are essential when integrating AI-generated code into your application and providing LLMs access to your company code. 

Restrict Access to Private and Sensitive Data 

One of the best features of AI tools is that they are trainable systems. Developers often feed AI code samples from their projects to guide them. Training the LLM helps AI analyze your code and even help you refactor it if necessary. 

When giving AI tools access, ensure you’re not allowing them to access restricted data in the process. If you haven’t already, create company-wide policies prohibiting giving AI tools access to intellectual property and other sensitive data. 

ChatGPT creators OpenAI are very aware of these concerns and have addressed them in ChatGPT Enterprise, their LLM that does not use your data or prompts as training data.

Regardless of the fail-safes that AI tools install, your team should also establish what checks need to be done both before and after an LLM interacts with your code in any way. 

Understand the Risk of Prompt Injections

Another security concern related to AI coding is that developers can’t be sure that the AI tool is secure and hasn’t been compromised at any point. One possible problem security teams need to be aware of is direct and indirect prompt injections. 

AI tools are attractive targets for cybercriminals because of their growing popularity. Prompt injections would enable attackers to inject malicious input into the LLM, which the AI tool can then pass to other systems. 

Direct and indirect prompt injections administer specific instructions or queries into a language model to influence output. These techniques are often used in fine-tuning or customizing a language model's behavior for a specific task or application.

For example, prompt injections could manipulate AI tools into recommending insecure coding solutions to developers, which, if implemented, could compromise application security. 

Practice Proper Secrets Management 

In software development, the term "secrets" pertains to sensitive credentials of a non-human nature. Secrets are the digital keys used for authentication and giving access to software data and systems.

Software applications today have transcended standalone entities and depend on an intricate network of third-party databases, cloud infrastructure, and various software components. These components are intricately interlinked and securely established through the utilization of secrets.

Implementing secrets management represents a fundamental cybersecurity practice for digital security teams. Defining this process ensures that access to secrets is restricted solely to individuals with proper authorization and authentication.

To ensure that your developers can’t accidentally or unknowingly offer up secrets within their AI-created code, it’s important to store secrets securely and encrypt them so that if they are revealed or stolen somehow, they still can’t be deciphered. 

Take advantage of automated code analysis tools with secrets detection capabilities and other security features.

Review Suggested Third-Party Dependencies

Incorporating third-party dependencies like external libraries, frameworks, or modules is a widespread practice that can save developers time, provide additional functionality, and improve overall efficiency. 

AI tools often recommend third-party libraries or frameworks if the tool believes that these dependencies could enhance your project. No matter who (or what) is giving these types of recommendations, you should review them thoroughly before incorporation. 

Don’t implement third-party dependencies blindly. Research them first to see if they are coming from a trusted source. When reviewing dependencies, you’ll want to know if they are coming from a credible and large community, if they are actively maintained, and whether known security vulnerabilities exist. 

It’s also a good idea to have an automated tool that scans and detects insecure dependencies in your application. If you do incorporate a dependency, be sure to test it in a controlled environment before deployment. 

Be Wary of Hallucinations 

AI models can occasionally produce inaccuracies or be led astray by erroneous data. These so-called “hallucinations” refer to instances where the AI generates entirely fictional or incorrect information. Similarly, misleading data may manifest as plausible outputs but are ultimately inaccurate or biased.

LLMs cannot discern when they are wrong or engaging in hallucinatory behavior. Unlike humans, who can make assumptions and perform tasks guided by limited facts, LLMs generate responses solely from learned patterns and information in their training data. They operate without consciousness or self-awareness and cannot assess the accuracy or correctness of their outputs and the data they learn from. 

An excellent example of AI tools producing hallucinations and vulnerable third-party dependencies can be seen in this study performed by security company Vulcan. The study identified an attack technique in which ChatGPT offered links to nonexistent coding libraries and generated questionable code snippets as fixes to common vulnerabilities. Cybercriminals could then hijack these fake libraries by publishing a malicious package in place of the library and hoping developers would install the infected library based on the AI tool’s recommendation.

Automated Code Review Tools Make Coding with AI Easier and Safer

AI coding tools are great helpers that aid developers in brainstorming and create code faster than ever before. However, developers need to treat AI-generated code like any other code. They must thoroughly test and review it before deploying it. 

Codacy is a perfect platform for development teams that want to take advantage of the benefits of AI coding tools while avoiding potential pitfalls. Codacy automatically scans all your human and machine-generated code, identifying code quality and security issues as you work. 

To test it out, sign up for a free 14-day trial today


Code Reviews: Best Practices
Because code reviews are a great tool to achieve higher quality code in a software development project, we will provide an overview and discuss best...
Is Coding with AI Secure? A Guide to Safe AI-Assisted Development
In a recent Gartner survey of more than 240 senior enterprise executives, generative AI was the second most frequently named emerging risk for...
Code Reviews in Large-Scale Projects: Best Practices for Managers
Code review in large teams and projects is daunting. Almost everything is against you: you have many developers and a lot of code spread over a large...

Automate code
reviews on your commits and pull request

Group 13