Lasted edited: March 7, 2025

A work in progress. Maybe.

I had this 1200 line main.py file that needed refactoring. The app, a media asset manager, was nowhere near completion. It was only gonna get longer. So, I thought, refactoring with AI shouldn’t take more than a day. A week later my main.py is 170 lines and the app ain’t exactly working like before. But it ain’t bad. So, I guess it’s time for another.

This time it’s with a 638-line file_utils.py.

Having A Conversation With Claude

After having talk with Claude, I got recommended to use pyan3. Not the best recommendation as pyan3 doesn’t hasn’t been updated in a while and doesn’t work with python 3.7 and above. I’m on 3.10 so that’s that.

In it’s place, Claude said to install pydeps. Which I did. But I’m developing on a Mac and I got:

pydeps calls dot (from graphviz) to create svg diagrams,
               please make sure that the dot executable is available
               on your path.

Installing graphvix on a Mac needs to be done with home-brew.

brew install graphviz

Okay. Got another error. That it wasn’t installed.

You must: brew install svn

And this is way refactoring files takes about a hundred years.

Now, I know I’m on a old Mac air. But I don’t do video processing. I’m just typing text, running a browser. Connecting to servers. I don’t need no stinking m4. And then there’s that configuring stuff that I hate to do.

Of course, on my 8-year-old Mac when I install with home-brew, it could take forever. Maybe it’s time to upgrade.

Okay, I got svn installed. Now installing graphviz. Fingers crossed. Time for coffee as home-brew is stuck on ==>make. I forgot what iI was installing, but one time it got stuck for hours.

And the winning answer to refactoring came from Gemini

For whatever reason, Claude never mentioned that I should just use the refactoring tool in vs code.

Instead, after feeling frustrated from Claude’s advice I switched to Gemini and got the winning advice.

1. Using VS Code's "Move Symbol" Refactoring (Recommended)

This is the most direct and efficient method.

Move the Function:
Open utilities.py.
Locate the resize function.
Right-click on the function name (resize).
Select "Refactor..." -> "Move Symbol to File...".
Type or select formatting.py.
VS Code will automatically:
Move the resize function to formatting.py.
Add the necessary import statement in formatting.py if it relies on other modules.
Update all import statements in your project that were using utilities.resize to now use formatting.resize.
Benefits:
Handles all import updates automatically.
Maintains code integrity.
Reduces the risk of errors.
Very fast.
2. Using VS Code's "Organize Imports" Feature (For Cleanup)

After moving functions, you might have some redundant or unused imports. Use this to clean up.

Organize Imports:
Open any Python file in your project.
Right-click within the file.
Select "Source Action..." -> "Organize Imports".
VS Code will:
Remove unused imports.
Sort imports alphabetically.
Consolidate similar imports.
Benefits:
Keeps your code clean and readable.
Helps identify unused dependencies.
3. Search and Replace (Use with Caution)

While powerful, this method is less safe than VS Code's refactoring tools.

Search and Replace (If absolutely needed):
If, for some reason, the refactoring tools don't catch all instances, you can use Search and Replace.
Search for utilities.resize in your entire project.
Replace it with formatting.resize.
However, you will also need to manually add the from formatting import resize statement to the top of each file that needs it.
Important:
Be very careful with Search and Replace, as it can accidentally modify unintended parts of your code.
Use it only as a last resort.
Use the "Replace in files" feature, and double check each change before applying it.
Best Practices for Refactoring:

Commit Frequently:
Before each major refactoring step, commit your changes to version control (e.g., Git).
This allows you to easily revert if something goes wrong.
Test After Each Change:
Run your unit tests after each refactoring step to ensure that you haven't introduced any bugs.
If you don't have unit tests, create them! They are invaluable during refactoring.
Refactor in Small Steps:
Don't try to refactor everything at once.
Break down the refactoring into smaller, manageable tasks.
This makes it easier to track changes and identify errors.
Use Descriptive Names:
Ensure that your files and functions have clear and descriptive names.
This makes your code easier to understand and maintain.
Use Type Hints:
If you are not already using them, adding type hints to your code will make refactoring easier, and will also help catch errors.
Use a Linter and Formatter:
Use a linter (like Pylint or Flake8) and a formatter (like Black) to keep your code consistent and clean. This is good practice in general, but especially useful during refactoring.

The bottom line is using vs code’ refactoring made the process very easy.

First, set up the folders and files where you’re going to move the code.

Then just go to the file that’s being refactored and move each function individually. Highlight the function name, right click, choose refactor, then click on move symbol. Then choose the file to move the function. And then, vs code updates the imports.

I used the AI Code to write the __init__.py file and to correct any import/circular problems.

Refactoring was so quick and easy, I couldn’t believe it.

Visit Emlekezik.com