Snippets R Us!

The web page dedicated to not using (computer) code snippets in forum questions.

For other uses of the word "snippets" please see here.


Written by Nick Gammon. Updated on 5th November 2013.

Link to this page: http://snippets-r-us.com


Q. So, what is a snippet?

A. A snippet is a small piece of code imbedded in a forum question, so short and devoid of details that it makes it impossible to answer the posed question. Thus it is usually necessary to ask the poster to "post all your code".

Disclaimer: The examples are generally taken from the Arduino forum, and are written in C++ as implemented on the Arduino, so various libraries like "Serial" (for outputting text) are assumed to exist.


Example 1

Q. My code crashes, can you help?

Snippet:


foo [bar] = 42;

A. There are a few things missing there:

Full code:


int foo [100];

void setup ()
  {
  int bar = 100;
  foo [bar] = 42;
  }  // end of setup

void loop ()
  {
    
  }  // end of loop

The problem is now revealed:

Example 2

Q. This code does not run. Why is the Arduino so hopeless? I followed all the examples:

Snippet:


void setup ()
  {
  Serial.println ("Program starting.");
  }

A. Insufficient information to say.

Full code:


int foo [3000];

void setup ()
  {
  Serial.begin (115200);
  Serial.println ("Program starting.");
  }

void loop ()
  {
  foo [0] = 42; 
  }  // end of loop

The problem is now revealed:

Example 3

Q. Why can't I do serial printing?

Snippet:


  Serial.println ("Starting robot.");

A. Insufficient information to say.

Full code:



void setup ()
  {
  Serial.println ("Starting robot.");
  }

void loop ()
  {
    
  }  // end of loop

The problem is now revealed:

Moral of this story

Include all your code. If that is too much (or too confidential) make a "demo" program that demonstrates the problem. If your "demo" program does not reveal the problem, you have a clue as to where it really is.

When posting use code tags! Example:

To do it "manually", put your code inside tags like this:



[code] (your code here) [/code]



If your code is too long to fit into a post, or you have multiple files (eg. code and libraries), or you want to show an image, click on the Additional Options button:

Then you can browse your hard disk for the code or zip file you want to attach:


Other sites of interest

The Arduino Forum

Nick Gammon's Arduino/electronics pages

Arduino home page

How to use the Arduino Forum

How to post a programming question

Can you trust your computer?


Thanks!

Thanks to PaulS of the Arduino forum for the idea for this page. He usually tells people who post snippets to take them to http://snippets-r-us.com, so I thought it would be nice to have that link actually be educational.

Thanks also to all the friendly and helpful people on the Arduino forum. Questions about programming, the Arduino, electronics, etc. are usually answered promptly and helpfully.


Permission to reproduce

This page is not officially affiliated with Arduino.

Permission is granted to reproduce or link to this page. If reproducing please give credit to the original author (Nick Gammon) and a link to the original page (http://snippets-r-us.com). Information on this page may be updated from time to time.


Valid CSS!