Code Refactoring

A shared folder with AI prompts and code snippets

From workspace: Replit

Team: AI Prompts

Total snippets: 6

Replit

Code Refactoring

6 snippets

Refactor Callback Hell to Async/Await

Convert deeply nested callbacks into async/await syntax.

This code uses nested callbacks. Refactor using async/await. Code: getUser(id, function(user) { getSettings(user, function(settings) { saveData(user, settings, function(response) { console.log('Done'); }); ...

Refactor Global State Into Class

Encapsulate global variables and functions into a class.

This code uses global state. Refactor by moving everything into a class. Code: settings = {'theme': 'dark'} def get_theme(): return settings['theme'] def set_theme(t): settings['theme'] = t

Convert For Loop to Map/Filter

Simplify a loop using functional programming methods.

Convert the loop to a functional style using `map` or `filter`. Code: names = ['Alice', 'Bob', 'Charlie'] upper = [] for name in names: if len(name) > 3: upper.append(name.upper())

Extract Method from Repeated Code

Identify repeated logic and move it to a separate method.

Refactor by extracting common logic into a new helper method. Code: public class Report { public void printDaily() { System.out.println("==== Report Start ===="); System.out.println("Date: " + today()); ...

Convert Nested If Statements to Guard Clauses

Refactor nested if conditions into cleaner early returns.

Refactor this function by replacing deep `if` nesting with guard clauses. Goal: reduce cognitive load and improve structure. Code: function processOrder(order) { if (order) { if (order.items.length > 0) { if...

Refactor Long Function to Smaller Functions

Split a long function into smaller, readable, reusable units.

Refactor this long function into smaller helper functions. Goals: – Improve readability – Make code easier to test – Avoid repetition Code: def process_user_data(user): if '@' not in user['email']: raise ValueError('Invalid email') ...