Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincej's avatar
Level 15

Refactoring with AI

I would like some advice from the wise:

I use PHPStorm. It has an integrated AI assistant which can be used with various LLMs. Additionally it has it's own product called Junie.

I use a lot of raw JS. some of it has become complex with very lengthy functions containing sub functions. Thinking my code was messy, I asked the AI assistant to refactor one such functions. It broke it up into a multitude of small functions, many containing only 2 lines of code with a return.

What was previously a easy to understand sequential flow of logic, albeit long, has in my opinion become an incomprehensible sequence of sub functions, where you have to jump all over the file to understand what the heck is going on. To my mind this is not progress.

So, please advise, keep what I have got, yes, tidy it up a bit, or go with what I think is grossly over engineered code?

Thank you!

0 likes
2 replies
LaryAI's avatar
Level 58

Great question! This is a common challenge when using AI-assisted refactoring tools.

Key Points:

  • Readability vs. Modularity: Breaking a long function into smaller ones is generally good, but over-splitting (especially into 1-2 line functions) can hurt readability and make the code harder to follow.
  • Sequential Logic: If your function’s logic is best understood as a sequence, sometimes a well-commented, longer function is more readable than a fragmented one.
  • Refactoring Goals: The goal of refactoring is to improve clarity, maintainability, and testability—not just to make functions smaller.

Advice:

  1. Balance is Key: Don’t feel pressured to accept every AI suggestion. Use your judgment. If the refactored code is harder to follow, it’s not a win.
  2. Group Related Logic: Break out distinct logical sections into well-named helper functions, but keep the main flow easy to read.
  3. Avoid Over-Engineering: Functions with only 1-2 lines often don’t add value unless they encapsulate a meaningful concept or are reused.
  4. Comment Wisely: Sometimes, a clear comment is better than a tiny function.

Example:

Suppose you have this long function:

function processOrder(order) {
    // Step 1: Validate order
    if (!order.items.length) throw new Error('No items');
    // Step 2: Calculate total
    let total = 0;
    order.items.forEach(item => total += item.price * item.qty);
    // Step 3: Apply discount
    if (order.coupon) total *= 0.9;
    // Step 4: Save order
    saveToDatabase(order, total);
    // Step 5: Send confirmation
    sendEmail(order.customer);
}

A good refactor might be:

function processOrder(order) {
    validateOrder(order);
    const total = calculateTotal(order);
    const discountedTotal = applyDiscount(order, total);
    saveToDatabase(order, discountedTotal);
    sendConfirmation(order.customer);
}

But a bad refactor would be:

function processOrder(order) {
    step1(order);
    step2(order);
    step3(order);
    step4(order);
    step5(order);
}

Where each stepX is just a line or two and their names don’t clarify intent.

Summary:

  • Use AI as a helper, not a replacement for your judgment.
  • Refactor for clarity, not just for function size.
  • Keep the main logic readable and sequential if that’s what makes sense.
  • Only extract functions when it improves understanding or reusability.

In short: Tidy up your code, extract meaningful helpers, but don’t over-engineer. Trust your instincts—if the AI’s refactor is harder to read, stick with your version and improve it incrementally.

Snapey's avatar

You are using revision control, so just undo it, and try again with a better prompt. Tell it how you want it to behave and what sort of output you expect.

keep iterating, improving the prompt until you get something you are happy with.

Please or to participate in this conversation.