Create Logos

Using AI to Make Your Code Show Style!

Using AI to Make Your Code Show Style! blog article main image

A GPT-Assisted Approach

The use of AI models like GPT to format code based on a layout specification can offer several key benefits:

  1. Improved Code Readability
    AI can automatically apply formatting rules, making code easier to read and understand, which can enhance productivity and decrease the time required to understand new codebases.
  2. Consistency
    AI can enforce a consistent code style across a codebase. This makes the code easier to read and understand, especially in team environments where multiple developers contribute to the code.
  3. Increased Efficiency
    Manually formatting code can be a time-consuming process. AI can automate this task, freeing developers to focus on solving more complex problems.
  4. Reduced Errors
    Inconsistent code formatting can sometimes lead to errors or bugs. By enforcing a consistent style, AI can help prevent such issues.
  5. Customization
    AI models can be trained to format code according to custom style guides, making them adaptable to different project or company standards.
  6. Code Review Process
    AI can help in the code review process by ensuring that the code is consistently formatted, letting reviewers focus more on the logic and structure of the code.
  7. Learning Tool
    For novice developers, AI-formatted code can serve as a good example of how to properly format and structure their own code, promoting best practices.

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.

GPT Prompt

Generate a post that describes what the following typescript function does and add comments to the function as required::

  1. Indentation of 4 characters using spaces not tabs
  2. Opening braces to the right of the previous line of code
  3. Closing braces on a line by themselves
  4. A single space between function parameters and between the colon and parameter type
  5. A single space either side of the colon used for a function return type
  6. A single space either side of an any arithmetic or boolean operator, and assignment operators
  7. No more than a single blank line between logical blocks of code
  8. Only a single Variable or Const declaration per line
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 );
  }
}

Prompt Overview

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.

GPT Response

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.

Conclusion

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.

Was This Helpful?

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!

Blog CTA

Get Started For Free. No Signup Required.

1. Get started

Add your business name to instantly generate an endless selection of logos and brands.

Icon AI business name & slogan generator
Icon Select a business or general logo theme
Icon Select a logo name layout (for 2+ word names)
Icon Generate unlimited logos in seconds
Icon No sign up required
Start Logobean Logo Maker
Logobean logo maker get started
Logobean logo maker select styles live preview favorite

2. Select styles, live preview and favorite your logos

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.

Icon Select styles to refine your generated logos
Icon Filter logo layouts, icons, colors, fonts & more
Icon Revolutionary & interactive logo previews
Icon Live & interactive logo style guide
Icon Save logos that you love to your favorites
Start Logobean Logo Maker

3. Edit and perfect your logo

Edit any logo to perfection using our intuitive logo and rich text editors.

Icon Intuitive logo editors for every logo layout
Icon Rich text editors for your name and slogan
Icon Icon, color and font selectors
Icon Smart icon coloring
Icon Live preview your logo whilst editing
Start Logobean Logo Maker
Logobean logo maker edit logo editor
Logobean logo maker select styles live preview favorite

4. Download your logo files & more

Once you've found the perfect logo, download and use your logo package instantly!

Icon High quality PNG & SVG logo files
Icon 100+ on-brand designs
Icon Ready-to-upload social media profile & cover images
Icon Brand color palette
Icon Manage your logos, brand and downloads on-going
Start Logobean Logo Maker

Ready to get started?