
Ask HN: How often do you really reuse ​your code? - humble_dev
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.<p>I wonder how it looks for other people, is it worth to make your code reusable in the first place?
======
lsiunsuex
Random Wordpress website - 50-75% reuse - most generic sites are the same -
upload a theme I've used in the past, change colors, some layout, images,
etc... saves a lot of time to have a vetted baseline

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.

------
err4nt
How often? Daily!

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](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](http://staticresource.com/touches.html)
or heres another demo that illustrates JavaScript's Math.random()
[http://staticresource.com/marbles.html](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.

------
auganov
Straight up reuse - very rarely. But fairly often I'll just go "ohhh i've done
smth like that in that other project" and just go look at the code, paste it
in, play around a bit. More about getting into the zone and having the past
experiences pop back into the mind rather than reusing code.

------
seiji
Try to structure your problems as: If a pre-existing library existed to solve
my problem, what would it look like?

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.

------
twunde
5% reuse is low, although it probably depends on what exactly you're working
on. One approach is to make as much of your code into libraries as possible.
This works especially well if you're making new applications using the same
framework, although it's still valuable if you're maintaining an application.
I've also found that separating the ui into partial views can save a lot of
time, especially if you can reuse some of them for other projects. A third
option is to make some functions that you use by convention. For example if
you're using Selenium for testing there are 15+ ways to find something on a
webpage. Choose 5 and only use those.

------
iMerNibor
The huge advantage of making reusable code, even if you never reuse it, is
that it's clean and decoupled from the other parts of your application. Thus
it'll be easier to understand and to deal with - you won't have to look over
what dependencies might cause unwanted effects here or there or what might be
able to go wrong in case X. With decoupled code you only need to make sure it
does it's "one" (main) function properly.

It also prevents code from turning into spaghetti when adding "special" cases

~~~
mattkrea
Do you write test cases? If not I'd recommend it. The decoupling required for
effective tests leads you into modularizing things you may not have otherwise.
I have seen myself that after this started code re-use in general has jumped
due to the modules already sitting around where previously I may have just
written somewhat similar functionality from scratch all over again in a new
project.

------
jtfairbank
I re-use my code less across projects in the web, but well designed MVC or
MVVM can save you a lot of time. Just by having models available, I can re-use
them on every page.

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.

------
csmattryder
Depends if I move across problem domains, but I spend a bit of time writing
games with LibGDX, and do have a Java package with some basic stuff such as
screen generation and asset loading. It does end up saving me a good hour on
getting a new game prototyped.

Though in Rails, very rarely, it's usually faster just rewriting it than
trying to 'frankenstein' it from a set of piecemeal snippets.

------
insoluble
In terms of desktop application development, I easily reuse 30% or more of my
code, mostly in a linked fashion. I sometimes have to port between languages,
but I still consider that reusing. When it comes to Web Dev, I reuse much
less, perhaps only 5 or 10%. I also have less experience with Web Dev than
Desktop development, which may be a part of the reason.

------
AnimalMuppet
Depends on how you look at it. Arguably, I reuse my code every time I modify
an application to make it do something it couldn't do before. That effort to
make the code reusable helps (if done right) when I have to modify the program
some time in the future.

But that's probably not the way you were looking at it when you asked the
question...

------
shortoncash
If you count utilizing metaprogramming features within some of the more
mainstream languages, my code re-use is sky-high.

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.

------
veddox
I'm not a professional developer, but for what it's worth, I have a Common
Lisp utility module that I use in virtually every Lisp project of mine.

Also I often approach project planning in such a way that I can reuse
solutions for problems I solved previously.

------
mr_nobody
It remindes me of: [http://blog.codinghorror.com/the-delusion-of-
reuse/](http://blog.codinghorror.com/the-delusion-of-reuse/)

