
Ask HN: How do you keep track of small pieces of useful code? - nickelbob
Is there some sort of Evernote for code? I&#x27;d love to be able to save small pieces of code with some accompanying documentation that describes how it works.
======
muzani
I just have a giant .txt file, one for each programming language. Anything I
google more than once goes there.

It's an optimization. Useful for rare bits of code, but not so useful for
common things like converting an int array to string array. The common things
become muscle memory and are removed from the file.

CTRL+F to search. Most of it is self-documenting, in the form of functions or
methods that I can just copy into a project.

I don't think any cloud based tools are useful for this because they're too
slow with login and search.

------
keviv
I use [https://gist.github.com](https://gist.github.com)

------
superasn
I have created two files everything.js and everything.php. Any time I find a
useful function I just add it to those files with a comment and example (if
php inside a !defined_fn block).

For local weekend projects I'm not embarrassed to say that I sometimes just
include the whole file. It saves me a lot of time and helps me work on the
thing I intend to work on instead of searching the net or wasting time on
thinking what implementation is the best.

~~~
Kagerjay
I mostly do frontend development, so I just use codepen, and tag all my
codepens so its easy to search. Then I bookmark that page. I have all those
files backed up by gists.

I might try the "everything.php" or "everything.js" file, scrolling through a
single page makes a lot of sense for me as well. But I find I just reference
the actual repository in github for backend things, so I can see the context
of the use case.

Do you dump "everything.js" and "everything.php" in its own repo in dropbox?
Would save the hassle of needing to commit it everytime, pull it from seperate
PC's, etc.

I'm still looking for a golden unicorn solution where I just look in one place
while still offering micro-testing environments (like codepen). I've tried way
too many code snippet solutions it just adds more friction to development

Common snippets I have it all binded by macros.

My goal is to have everything searchable in 3 steps max, one keyboard click,
one file navigation button press, and grokking it to find the snippet.

~~~
superasn
Yes I just used to dump everything in those file. But later on I realized that
it makes more sense to segregate it into broad categories like net.php (google
search, youtube, and website stuff related), mechanize.php (we crawler stuff)
and everything.php (everything including the kitchen sink).

It really doesn't even take me 3 steps to find it anymore since I name the
functions quite intuitively (str_match_brackets_balanced) so anytime i type
strbrackbal phpstorm automatically autocompletes the function for me. Similar
for JS though I haven't been able to create any categories and everything.js
is all that is.

------
rc-1140
I like working in c#, so I have a folder in my Dropbox that holds a collection
of linqpad files (.linq) about how to do something or other in c#. I also make
sure to keep my custom classes/extensions in there as well as a folder within
called "mockdata" which has exactly the content you'd expect; three versions
of mock data I generated from Mockaroo: one that's a SQLite DB, a csv, and a
json file. Also have a lorem.txt folder in there.

I'd love to be able to reorganize that into three subfolders: one for .linq
linq queries, one for .cs files instead of .linq files, and another for the
mock data. Would be great if I could have all of my c# snippets in .cs files
and play with them in VS Code, but I can only run the files in VS Code if I'm
working with .NET Core.

I've got a similar but far smaller collection for python snippets and
experiments (i.e., playing with an API).

------
TeMPOraL
Start a folder (or even a single file) for code snippets. If you want it
accessible on many machines, put it in Dropbox, or a Github repo, or whatever
you like. A separate "Evernote for code" is the last thing you need.

Personally, I have "snippets.org" file in my "wiki/" subfolder on Dropbox.

~~~
diegoperini
I second to this. The only difference on my use case is I treat each quality
of life snippet as its own project, foldered with a sample executable if
possible. Dropbox works perfect for non git, reference only code.

------
BetaCygni
Interesting question. I never have. I just search for it if I need it. Usually
on the web, but sometimes in the codebase.

I find the idea of a personal library of code snippets quite weird. The
thought never crossed my mind and I know nobody who uses something like that.

~~~
Sileni
I feel like it would just add overhead; I remember what project I wrote that
function for, now I also have to remember where it is in my everything
library? Why?

------
aosaigh
If you are on a Mac, you could try Quiver. It's a general purpose note taking
app aimed at developers that handles code via Markdown. You can then tag and
orgnaise everything. I use it along with Notability on iPad (for hand written
stuff)

------
ademcan
I am the developer of canSnippet, a macOS
([https://www.cansnippet.com/](https://www.cansnippet.com/)) and web-based
([https://www.cansnippet.org/](https://www.cansnippet.org/)) snippet
management tool. canSnippet allows the user to easily save and reuse pieces of
useful code/text.

In addition to the code itself, canSnippet also support tags and a description
for your snippet.

------
chatmasta
If it’s really that useful, I’ve probably used it once. So the next time I
need it, I find the code where I used it last time.

------
jamietanna
If I can find anything common that I think others would use, I'd blog it
(under
[https://www.jvt.me/posts/categories/blogumentation/](https://www.jvt.me/posts/categories/blogumentation/)),
and am looking at building common snippets into ie Ruby Gems that can be
shared and improved over time

------
TangoTrotFox
I have a utilities library that I constantly expand and reference it in all
projects.

------
ccvannorman
I have many code snippets, whih I keep either on Box Sync or Drive depending
on platform. Would be happy to share them! Would you be interested?

I'd definitely be interested in peeking at/copying other snippets too.

------
Artemix
I use both Gitlab snippets and a repository with a mkdocs website.

~~~
dsumenkovic
Glad to hear that, if you need any help, feel free to reach out to
community@gitlab.com.

------
jxub
I think a git-like system complete with dependency resolution at function
level like Ritch Hickey told in a podcast about would be a great sollution for
code reuse.

------
fosco
I have one giant emacs org-mode file.

It contains most of my brain, I wish I started this when I was younger.

------
sloaken
I find MS OneNote to be a great tool. Easy to organize, easy to toss stuff
into. Fast.

------
loa-in-backup
I have a whole directory tree ~/useful/{scripts,snippets,values}/ etc.

------
Raed667
Private Github gists. But I also have a dropbox for bad code I find in
projects.

~~~
jason_slack
Thanks for mentioning this. Since my workflow and even setting up a new
laptop, virtual machine, etc is all git based for me, using gists could be a
great addition for me.

------
I_complete_me
In my zim-wiki Computer directory, I have a page called 0_Quickies.

------
passportour
Editor snippets!

------
hguhghuff
GitHub gists

------
m1573rp34130dy
very often a "remark" or a non compiling comment is included in the source
code snippet. e.g.

rem "this code does nothing"

    
    
        let i=1
    
        for i=0
    
           beep
    
        else end
    

alternate functionalities may written as a "remark" and copypasted or edited
to make it a compiling line of code so that you can quickly edit the source
code and not have to type so much, or go looking through a codesnippit
database to find the tool you need... the actual source code script , depends
of course on what language you are using, i chose a pseudo BASIC bcz , well
its basic...

