6

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);
Glorfindel
  • 3,137
  • 6
  • 25
  • 33
sixtytrees
  • 469
  • 5
  • 10
  • 4
    Possible duplicate of [Best practices for sharing tiny snippets of code across projects](http://programmers.stackexchange.com/questions/193415/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
  • "Best practices for sharing tiny snippets ..." discussion went into "should I bundle my networking snippets in one library and use it whenever I need some functionality?" With the answer don't do it; this sort of library is not a Swiss knife, but a mud ball". This discussion isn't really applicable to my question. I am looking for a way to store syntax constructs (correct way to pack a frame is `frame.pack()`), not large libraries. – sixtytrees Jul 25 '16 at 15:21
  • 1
    What features do you have in your IDE that could solve this problem? – Robert Harvey Jul 25 '16 at 15:26
  • I am using NetBeans. the closest hit is autocompletion. Unfortunately, it only cannot do natural language search. Using my example, one cannot type "function to align column right". I recognize common functions (such as `for`) but I have to google less common function and recall "yes, that is what I was using before". – sixtytrees Jul 25 '16 at 15:27
  • I take it [Code Snippets](https://platform.netbeans.org/tutorials/nbm-palette-api1.html) doesn't float your boat? – Robert Harvey Jul 25 '16 at 15:43

2 Answers2

8

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.

Tulains Córdova
  • 39,201
  • 12
  • 97
  • 154
  • 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 07: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.