Ask HN: How often do you really reuse your code?
As most of developers, I try to create as much reusable code as possible. But in reality, I rarely use my code again. I could easily say that less than 5% of my code will be reused in the future.
I wonder how it looks for other people, is it worth to make your code reusable in the first place?
14 comments
[ 3.4 ms ] story [ 34.9 ms ] threadThough in Rails, very rarely, it's usually faster just rewriting it than trying to 'frankenstein' it from a set of piecemeal snippets.
Custom CMS - 100% reuse - always start with a rolling base and constantly improving it though WP sites have cut into this - people think WP is better - get what you pay for.
Random 1-offs - how many times I've had to build a newsletter system... Base system gets copied and installed with specifics changed - where to pull the email list from, mailgun or aws for sending, etc...
Server config could be considered "code reuse" - I'll almost always refer to another server I built or run myself to copy apache / php / whatever config sections as a starting point.
Then, go and create that library. You'll get more reuse (and better test coverage, maintainability, open-source ability) than just making spaghetti code every time.
But that's probably not the way you were looking at it when you asked the question...
It also prevents code from turning into spaghetti when adding "special" cases
This turned out to be a life saver when I had to analyze / modify a bunch of data- I was able to get a node script up and running over one weekend re-using the existing javascript models and just focus on the analysis, instead of having to recreate everything in Java or Python.
I do a lot of HTML/CSS specialist stuff, writing a lot of front-end interactivity from scratch. I maintain a list of snippets that have helped me and others.
https://gist.github.com/tomhodgins/27c29ecb4aceaefe5cdf
Any code I have re-used three or four times ends up getting cleaner each time I work with it, and if it has proved useful a handful of times I add it to list of Snippets :)
Lately I have also been building teeny tiny HTML pages that illsutrate one concept clearly.
Here's my demo of how to harness multitouch cursors with plain JavaScript: http://staticresource.com/touches.html or heres another demo that illustrates JavaScript's Math.random() http://staticresource.com/marbles.html
Little pages like that serve as reminders, teaching tools, and provide a quick reference for me when I want to re-use similar concepts in another project, without looking up what other people are doing or grabbing the trendy Framework du jour.
Also I often approach project planning in such a way that I can reuse solutions for problems I solved previously.
If you aren't generalizing the problems you're solving in such a way that you can just use policy-types to customize behavior, you're probably not doing yourself any favors in the code re-use sense.