Enhance Your Code: CodiumAI’s Automated Analysis and Suggestions for Python and JavaScript
Tom Smykowski
Posted on June 13, 2023
Writing code tests plays a vital role in software development, guaranteeing high-quality code, early bug detection, and enhanced maintainability. Yet, this process often becomes a time-consuming and monotonous endeavor.
Developers frequently find themselves allocating considerable time to writing tests, diverting their attention from actual development. Fortunately, the progress in artificial intelligence (AI) has opened doors to automated test generation, presenting developers with an innovative method for creating code tests. One such tool is CodiumAI, which allows you to automatically generate tests for your code.
With CodiumAI, you receive meaningful and straightforward test suggestions directly within your integrated development environment (IDE), enabling you to write better code. Let’s see how we can use CodiumAI on VSCode and PyCharm.
I will be keeping it simple and will only focus on how CodiumAI can be used to do an analysis of the code and also get suggestions.
How to Use CodiumAI to Generate Tests for Python
Let’s take a simple Python code to better understand how code analysis could be done using CodiumAI. The following function, ‘largest_prime_factor’ takes a number and tells the largest prime factor for that number.
For some people, this code could be difficult to understand. Along with that, this code is also not well-optimized. Let’s check out the analysis of the CodiumAI regarding our Python code.
Click on the ‘Generate tests for the below function’, to pop up the CodiumAI window. Once the CodiumAI tabs show up, click on the ‘Code Analysis’ tab.
As the name suggests, the analysis tab provides a detailed analysis of the code. This covers objectives, inputs, flow, outputs, and a few additional aspects. Almost anyone can easily understand the code by just simply going through the analysis. This makes it easier for other programmers to understand the code and also contribute to it.
You can also improve your code with AI-generated suggestions. For suggestions, click on the ‘Code Suggestions’ tab to get suggestions from the CodiumAI.
Under the ‘Code Suggestions’ tab, you will see multiple suggestions. Each suggestion contains two things, first the actual suggestion, and second, how your code will be affected by following the suggestion.
Following are the few suggestions given by CodiumAI regarding our Python code.
To apply the suggestion, just click on the checkbox on the top left of each suggestion. After checking the box, click on the ‘Prepare Code Changes’ button at the bottom right of the window. You can also select all the suggestions by clicking on ‘Select All’, which is present at the bottom left of the window.
Once you click on ‘Prepare Code Changes’ you will see a new window with the previous code along with the suggested changes for a better comparison. You can also see the list of suggestions that are being applied to the code.
After that click on ‘Apply to my Code’ to apply the changes. Right after clicking, your code will magically change into a well-optimized, functional code. Just a few clicks, it’s that simple.
How to Use CodiumAI to Generate Tests for JavaScript
We have seen how we can get detailed suggestions for our Python code. Now let’s see how we can do the same with JavaScript.
I have taken an easy and basic example of a Fibonacci series generator in JavaScript. The following function generates n number of terms in Fibonacci Series.
Is our code perfect? I mean it does the job but is it really perfect? Let’s see what CodiumAI thinks about this.
Click on the ‘Generate tests for the below function’, to pop up the CoidumAI window. Once the CodiumAI tabs show up, click on the ‘Code Analysis’ tab.
Once you click on it, a detailed analysis of the code is displayed. The analysis includes objectives, inputs, flow, outputs, and some additional aspects. As you can see in the screenshot, this is a pretty detailed analysis of such a small function that we wrote. Anyone can understand the code immediately by just going through the analysis. This might not look much helpful in this example that we are using, but this is very helpful once you are writing complex code. This makes it easier for others to understand your code.
This is the analysis part. The best thing is that we can get code suggestions for JavaScript too.
Click on the ‘Code Suggestions’ tab and you will be presented with some suggestions. The process is similar to what we followed for Python.
Under the ‘Code Suggestions’ tab, you will see multiple suggestions. Each suggestion contains two things, first the actual suggestion, and second, how your code will be affected by following the suggestion.
Following are the few suggestions given by CodiumAI regarding our JavaScript code.
To apply the suggestion, just click on the checkbox on the top left of each suggestion. After checking the box, click on the ‘Prepare Code Changes’ button at the bottom right of the window. You can also select all the suggestions by clicking on ‘Select All’, which is present at the bottom left of the window.
Once you click on ‘Prepare Code Changes’ you will see a new window with the previous code along with the suggested changes for a better comparison. You can also see the list of suggestions that are being applied to the code.
After that click on ‘Apply to my Code’ to apply the changes. This will convert your code into a well-optimized, readable code. I bet you haven’t seen an easier method of code refactoring than this!
As you can see CodiumAI is quite nice if it comes to code analysis and suggestions too. I found this feature by accident, and already using it often. Do you use any AI tools to improve your coding? Let me know about it, and what do you think about CodiumAI!
Subscribe for more coding articles and tools!
Posted on June 13, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
June 13, 2023