F
23

I thought writing my own functions was a waste of time

For months, I just copied and pasted the same few lines of code over and over in my Python scripts. My friend pointed out a 50-line script I wrote that could be just 10 lines with one function. Seeing that side-by-side finally clicked for me. Anyone have a good example of a simple function that saved them a ton of typing?
4 comments

Log in to join the discussion

Log In
4 Comments
andrew_shah
Yeah I was the same way, always telling myself it wasn't worth the extra step. What got me was formatting dates for my kids' school calendars. I had this ugly block of code repeating strftime stuff a dozen times. Wrapping it in one function called clean_date() cut out so much clutter and fixed my mistakes in one spot.
3
robin896
robin89622d ago
Totally get that "ugly block of code" feeling. I kept writing the same lines to pull a user's first initial and last name for email greetings. Did it maybe a hundred times before I made a small function. Now it handles all the edge cases in one place, like when someone only has one name. Same deal as @tessalane with the roofing math, just a little task that adds up to a huge mess if you don't bundle it. What's the next bit of repeated code you're planning to wrap up?
1
tessalane
tessalane1mo ago
My roofing estimates used to be a mess of repeated math. Wrote a tiny function that takes square footage and pitch, spits out material costs. Went from checking the same calculator fifty times to one clean line of code. Feels like cheating now.
2
nora_murphy
Ever try that with your grocery list math?
-1