2023/12/12

Ship It 5: Refactor a Big Block of Code Into a Mapped Config Using AI

Leverage AI to handle routine tasks such as refactoring, allowing you to focus on more critical aspects.

At times, during coding, we may find ourselves repeating code without giving much thought to its organization. We usually decide to address it when it begins to pose challenges, such as when we need to build additional features around it.

I find using AI for refactoring tasks like these extremely helpful. I use Cursor, an enhanced version of VS Code with integrated AI assistants.

The Process

  • Identify the code segment that requires refactoring.
  • If you're using Cursor, press Command + L. This action will bring the selected code into the context of the chat sidebar.
  • Provide a prompt. For example, if the code is a list, you could say: "Transform this into a configuration map. The keys should be href, title, and children."

Cursor will generate the refactored code for you. You can then copy and paste this code into your codebase.

Conclusion

Leveraging AI for code refactoring can save a considerable amount of time. It not only enhances the organization and readability of the code but also allows the developer to focus on other tasks. In this case, I saved approximately five minutes of refactoring time, which I could utilize for other critical tasks.