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 customarily 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 ands 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] Example

In this example, the if code snippet inserts an if statement with a curly-brace code block.

 if (true)
 {

 }


[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.

[edit] See also