Code Snippets

A code snippet is a minimally functioning program that is used to demonstrate the use of a single feature of a programming language. A section of code is considered a snippet if

  1. It is a completely functioning program, and
  2. The demonstrates exactly one concept.

As an example, the following code would be a snippet for demonstrating functions in C++,

#include <iostream>
 
void MyFunction()
{
    std::cout << "Inside My Function" << std::endl;
}
 
int main()
{
    MyFunction();
    return 0;
}

but the following would not, because it would not compile.

void MyFunction(std::string filename)
{
    std::cout << "Inside My Function" << std::endl;
}
 
int main()
{
    ...
 
    MyFunction("/home/stargazer/myfile.txt");
 
    ...
}

While snippets should only demonstrate a single concept, this can be difficult, because there are many times where multiple techniques are needed simply to show a more advanced concept. The key is that code snippets should be as small as possible while still demonstrating functionality.

Code Snippets should always follow good programming techniques.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License