The use of AI models like GPT to format code based on a layout specification can offer several key benefits:
However, it's important to note that while AI can help significantly in this area, human oversight is still needed to ensure that the applied formatting is accurate and the resulting code is as expected. Also, while formatting is important for readability, it does not replace the need for good coding practices and well-structured code.
Note that given the formal structure of programming languages, formatting code can be effectively achieved using non-AI tools such as Prettier (many languages), GNU Indent (C++), Google Java Format (Java) and Black (Python) etc.
This purpose of this article is to demonstrate just how flexible AI tools such as GPT are and for small formatting tasks or for more obscure languages they may be a convenient alternative to using a dedicated code formatting tool.
Generate a post that describes what the following typescript function does and add comments to the function as required::
import * as bcrypt from 'bcrypt';
class UserAuth
{
static validateEmail( email:string ) : boolean
{
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
static validatePassword(password:string):boolean
{
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$/;
return passwordRegex.test(password); }
static splitEmail(email: string): [string, string, string]
{
const splitEmail =email.split("@");
const username= splitEmail[0];
const domainSplit = splitEmail[1].split(".");
const domainName = domainSplit[0];
const topLevelDomain = domainSplit[1];
return [username, domainName, topLevelDomain];
}
static async hashPassword( password :string ): Promise<string> {
const salt=await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
return hashedPassword;
}
static async validateHash(. password:string, hash : string. ): Promise<boolean>
{
return await bcrypt.compare(. password,hash );
}
}
Whilst a very simple and incomplete set of formatting rules were specified, GPT was able to format the code and correctly apply each of the rules.
Here's how our TypeScript code looks after applying the rules we've specified:
import * as bcrypt from 'bcrypt';
class UserAuth {
static validateEmail(email: string) : boolean {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
static validatePassword(password: string) : boolean {
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$/;
return passwordRegex.test(password);
}
static splitEmail(email: string) : [string, string, string] {
const splitEmail = email.split("@");
const username = splitEmail[0];
const domainSplit = splitEmail[1].split(".");
const domainName = domainSplit[0];
const topLevelDomain = domainSplit[1];
return [username, domainName, topLevelDomain];
}
static async hashPassword(password: string) : Promise<string> {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
return hashedPassword;
}
static async validateHash(password: string, hash: string) : Promise<boolean> {
return await bcrypt.compare(password, hash);
}
}
The formatted code now follows the rules we have specified, making the code is more readable and maintainable now that it adheres to a consistent style.
With AI, applying code formatting rules can be automated, saving time for developers.
Good coding practices are essential in software development. Consistent code formatting not only makes the code more readable but also helps in reducing bugs and improving code quality.
If you haven't already, consider incorporating automated code formatting into your workflow.
If you found this blog post helpful, feel free to check out our other blog posts on using AI in software development at the Logobean Blog!
Add your business name to instantly generate an endless selection of logos and brands.
Select your logo styles to refine the generated logos, click any logo to view it in the live previews & logo style guide and favorite the logos that you love.
Edit any logo to perfection using our intuitive logo and rich text editors.
Once you've found the perfect logo, download and use your logo package instantly!