Skip to content
Notifications
Clear all

ELI5: Why does Aider sometimes rewrite code that was already correct?

2 Posts
2 Users
0 Reactions
55 Views
(@code_reviewer_anna)
Honorable Member
Joined: 7 months ago
Posts: 484
Topic starter   [#28823]

Hey everyone! 👋 I've been using Aider for a few months now and love how it supercharges refactoring. But there's one quirk I keep bumping into, and I'm curious if others have the same experience.

Sometimes, when I ask for a small change, Aider will rewrite a whole chunk of perfectly good, working code. It's not introducing bugs, but it feels unnecessary and can muddy the git diff. I think I've figured out a few reasons why this happens:

* **Context window limitations:** Aider sends a section of code to the LLM for context. The model might "rephrase" that code to fit its own style when generating the final output, even if the logic stays the same.
* **The "autocomplete" nature of LLMs:** They don't *edit* in the traditional sense; they regenerate text. If your existing code is syntactically correct but structured slightly differently from the model's training data, it might get rewritten into a more common pattern.
* **Over-enthusiastic refactoring:** If you ask to add a feature near a function that *could* be cleaner (e.g., using a list comprehension instead of a for-loop), Aider might "fix" that function while it's in there.

Here's a tiny Python example. Suppose I have this function:

```python
def calculate_total(items):
total = 0
for item in items:
total += item.price
return total
```

If I ask Aider, "Add a tax calculation of 10% at the end," it might return:

```python
def calculate_total(items):
subtotal = sum(item.price for item in items)
return subtotal * 1.1
```

The new code is greatβ€”maybe even better!β€”but it rewrote the loop into a `sum()` comprehension. The change I asked for was just `return total * 1.1`. The rest was "free" refactoring, which isn't always desired.

**My workflow tip:** I've gotten into the habit of staging my changes in git *before* using Aider. That way, I can easily review its diff and discard any unrelated reformatting or unnecessary rewrites before committing. It adds a step, but keeps the history clean.

Has anyone else found good prompts or settings to minimize this? I'm all for code improvements, but I prefer them to be intentional.


Clean code is not an option, it's a sanity measure.


   
Quote
(@data_pipeline_guy)
Reputable Member
Joined: 8 months ago
Posts: 388
 

You nailed the main reason. It's not editing, it's autocomplete on steroids.

The real headache isn't the git diff, it's when it "refactors" a complex, battle-tested SQL block into something "cleaner" that subtly changes the business logic because the model doesn't understand the underlying data model. Happens all the time with window functions.

You learn to give it much smaller, surgical prompts or just edit the file yourself.


SQL is enough


   
ReplyQuote