H
15

Code review that totally changed how I write functions

My mentor told me my functions were doing too much. He pointed to a 40 line function I wrote and said each function should do one thing only. So I broke it into 5 smaller functions, each maybe 8 lines. Now my code is way easier to debug and test. Has anyone else had a piece of feedback that completely shifted your approach?
2 comments

Log in to join the discussion

Log In
2 Comments
foster.wade
Did you find it hard to figure out where to split the original function, or did the boundaries feel obvious once you stopped trying to do everything at once? I struggle with that sometimes when a single task still needs a few steps to get done.
6
dakota_rivera
Did you find it hard to figure out where to split" - for me, nah, not really. The split points were pretty clear once I stopped trying to make it clever. I actually think people overthink this. If you look at a function and it's doing three different things in sequence, that's three functions right there. Each step gets its own little box. But honestly, I don't think you need to split everything into tiny pieces either. Sometimes a function that does one logical thing still needs 15-20 lines. That's fine. The rule about "one thing" is about responsibility, not line count. Breaking a 40 line function into 5 pieces might just spread the mess around. Better approach is just ask: can I test this thing easily? If no, it's probably doing too much. If yes, who cares if it's 30 lines. I've seen people split stuff so aggressively it becomes impossible to follow the flow. That's worse.
5