Snippet (programming)

Last updated

In computer programming, a snippet is a relatively small amount of source code that is stored and later inserted into a larger codebase as part of the process of writing the code. Some development tools such as a code editor or an integrated development environment (IDE) provide snippet management features to enhance productivity by allowing a developer to insert a fragment of code instead of typing it. [1]

Contents

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. [2] Snippet insertion is similar to a preprocessor macro except that it's like 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.

Types

Example of a scriptable snippet in PowerShell Animated example demonstrating use of snippet in PowerShell ISE.gif
Example of a scriptable snippet in PowerShell

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.

Placeholder

A placeholder is a part of a snippet that is replaced at insertion-time. Generally, a placeholder has special markup syntax that allows the development tool to identify the boundaries of each placeholder so that it can replace it. Some tools provide a graphical user interface (GUI) in which the user can replacement values.

A placeholder is usually delimited in a snippet with special syntax and may be identified by a name or a number. The following snippet text uses identified placeholders: first_name, last_name, and item:

Hello {%first_name%}{%last_name%},Your shipment of {%item%} is now ready to pick up.Thanks {%first_name%}!

With an identified placeholder, a snippet can use the same replacement value multiple times. In the previous example, the placeholder first_name is an example of this use.

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

Example

The algorithm of swapping the value of two variables, x and y could be coded as:

temp = x x = y y = temp

To make the snippet more useful, the variable names are replaced with placeholders as:

temp = $1 $1 = $2 $2 = temp

When the snippet is inserted, the development tool might prompt the developer for more specific names to replace the placeholder names. For foo and bar, the inserted code is:

temp = foo foo = bar bar = temp

If the snippet is subsequently changed, say to use __temp instead of temp, the existing code would not automatically use the new snippet definition, but subsequent insertions would.

Alternatives

A common alternative to snippet use is the function which has the advantage that modifying its definition affects consumers whereas modifying a snippet does not modify existing insertions of the snippet. Many development techniques such as object-oriented programming and functional programming stress the avoidance of duplicate code which is counter to using snippets.

Another alternative is the preprocessor macro which is available in C and many related languages.

Design pattern
A design pattern is similar to a snippet in that code is generated rather than coded as functions, but a pattern differs in scope. A pattern is relatively large while also is challenging to code as a reusable library.
Object code fragment
Similar to a source code snippet, a just-in-time (JIT) compiler can splice together pre-compiled code as longer object code segments. This reduces interpret time and speeds execution.
AI code insertion
Generative artificial intelligence (AI) such as GitHub Copilot can infer functionality from source code and insert relevant code that one might consider similar to a snippet. [3] [4]

See also

References

  1. "Gedit/Plugins/Snippets". Archived from the original on 2012-08-25. Retrieved 2010-01-09. Example of the feature in the Gedit editor.
  2. Kim, Miryung; Bergman, Lawrence; Lau, Tessa; Notkin, David (2004). "An ethnographic study of copy and paste programming practices in OOPL" (PDF). Proceedings: 2004 International Symposium on Empirical Software Engineering, ISESE 2004: 19–20 August 2004, Redondo Beach, California. Los Alamitos, CA: IEEE. pp. 83–92. ISBN   978-0-7695-2165-7. OCLC   326620442. Archived from the original (PDF) on 19 October 2016. Retrieved 18 October 2016. For each C&P instance, we also noted the relationship between a copied code snippet and code elsewhere in the code base.
  3. H. M. LE, TRIET; CHEN, HAO; ALI BABAR, MUHAMMAD (2021). "Deep Learning for Source Code Modeling and Generation". ACM Computing Surveys. 53 (3). The University of Adelaide: 1–38. arXiv: 2002.05442 . doi:10.1145/3383458. S2CID   211096967.
  4. "GitHub previews new AI tool that makes coding suggestions". TechCrunch. 29 June 2021. Retrieved 2021-07-25.