Snippet (programming)
From Wikipedia, the free encyclopedia
Snippet is a programming term for a small region of re-usable source code or text. Ordinarily, these are formally-defined operative units to incorporate into larger programming modules. Snippets are often used to clarify the meaning of an otherwise "cluttered" function, or to minimize the use of repeated code that is common to other functions.
Snippet management is a feature of some text editors, program source code editors, IDEs, and related software. It allows the user to persist and use snippets in the course of routine edit operations.
Contents |
[edit] Overview
Snippet management is a text editor feature popular among software developers or others who routinely require content from a catalogue of repeatedly entered text (such as with source code or boilerplate). Often this feature is justified because the content varies only slightly (or not at all) each time it is entered.
[edit] Snippets in text editors
Text editors that include this feature ordinarily provide a mechanism to manage the catalogue, and separate "snippets" in the same manner that the text editor and operating system allow management of separate files. These basic management abilities include operations such as viewing, adding, editing, deleting, sorting, filtering, grouping, renaming and storing snippets in a repository, catalogue or database.
[edit] Snippets in IDEs
Some programmer's applications such as Microsoft's Visual Studio and other IDEs include built in parts of structure for ease of coding.
Other applications such as Macromedia Dreamweaver make use of these code snippets as well for Web Development.
[edit] Conventions
In addition to the basic management abilities described previously, snippet management features can be classified according to the scope of interactivity between snippets and the text editor or application that hosts them.
These snippet feature groups include:
- plain-text or "static" snippets
- interactive or "dynamic" snippets
- scriptable snippets
Static snippets consist primarily of fixed text that the user can choose to insert into the current document. The user is not able to specify anything else, except perhaps the cursor position relative to the newly inserted text. Static snippets are similar to simple macros.
Dynamic snippets consist of fixed text combined with dynamic elements. The user may specify both the content of the dynamic elements, as well as their position relative to the fixed text, as part of choosing what to insert into the current document. Examples of dynamic elements could be variables such as the current date or system time, or input from the user that is supplied via a GUI, or input from another application. (see also: programmable macro).
Scriptable snippets consist of runnable segments of code in either a macro language or a scripting language. Scriptable snippets provide the greatest degree of flexibility to the user, although that depends somewhat on the programming languages supported by the text editor, and whether or not the programming language is well-known, or particular and unique to that specific editor.
The type of scripting support varies, but may include features such as running shell commands, providing a GUI dialog or other methods of user interaction with the operating system; other applications; or other sub-components of the hosting application itself.
[edit] Snippet programming features
For applications that support scriptable snippets, the range of supported programming features varies. The following enumerates some of the features that are commonly implemented for programmable snippets.
[edit] Plain text
Although plain text is a fundamental feature included even with applications that support only non-programmable "static" snippets, programmable snippets are also used for working with plain text.
One common complication, however, is that environments that support programmable snippets often have to make distinctions between what counts as "plain text" and what counts as "programming instructions". Further complicating this distinction is the fact that applications that support programmable snippets almost always include support for recognition of multiple programming languages, either through basic syntax coloring or execution of embedded commands.
For these and other reasons, emitting plain text from programmable snippets almost always entails being careful to avoid problems with syntax and delimiter collisions.
[edit] Constants and variables
Programmable snippets often include an ability to establish a binding to an existing variable scope or namespace, from which the user can select any of various constants or variables. These might include values such as the email address of the currently-logged-in user on a given machine, the current system time and date, or the output value of a function.
Scriptable snippets are often associated with one or more currently-active files. Consequently, variables may also include environment variables and arguments that specify the filename, cursor position, parent directory and other stats relating to the files in a current editing session.
[edit] Interpreted code
Scriptable snippets may allow execution of code in one or more programming languages. This may include one or more standalone languages, or a language that is specific to the application in which the language is hosted.
[edit] Fill-in-the-blank placeholders
Scriptable snippets routinely include a system for specifying user-supplied values that are not determined until the text of the snippet entry is inserted or activated in the course of an ongoing editing session. Some applications define special markup syntax that allows the editor to identify the boundaries of placeholders relative to the other text in the current edit buffer.
Other applications employ GUI interfaces and modal dialog boxes that allow the user to enter one or more values to be supplied for the placeholders.
[edit] Placeholder duplication
This allows the user to indicate that the value supplied for one placeholder should be replicated in multiple places, relative to the entire text of the programmable snippet.
[edit] Placeholder transformation
This allows the user to indicate that one or more values supplied for a placeholder should be replicated and transformed in other places within the text of the programmable snippet. For example, the user may supply a document title in one part of the snippet, and specify that the document title should be repeated in other places, with the first instance being all-uppercase and every other instance being lower-case.
[edit] Application support
The following is a summary of snippet support in some applications such as text editors and IDEs. For more details, see also Comparison of text editors.
plain text | dynamic | scriptable | |
Textmate | yes | yes | yes |
Textpad | yes | no | no |
[edit] See also
[edit] External links
- Snipplr Social snippet repository that supports over 60 languages.
- Friendsnippets A social snippet bookmarking site.
- Wikicodia.com the code snippets encyclopedia.
- http://snippets.dzone.com/ A public source code repository.
- http://www.daniweb.com/code/java.html
- http://c.snippets.org/ The original C code snippets archive, maintained in memoriam: Auke Reitsma 1947-2003