Be Correct

Example code is an opportunity to directly influence how your users write code. Therefore, examples should showcase the best way to use your product

Example code should meet the following criteria:

  1. Free of any errors.
  2. Perform the task it claims to perform.
  3. Be as production-ready as possible.
  4. Follow language-specific conventions.

Be General

Examples should focus on explaining or demonstrating concepts that can be applied to a wide variety of scenarios. If you find writing a super specific example, ask yourself if writing a tutorial would be a better way to address it.

Examples are not tutorials. People already know what they are seeking from a tutorial, examples instead introduce people to what's possible.

Be Simple

As a beginner, seeing a page full of code can be very intimidating. To avoid this, examples should be kept as simple as possible.

Try and keep the pre-requisite knowledge to a minimum. Since an example is ideally being used to introduce a new concept or framework, the reader shouldn't have the added burden of figuring out other stuff which could be avoided.

In some cases, an example will require more complexity. Break it down further into three separate examples that address different experience levels Beginner, Intermediate & Advanced.

Be Well Commented

In the case of examples, comments almost form a dialogue with the code. This gives the reader a sense that someone is explaining the code to them.

Comments are also extremely helpful in introducing additional context. Clarity and understanding of the purpose of a particular line of code are important to avoid unwanted confusion.

Moreover, since the reader is exposed to well-documented code this will encourage them to add comments to their code as well.

Be Inviting

This one is pretty simple, the example should invite the user to play around with the code.