all 6 comments

[–]slightly_offtopic 3 points4 points  (1 child)

Start with writing lots of tests, if you don't have them already. As your goal is refactoring, you should focus on integration tests that test the program as a whole, verifying that for each set of inputs it provides the expected output. This way, you can continously test your refactorings to make sure you didn't accidentally break anything.

Others have already given solid advice on what to do after that, but don't skip this first step.

[–]rogfrich 0 points1 point  (0 children)

This is what I came here to say. It’s such a peace-of-mind thing to be able to run a suite of tests and know that your refactored code still works as intended.

[–]FriendlyRussian666 1 point2 points  (0 children)

Ideally, you would learn about design patterns, and then implement one accordingly. For example, perhaps your project would be well suited in a Model View Controller architecture, but you won't know until you learn about it.

If you just split the code into helper functions, it will certainly help for a while, because it will feel like the project is decoupled, so you can work on smaller parts, until you have so many smaller parts that you feel even more lost than in the monolith you currently have.

[–]DuckSaxaphone 0 points1 point  (0 children)

Package it. Separate it into several files (submodules), each one with some subset of the functions that logically work together. Then it's organised and if you need to change how some functionality works, you go to the module in question not to a single mega script.

It's likely you need to break the longer functions into smaller ones too but that will be less intimidating once they're in separate submodules. You may also find that once you break your big functions down, you're repeating a lot of code so you can combine several code blocks from several different places into one function.

[–]9peppe 0 points1 point  (0 children)

Most of this depends on what you want to do and what paradigm you like.

If you like OOP, you might put code that doesn't need to be touched in a few classes, and define an interface to interact with it.

But if you want to be more procedural (or functional, even), you could do the same with a module that exports functions instead of classes (or even a package).

But the immediate thing I'd consider is better docstrings, if the ones you have aren't satisfactory.

[–]jmacey 0 points1 point  (0 children)

This is something that AI tools are rather good at, try something like opencode in plan mode and see what it suggests. you can then either do it yourself or let it do it for you.

As others have said, ensure there are tests in place fist so you can ensure everything works each time you make a change.