4

I've noticed that a lot of my work follows this pattern:

  1. Decide that I need to add a behavior (say, align table columns right)
  2. Recall that I did it few months ago on a different project, try to find it
  3. Google the question
  4. Check several tutorials, find relevant code

This is time consuming and I want to find a better way to store snippets. I tried to store it in a plain text file, but it grows large very fast and it is hard to come up with a good indexing system. Storing each snippet in a separate stub project in IDE leads to a long list of stubs that is hard to navigate through. Leaving comments in actual projects like "here I set the alignment" seems like a bad practice to me.

What are the efficient ways to store useful code templates?


Yes, I saw Best practices for sharing tiny snippets of code across projects discussion. It deals with much larger pieces of code. Compare Framework.Data (some 100 lines of code) vs "align text right" (1 line of code).

This makes difference, because it makes sense to have a library with two functions writeToDatabase(String query){...} and readFromDatabase(String query){...} that do all the necessary connection/exception handling. It doesn't make sense to have a library that has a function

row.alignRight(n); 

That does a generic formatting by executing some bulky code like:

DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(JLabel.RIGHT);
table.getColumnModel().getColumn(n).setCellRenderer(rightRenderer);
  • 4
    Possible duplicate of Best practices for sharing tiny snippets of code across projects – gnat Jul 25 '16 at 15:02
  • Yes, I saw that post. It doesn't resolve my question. That post focuses on "should I bundle snippets in a library" with a consensus "don't bundle stuff". My question is "how do I store micro recipes in a searchable format". – sixtytrees Jul 25 '16 at 15:05
  • Why is 100 lines that solve a specific problem any different from one line that soves a specific problem? Seems to me that the focus should be on the "solves a specific problem" aspect of it. On the other hand, the lawyers might see a difference between sharing 100 lines between projects that you did for two different customers and sharing one line... – Solomon Slow Jul 25 '16 at 15:12
  • What features do you have in your IDE that could solve this problem? – Robert Harvey Jul 25 '16 at 15:19
  • 1
    What features do you have in your IDE that could solve this problem? – Robert Harvey Jul 25 '16 at 15:26
7

I'm a Linux user and what I do is as follows.

I have a folder named cheatsheets, and under that I have folders for topics like bash, java, css, html, sql, etc.

I save each code snippet or cheat sheet in it's own text file with a pertinent name.

Then I made a script based on the locate comand that helps me find the cheatsheet I need.

Besides that I just do grep -iIR into my project folders to find code that I've used before and that I need to use again.

  • This seems portable and convenient. I will try that. – sixtytrees Jul 25 '16 at 17:28
  • 1
    I use something similar, except that I keep it in Workflowy (an online outline editor) which allows me to replace the files-and-folders "classification" with a (homogenous) outline structure (nested bullets, basically). I'm not saying it's "better", but it is at least accessible from any (online) computer. – KlaymenDK Jul 26 '16 at 7:24
  • 1
    @KlaymenDK Due to the nature of my work I need my cheat sheets available even if the internet is inaccessible or unreliable. Of course I have a weekly backup in case my hard drive dies. – Tulains Córdova Jun 19 '17 at 19:58
0

I use an app called CodeBox. It has some issues with syntax highlighting but it's fine for me.

I use it multiple times a day for retrieving command line shortcuts, SQL queries, CSS, JS etc. I like that it's cheap, has great search and most of all that I can keep the .cbxml file in my Dropbox so it's available at work, home and offline.

protected by gnat Sep 29 '17 at 17:35

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.