This is a prompt I use often to improve readability of a module. With something like Claude custom slash commands you can map these to an alias for easy use.
Prompt:
Analyze this code in the context of the following rules and apply appropriate refactoring.
Summary:
The goal of these rules is to improve readability within a file.
It is highly valuable to start at the top of the abstraction hierarchy
and descend the hierarchy as you read top to bottom.
Rules:
- Decompose all functions into sub-functions until the function body is four lines or less, or you cannot meaningfully extract anymore.
- If a sub-function is only called once, nest it at the bottom of the innermost calling function.
- Use named functions unless there is a strong benefit to using arrow functions or an IIFE.
- Organize code within a file such that function and method declarations are below ALL of their call sites. (stepdown rule).
- If a function has subfunctions inside it, they should be below the return statement(s)