I asked ChatGPT to refactor the Forem StoriesController
Tags: ai, gpt3, openai, ruby
Published: 2022-12-06 15:01:54

The [Forem codebase](https://github.com/forem/forem), which powers DEV and other networks, is open source, and is therefore great for demonstration purposes.

Rewriting existing code, or refactoring, is a really interesting use case for [ChatGPT](https://chat.openai.com/), and the chat interface is potentially the cleanest way to make use of this potential while still maintaining some common-sense control over the process.

Here were my results:

***

## Prompt

> Refactor this Ruby on Rails controller:
> (Pasted code from [here](https://github.com/forem/forem/blob/main/app/controllers/stories_controller.rb))

## Answer

<div class="request-:R2dm:-10 markdown prose dark:prose-invert break-words dark"><p>This controller can be refactored to make it more readable and maintainable. Here are a few suggestions:</p><ol><li><p>Extract the <code>DEFAULT_HOME_FEED_ATTRIBUTES_FOR_SERIALIZATION</code> constant into its own class or module, so that it is not defined within the <code>StoriesController</code>. This will make the controller easier to read and understand, and will allow the constant to be easily reused in other parts of the application.</p></li><li><p>Use the <code>before_action</code> callback to set the <code>@page</code> instance variable, rather than defining it in the <code>index</code> action. This will make the <code>index</code> action easier to read and understand, and will avoid repeating the same code in multiple actions.</p></li><li><p>Extract the <code>handle_user_or_organization_or_podcast_or_page_index</code> and <code>handle_base_index</code> methods into their own private methods, so that the <code>index</code> action is not cluttered with a large amount of code. This will make the <code>index</code> action easier to read and understand, and will allow the code to be more easily reused in other parts of the controller.</p></li><li><p>Extract the <code>assign_hero_html</code>, <code>get_latest_campaign_articles</code>, and <code>redirect_to_changed_username_profile</code> methods into their own private methods, so that the <code>show</code> action is not cluttered with a large amount of code. This will make the <code>show</code> action easier to read and understand, and will allow the code to be more easily reused in other parts of the controller.</p></li><li><p>Use the <code>before_action</code> callback to authenticate the user, rather than defining the authentication logic in multiple actions. This will make the controller easier to read and understand, and will avoid repeating the same code in multiple actions.</p></li></ol></div>

***

## Takeaway

This definitely validated my feelings that this could be a useful exercise, and could lead to major changes in how we do our work.

**Would it be useful to blindly implement these suggestions? No. Absolutely not (they're not even really correct, technically) — but it certainly introduces a tool which is... going to be a thing, one way or another.**

It is dangerous to get too caught up in what this type of technology can or cannot do well _today_, because some of those gaps can be closed with targeted application. ChatGPT as an application of GPT-3 is an example of just this. [ChatGPT is _less powerful_ than GPT-3](https://dev.to/ben/the-difference-between-chatgpt-and-gpt-3-19dh), but demonstrated a tremendously powerful application in how it is being used.

**There is much more exploration to be done.**

Happy coding!