Lably
Skip to content

Snippet (programming)

Small amount of source code used for productivity

  • 4 min read
  • 5 chapters

Related on Lably

Through Computer programming, Productivity and Abstraction (computer science)

In contrast to an abstraction construct, such as a function, a snippet is a form of copy and paste programming – a macro operation that expands the stored template text and inserts the result into the source code. Snippet insertion is similar to a preprocessor macro except that it's inserting the expansion of the macro definition rather than referencing it. Changing the snippet after insertion does not affect the code as does changing the definition of a preprocessor macro. For this reason, best practice is to use a snippet only for relatively simple code such as a function prototype, control structure, or library import. A snippet is typically used when an abstraction is not available (i.e. not supported by the programming language) or not desirable (i.e. due to runtime overhead).

A development tool that provides snippet management typically provides for maintaining a catalogue of stored templates that involves operations such as view, add, delete, edit, and rename. Notable tools that support snippet management include: Eclipse, NetBeans, Visual Studio and Dreamweaver. Snippet features like completion and placeholder substitution can be used in shells such as bash and PowerShell.

Integrated development environments often supports snippet by implementing the Language Server Protocol which support snippets.

Types

Snippets can be classified according to the interactivity in using it, including:

Static
A static snippet is fixed text that can be inserted without entering information at insertion-time.
Dynamic
A dynamic snippet contains placeholders for which the user may be required to enter replacement text at insertion-time.
Scriptable
A scriptable snippet is runnable code. Support for such snippets may include running a shell command, 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.

Sign in to keep reading

4 more chapters are free with a Lably account.

More in Computing and communication