3 ms·
There's a lot of good sources. I recommend reading books like Clean Code and Domain Driven Design to know what good code looks like. Then read Martin Fowler's r
by muzani 4y ago
There's a lot of good sources. I recommend reading books like Clean Code and Domain Driven Design to know what good code looks like. Then read Martin Fowler's refactoring books (or even the blog) to morph it into that. There's a lot in these books that I don't agree with. Don't be too dogmatic. Just improve your toolset.
The immediate approach is to figure out what the function is trying to do. Write a test, perhaps manual if necessary. Manual meaning you run the function, what happens? What appears in the log? What shouldn't happen?
Is there anything in there you can write a test for? Pick the low hanging fruit. I like to tackle validation first, because they're pretty important, easy to test, and get messy fast.
Extract it into another function. Sometimes you want to extract it into a class. Write a test on this function (again, manual is okay). What's the input? What's the expected output?
Then you have lots of littler functions with their own expected inputs and outputs.