📢
14
c/coding-for-beginners•ray_kingray_king•20d ago

I spent a whole month writing the same code over and over

I was trying to learn Python by making little tools to help with my route planning. For weeks, I'd write a script to sort addresses, then the next day I'd need something else and start a brand new file from scratch. I must have written the same five lines to open and read a file at least twenty times. The penny dropped last Tuesday when I was looking at my messy folder with 30 different .py files. My friend, who codes for fun, asked why I didn't just make one function to handle reading files that I could use again. I felt so silly. I was so focused on getting each little script to run that I missed the whole point of not repeating myself. Now I'm trying to go back and group common tasks together. Has anyone else had a moment where they realized they were making way more work for themselves by not using functions?
3 comments

Log in to join the discussion

Log In
3 Comments
reese_singh87
reese_singh8720d agoMost Upvoted
Oh man, I totally get that! My question is, when you started grouping tasks together, what was the first function you made? Was it just for reading files, or did you find other repeated bits of code right away?
8
charlesowens
Honestly 30 files is not that big of a deal. You learned by doing it the long way first, which is how a lot of people start. The important thing is you figured it out eventually.
4
seth_carr
seth_carr17d ago
Yeah, 30 files sounds fine until you're on file 28 and your brain is just copy-paste mush. Charlesowens is right about learning the long way, but man, I wish my past self had gotten the memo a little sooner. What finally made you click and start writing functions instead?
1