Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

I am currently learning C#, I am pretty proficient in Java. A project I have been working on for a while is basically an application that lets you input code "Snippets" or blocks of frequently used code and lets you tag them with specific topics for easy sorting.

How should I go about saving the inputted code so I can access it later and possibly format it or keep it in the formatting it's pasted in? I would want to put more information into the file it is stored in addition to the actual code, like the tags and dates/times so things like XML have came to mind but I really am not sure.

Just for clarification, I am aware of the snippet function in visual basic. What I am asking about is for making my own C# standalone application and how to store the code on my own.

share|improve this question

marked as duplicate by MichaelT, gnat, GlenH7, Bart van Ingen Schenau, Dan Pichelman Apr 22 at 16:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Specifically for VS 12 see: programmers.stackexchange.com/questions/204871/… –  MichaelT Apr 21 at 12:54

2 Answers 2

up vote 0 down vote accepted

The key here is to start from "the simplest thing that could possibly work" but bear in mind that if it gets to be useful you will need to add a lot of features.

You mention formatting, but I would avoid keeping original formatting. You should expect to reformat the text when it's pasted, and you should also expect that editor settings may be different between the time of saving and the time of use. Even keeping tabs or non-essential whitespace is a potentially hazardous thing to do. I would tokenise the text, remove non-essential whitespace, and reformat for display and insertion.

Yes, I think XML is probably a good choice. It has the enormous advantage for developer users that they can manually edit snippets (as XML) when needed.

If you use raw XML (in files) you will have to read the whole repository of snippets into memory before using them, in order to allow good enough searching. This will work fine for getting things working, but you will probably need a more sophisticated non-SQL database and indexing by attributes to handle large numbers of snippets. Then you need tools to import/export/rebuild indexes.

You also need to allow for substitutions, and they will always force reformatting. Snippets without substitutions are a waste of time. If this is a VS environment, give serious thoughts to using or hooking into T4. That is one incredibly powerful tool.

Searchable, editable snippets with substitutions, loops, conditionals, name generation, auto-commenting, auto-formatting and my favorite: sort into alphabetical order. Yes, I would use that.

share|improve this answer

This question is heavily dependent on IDE you are using. In Visual Studio, there are snippets, allows you to create your own and share with other developers. But, there is no way to categorize them. There is also possibility of using a Toolbox window. Simply select the code and drag it into the Toolbox to create new item, that can then be renamed or grouped under tabs. Drag it from Toolbox to add it into your code.

There is also question of why you want those snippets. Maybe your architecture or design is wrong and forces you to repeat some piece of code, instead of providing adequate abstraction for it. In which case, refactoring should be what you are supposed to do. Or you could want code generation like T4 to generate the whole file, instead of just snippets.

share|improve this answer
    
Good point about "copy and pasting" code. I hope this is being used more as a reference tool. This is handy when you may be fluent in one language and need similar code in another. The need to maintain formatting could be to make copy & paste easier or just readability. –  JeffO Apr 21 at 12:37
    
I know there is the tool in visual studio, I use it often. What I want to do is build my own program to store them for my use outside of an IDE. Thanks for the responses! –  Mattredsox Apr 22 at 0:56
    
So back to my questions, I am aware of the built in snippets in VS but I am asking about how to go about storing the code in my OWN application. Thanks! –  Mattredsox Apr 22 at 1:16

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