T
14

The day I stopped writing functions longer than my grocery list

So I've been teaching myself Python for about 4 months now, mostly through free YouTube tutorials. Last week I was working on a little project to organize my music files, and my main function was like 80 lines long. I kept getting lost trying to figure out where a bug was, scrolling up and down forever. Then I watched a video where a guy broke his code into tiny functions, each one doing just one thing. I rewrote my whole file into 8 small functions, each under 10 lines, and the bug jumped right out at me in maybe 5 minutes. Has anyone else had that moment where you realized simpler code actually works better?
2 comments

Log in to join the discussion

Log In
2 Comments
wadeyoung
wadeyoung12d ago
Man, that's exactly what did it for me too... I was writing this script to rename a bunch of photo files and had one giant function doing everything from reading folders to checking date stamps to renaming. Took me like two hours to find a bug where I was overwriting the wrong variable. Once I split it into get_files(), check_dates(), and rename_photos() all under ten lines each, I spotted the mistake in under a minute. It just makes the whole thing easier to read and test piece by piece...
5
sam_harris68
sam_harris6812d agoMost Upvoted
Reminds me of the time I spent an hour looking for my glasses. They were on my head.
6