﻿{"id":2770,"date":"2024-06-18T17:40:30","date_gmt":"2024-06-18T12:10:30","guid":{"rendered":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/?p=2770"},"modified":"2024-06-18T17:40:30","modified_gmt":"2024-06-18T12:10:30","slug":"ai-code-generation-key-points-and-cautions","status":"publish","type":"post","link":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/iedps\/ai-code-generation-key-points-and-cautions.html","title":{"rendered":"AI Code Generation: Key points and Cautions"},"content":{"rendered":"<h5><strong>Why use AI for code generation?<\/strong><\/h5>\n<p>Traditional coding is the fundamental way developers write code manually, line by line, in a specific language or framework as per the requirements. Developers have used this process over the years, developing logic and algorithms, sometimes even reusing them by creating libraries and distributing them.<\/p>\n<p>Many snippets <em>are<\/em> even present in blogs and Q&amp;A sites like stack overflow and a huge amount of knowledge is accumulated over the years. However, there are quite a few challenges associated with the traditional or manual approach.<\/p>\n<h6><strong>1.\u00a0\u00a0\u00a0\u00a0\u00a0 Time Consuming<\/strong><\/h6>\n<p>A complex task might require a lot of data structures and algorithm development from scratch. The functionalities also need to be written from scratch. This process might be repetitive and tiresome.<\/p>\n<h6><strong>2.\u00a0\u00a0\u00a0\u00a0\u00a0 Error Prone<\/strong><\/h6>\n<p>The developed code might be prone to errors. Bugs, typos, syntax errors, variable errors, missed logic, etc are just some of the issues that may pop up after coding. These can be difficult to track, fix, document, and are time-intensive<\/p>\n<h6><strong>3.\u00a0\u00a0\u00a0\u00a0\u00a0 Inefficient codes<\/strong><\/h6>\n<p>The developers tend to use and stick to a certain pattern of coding. Often, new code exploration is ignored as long as the existing codes work. This may lead to memory-intensive and space-intensive codes<\/p>\n<p>These are some of the issues that can be addressed by using Generative AI for generating code based on the needs and requirements.<\/p>\n<h5><strong>How does AI code generation work?<\/strong><\/h5>\n<p>The internet is a massive repository of knowledge. There are innumerable open-source codes, alternative codes, documentation, code snippets, blogs, online coding communities, etc., used to train a generative AI model. An LLM (Large Language Model) consumes all the data and familiarizes itself with coding, best practices, available libraries, syntax, versions, etc.<\/p>\n<p>The model is proficient in NLP to understand human inputs and process them into meaningful input for the LLM. Machine Learning is also leveraged to identify patterns, code snippets, understand context, understand the programming language, and predict an output structure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2839\" src=\"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-content\/uploads\/2024\/06\/MicrosoftTeams-image-22.png\" alt=\"\" width=\"638\" height=\"273\" \/><\/p>\n<p>AI Code Generation is not just a question-answer interaction between humans and AI.<\/p>\n<h6><strong>1.\u00a0\u00a0\u00a0\u00a0\u00a0 Input-Output<\/strong><\/h6>\n<p>This leverages NLP techniques to understand the requirements and generate the code accordingly. The developer breaks down the problem statement into technical low-level requirements and feeds it to the generative AI. The AI reads the input and analyzes the context and the programming language. Based on the input, a code is generated with generalized variable names and appropriate comments.<\/p>\n<h6><strong>2.\u00a0\u00a0\u00a0\u00a0\u00a0 Auto completion<\/strong><\/h6>\n<p>These kinds of models are generally integrated into IDEs. They leverage Machine Learning and Deep Learning to understand the intent, context, and coding style to predict and suggest variables, functions, or in some cases, even entire code blocks. The best example of this is the GitHub co-pilot.<\/p>\n<h5><strong>What are the advantages of using AI?<\/strong><\/h5>\n<p>There are advantages to generating code through AI over manual coding methods. Let us look at some of them below.<\/p>\n<h6><strong>1.\u00a0\u00a0\u00a0\u00a0\u00a0 Increased Productivity<\/strong><\/h6>\n<p>A lot of repetitive tasks and simple functionalities can be generated by AI, which leaves more time for the developer to work on innovation, design, and come up with solutions for complex logic.<\/p>\n<h6><strong>2.\u00a0\u00a0\u00a0\u00a0\u00a0 Error reduction<\/strong><\/h6>\n<p>The LLMs are trained on a massive volume of code and a vast number of codes. Although the error rate might not become zero, it significantly reduces the errors in generated code.<\/p>\n<h6><strong>3.\u00a0\u00a0\u00a0\u00a0\u00a0 Explore better options<\/strong><\/h6>\n<p>When AI generates code, it can be configured to provide a few alternatives. This helps in choosing a useful and better code.<\/p>\n<h6><strong>4.\u00a0\u00a0\u00a0\u00a0\u00a0 Improved Maintainability<\/strong><\/h6>\n<p>AI tools can suggest best practices and help in keeping similar coding styles. It can also generate comments along with the code, which helps in documentation.<\/p>\n<h5><strong>Key points to note with AI code<\/strong><\/h5>\n<p>Code generation using AI might seem an easy and simple process. But there are a few things to note when generative AI is involved<\/p>\n<h6><strong>1.\u00a0\u00a0\u00a0\u00a0\u00a0 Review and Edit<\/strong><\/h6>\n<p>The code needs to be carefully reviewed by a developer to check for any errors, inefficiencies, or security vulnerabilities. It is important to note that AI-generated code is not perfect. The code also needs to be vetted for fitment to project requirements, structure, and design. The developer needs to adhere to coding principles before allowing for any integrations with the project.<\/p>\n<h6><strong>2.\u00a0\u00a0\u00a0\u00a0\u00a0 Note Limits and Bias<\/strong><\/h6>\n<p>Using AI for highly complex logic might not be feasible as it might lack understanding of specific requirements, especially in niche domains. Bias can come into play because the models are trained on pre-existing codes. Any bias must be adjusted accordingly.<\/p>\n<h6><strong>3.\u00a0\u00a0\u00a0\u00a0\u00a0 Split tasks wisely<\/strong><\/h6>\n<p>AI is best used for tasks like common functionalities, repetitive codes, boilerplate codes, etc. More complex logic must be handled by developers. Different approaches to coding, strategic coding, prototype development, POCs must be taken up by developers. This leaves more room for innovation.<\/p>\n<h6><strong>4.\u00a0\u00a0\u00a0\u00a0\u00a0 Maintain Documentation<\/strong><\/h6>\n<p>Any code generated by AI, must be clearly marked, and documented with as many details as possible. This includes the edits made to the code and the reason for the modification. It is better to include the purpose of the code, as this can help improve maintainability.<\/p>\n<h5><strong>What cautions should I exercise?<\/strong><\/h5>\n<h6><strong>1.\u00a0\u00a0\u00a0\u00a0\u00a0 Accuracy and Reliability:<\/strong><\/h6>\n<p>AI-generated code is never 100% accurate and reliable. They are prone to errors, bugs, logical flaws, and security vulnerabilities. They may also be prone to biases based on the dataset used for training. Appropriate steps must be taken to review the code.<\/p>\n<h6><strong>2.\u00a0\u00a0\u00a0\u00a0\u00a0 Security Concerns<\/strong><\/h6>\n<p>The models are trained on historical data. The training data might contain hidden malicious code or some snippet which might potentially lead to data leaks due to security gaps. Another security concern is the integration of third-party libraries. The code must be thoroughly checked.<\/p>\n<h6><strong>3.\u00a0\u00a0\u00a0\u00a0\u00a0 Avoid Over Reliance<\/strong><\/h6>\n<p>AI should only be an assist to humans and not a replacement. Do not be over-reliant on AI-generated code. It can lead to reduced developer skills and be a detriment to innovation. Any AI tool is a black box behind the scenes. It is not possible to understand the reasoning behind the generated code.<\/p>\n<h6><strong>4.\u00a0\u00a0\u00a0\u00a0\u00a0 Ethical Dilemma<\/strong><\/h6>\n<p>License, ownership, and copyright notices need to be considered for not only the code but also for the AI tool used for the purpose. Bias is also another factor to be considered and to be adjusted accordingly.<\/p>\n<h5><strong>Summary<\/strong><\/h5>\n<p>Traditional or Manual coding is a time-consuming, error-prone, and potentially inefficient process. It is also tiresome and boring to write repetitive snippets. This can be accelerated by using generative AI and other AI tools that leverage LLMs and ML principles to generate code snippets, common functionalities, mid-complex functionalities, and other repetitive codes. However, it is also important to note that there are security concerns, accuracy, ethical dilemmas, and biased content even in AI-generated code. Hence, AI should only be used as an assistive technology rather than replacing developers. The codes generated must also be carefully reviewed, modified as necessary, and well documented to improve maintainability.<\/p>\n<p>Happy Coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why use AI for code generation? Traditional coding is the fundamental way developers write [&hellip;]<\/p>\n","protected":false},"author":624,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[4,1],"tags":[],"coauthors":[265],"class_list":["post-2770","post","type-post","status-publish","format-standard","hentry","category-artificial-intelligence","category-iedps"],"acf":[],"_links":{"self":[{"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/posts\/2770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/users\/624"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/comments?post=2770"}],"version-history":[{"count":7,"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/posts\/2770\/revisions"}],"predecessor-version":[{"id":2840,"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/posts\/2770\/revisions\/2840"}],"wp:attachment":[{"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/media?parent=2770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/categories?post=2770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/tags?post=2770"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.infosys.com\/emerging-technology-solutions\/wp-json\/wp\/v2\/coauthors?post=2770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}