H
2

I finally understood functions after a bad day at the library

I was at the downtown public library last month, trying to write a small program for a class project. I had copied the same block of code about five times to do the same thing in different places. It was a mess, and I couldn't get it to run. A guy at the next table, who was clearly working on his own code, leaned over and said, 'You know, if you're doing that more than twice, make it a function.' He spent maybe ten minutes showing me how to wrap my repeated code into one neat function I could call. It clicked right then. My file went from a long, confusing script to something I could actually read and change. I still write everything too long sometimes, but now I stop and ask myself if I'm repeating stuff. Has anyone else had a simple tip that suddenly made a big piece of coding make sense?
3 comments

Log in to join the discussion

Log In
3 Comments
rose_hart31
That "if you're doing it more than twice, make it a function" line is the real deal. It's one of those rules that sounds obvious once you hear it, but it changes how you see your whole code. I had the same kind of moment with learning about lists instead of making a new variable for every single item. It just makes everything cleaner and way less of a headache to fix later.
4
troy996
troy9967d ago
Wait, isn't that rule a bit too strict though? I get the idea but sometimes making a function for something tiny just adds more layers to jump through. If the code is only in two places and super simple, I'd rather just see it there than have to go find the function definition. It can make things harder to follow for no real gain.
3
sandra715
sandra7157d ago
A guy at the library just helped you for free?
0