Skip to content

Leveraging AI to Iteratively Improve Code Quality

Updated: at 02:12 PM

Leveraging AI to Iteratively Improve Code Quality: A Case Study with ChatGPT and Silicon Valley Code Camp

Introduction

Managing a mailing list for a large event like Silicon Valley Code Camp, with a user base of approximately 20,000 subscribers, comes with its unique challenges. While dealing with this complexity, I tapped into the capabilities of ChatGPT to not just write, but also iteratively improve an email obfuscator. What follows is an intriguing journey of code development and refinements.

The Genesis of the Obfuscator

The initial code generated by ChatGPT for obfuscating emails was as follows:

const obfuscateEmail = (email: string) => {
  const [user, domain] = email.split('@');
  const domainParts = domain.split('.');
  const obfuscatedUser = user[0] + '*'.repeat(user.length - 1);
  const obfuscatedDomain = '*'.repeat(domainParts[0].length - 2) + domainParts[0].slice(-2) + '.' + domainParts[1];
  return `${obfuscatedUser}@${obfuscatedDomain}`;
};

This function worked quite well but was vulnerable to edge cases. So, back to ChatGPT I went.

The Iterative Phase

My first refinement prompt to ChatGPT was:

“Make this TypeScript bulletproof.”

Although it did an acceptable job, it wasn’t quite what I wanted. So, I followed up with:

“For all the invalid cases, console log out the issue.”

That was better but not perfect. Thus, another round:

“Include the passed-in email as part of the console.log out. Also, call an internal function to console log the errors so that can be easily commented out.”

ChatGPT’s reply caught my eye:

“Excellent idea. Encapsulating the error logging in an internal function will make it much easier to manage.”

And so, the final code snippet materialized:

const obfuscateEmail = (email: string): string | null => {
  const logError = (message: string) => {
    console.log(`${message} Email: ${email}`);
  };

  if (!email.includes("@")) {
    logError("Invalid email format: '@' symbol missing.");
    return null;
  }

  const [user, domain] = email.split("@");

  if (!user || !domain) {
    logError("User or domain part is empty.");
    return null;
  }

  const domainParts = domain.split(".");

  if (domainParts.length < 2) {
    logError("Domain doesn't have a valid format.");
    return null;
  }

  const [firstPart, secondPart] = domainParts;

  if (!firstPart || !secondPart || firstPart.length < 3) {
    logError("Parts of domain are either empty or too short.");
    return null;
  }

  const obfuscatedUser = user[0] + "*".repeat(user.length - 1);

  const obfuscatedDomain =
    "*".repeat(firstPart.length - 2) +
    firstPart.slice(-2) +
    "." +
    secondPart;

  return `${obfuscatedUser}@${obfuscatedDomain}`;
};

The Real Value of AI in Coding

The back-and-forth with ChatGPT is an intriguing counterpart to professional code reviews, albeit facilitated by a machine. It’s not just about code generation but also about code improvement. The iterative process adds layers of robustness to your code, enhancing its quality and durability. This does wonders for productivity, as your coding speed is augmented by an AI-driven check on quality.

A Win-Win for Skill and Productivity

ChatGPT has significantly amped up my productivity. Contrary to the notion that AI might make us forget our coding skills, my experience has been educational. ChatGPT introduced me to effective patterns, like using JavaScript’s reduce in new ways. Now, when I see a piece of code that could benefit from using reduce, I don’t hesitate to refactor it, either manually or with ChatGPT’s assistance.

Conclusion

ChatGPT emerges not merely as a tool but as a formidable collaborator in the software development ecosystem. It’s not just about getting the code out the door; it’s about ensuring that the code is of high quality, efficient, and maintainable. In the competitive and ever-evolving landscape of technology, that’s not just an asset; it’s a game-changer.


Aside: Waving at the Future?

Mastodon Home Page Caption: Things don't always work out so well for driverless taxis.

I couldn’t resist sharing a somewhat surreal experience I had in San Francisco a few weeks ago. A driver-less Cruise taxi pulled up in front of someone’s house and stopped. The person came out, and to acknowledge the car, gave it a casual wave as they approached. Waving at a driver-less car? It was an odd yet fascinating glimpse into how we’re adapting (or not) to this new age of technology.

Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects