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,167

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,570
  • 13
  • 100
  • 156
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.