I asked ChatGPT to refactor the Forem StoriesController

Ben Halpern - Dec 6 '22 - - Dev Community

The Forem codebase, 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, 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)

Answer

This controller can be refactored to make it more readable and maintainable. Here are a few suggestions:

  1. Extract the DEFAULT_HOME_FEED_ATTRIBUTES_FOR_SERIALIZATION constant into its own class or module, so that it is not defined within the StoriesController. 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.

  2. Use the before_action callback to set the @page instance variable, rather than defining it in the index action. This will make the index action easier to read and understand, and will avoid repeating the same code in multiple actions.

  3. Extract the handle_user_or_organization_or_podcast_or_page_index and handle_base_index methods into their own private methods, so that the index action is not cluttered with a large amount of code. This will make the index action easier to read and understand, and will allow the code to be more easily reused in other parts of the controller.

  4. Extract the assign_hero_html, get_latest_campaign_articles, and redirect_to_changed_username_profile methods into their own private methods, so that the show action is not cluttered with a large amount of code. This will make the show action easier to read and understand, and will allow the code to be more easily reused in other parts of the controller.

  5. Use the before_action 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.


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, but demonstrated a tremendously powerful application in how it is being used.

There is much more exploration to be done.

Happy coding!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player