Skip to content

Commit 20feb9a

Browse files
authored
Merge pull request #187 from cbullinger/docsp-46206-code-guidelines
DOCSP-46206: Add code guidelines page
2 parents d66e994 + 4e3a730 commit 20feb9a

File tree

4 files changed

+248
-0
lines changed

4 files changed

+248
-0
lines changed
79 KB
Loading

source/reference/code-blocks.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.. meta::
22
:robots: noindex, nosnippet
33

4+
.. _code-block-reference:
5+
46
=============
57
Code Examples
68
=============

source/style-guide.txt

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ MongoDB external and internal customers.
4545
Provides guidelines for information types to identify the goals of a
4646
page and its audience.
4747

48+
:doc:`/style-guide/code-example-guidelines`
49+
Provides information about the types of code examples used in our
50+
documentation and guidance for optimizing examples for human and robot users.
51+
4852
:doc:`/style-guide/seo-guidelines`
4953
Provides guidance for optimizing your documentation pages for
5054
search.
@@ -67,6 +71,7 @@ MongoDB external and internal customers.
6771
/style-guide/nested-components
6872
/style-guide/release-notes-guidelines
6973
/style-guide/information-types/index
74+
/style-guide/code-example-guidelines
7075
/style-guide/seo-guidelines
7176
/style-guide/revision-history
7277

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
.. meta::
2+
:robots: noindex, nosnippet
3+
4+
.. _code-example-guidelines:
5+
6+
=======================
7+
Code Example Guidelines
8+
=======================
9+
10+
.. contents:: On this page
11+
:local:
12+
:backlinks: none
13+
:depth: 2
14+
:class: singlecol
15+
16+
.. default-domain:: mongodb
17+
18+
.. note::
19+
20+
This page provides general guidance for creating code examples in our docs,
21+
irrespective of language. Language-specific style guidelines and coding
22+
standards are in progress.
23+
24+
Code examples demonstrate how to use our products programmatically.
25+
High-quality, well-maintained code improves our docs' usability, builds our
26+
credibility with users, and helps reduce reported issues.
27+
28+
What is a Code Example?
29+
-----------------------
30+
31+
A code example is a block of code of any size that is set apart from regular
32+
text through a code directive. For details on valid block-level code
33+
directives, see the :ref:`code-block-reference` reference.
34+
35+
- Use block-level directives for all code examples. A code block is visually
36+
and functionally distinct from other page elements. It defines and displays
37+
its content as code, instead of regular text. It is also easier to read and
38+
better interpreted by robot users (such as screen readers, AI models, and
39+
crawlers).
40+
- Do not use inline-level markup for code examples. Use
41+
inline code markup to format code-related references within text in ``monospace``
42+
(such as method names).
43+
44+
1. Inline code text
45+
2. Code block
46+
47+
.. figure:: /images/code-inline-vs-block.png
48+
:alt: Comparison of rendered inline code and a code block
49+
:border:
50+
51+
Types of Code Examples
52+
----------------------
53+
54+
.. note::
55+
56+
Although "code example" and "snippet" are often used interchangeably, this
57+
guidance uses "snippet" to mean a context-less block of code.
58+
59+
We categorize code examples into the following types:
60+
61+
- :ref:`Usage Examples <code-category-usage-example>`: Standalone code blocks
62+
that show how to perform a task, including the relevant setup and context.
63+
- :ref:`Snippets <code-category-snippet>`: Code that illustrates a specific
64+
concept or detail in the context of a larger example, tutorial, or reference
65+
page.
66+
- :ref:`Sample Applications <code-category-sample-app>`: Runnable applications
67+
demonstrating broader use cases.
68+
69+
.. _code-category-usage-example:
70+
71+
Usage Examples
72+
~~~~~~~~~~~~~~
73+
74+
Usage examples are self-contained, actionable code blocks that show how to
75+
accomplish a specific task using MongoDB tools, drivers, or APIs. Usage
76+
examples include enough information to understand, modify, and run the code
77+
contained in the code block (for example, a single code block that contains all
78+
declared variables and includes comments to indicate which placeholders to update).
79+
80+
.. code-block:: csharp
81+
82+
using MongoDB.Driver;
83+
84+
// Replace the following with your MongoDB connection string
85+
const string connectionUri = "mongodb://<db_username>:<db_password>@<hostname>:<port>/?connectTimeoutMS=10000";
86+
87+
var client = new MongoClient(connectionUri);
88+
89+
.. _code-category-snippet:
90+
91+
Snippets
92+
~~~~~~~~
93+
94+
Snippets are narrowly scoped code blocks that help explain a specific concept
95+
or detail. They are typically used as part of a broader explanation or
96+
tutorial, and are often meaningful only within that context.
97+
98+
Snippets are intended to provide information. They aren't required to be valid
99+
or runnable code. In some cases, snippets may contain intentionally
100+
incomplete or incorrect code for demonstration purposes (for example, a snippet
101+
showing all possible arguments for a command).
102+
103+
Snippets fall into one of the following subtypes:
104+
105+
- **Non-MongoDB command**: a command-line (CLI) command for any non-MongoDB
106+
tooling (for example, ``mkdir``, ``cd``, or ``npm``), often used in the
107+
context of a tutorial.
108+
109+
.. code-block:: shell
110+
111+
dotnet run MyCompany.RAG.csproj
112+
113+
- **Syntax example**: an example of the syntax or structure for an API method,
114+
an Atlas CLI command, a ``mongosh`` command, or other MongoDB tooling.
115+
116+
.. code-block:: text
117+
118+
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net
119+
120+
- **Return example**: an example of an object, such as a JSON blob or sample
121+
document, returned after executing a corresponding piece of code. Commonly
122+
included as the output of an ``io-code-block``.
123+
124+
.. code-block:: text
125+
:copyable: false
126+
127+
A timeout occurred after 30000ms selecting a server using ...
128+
Client view of cluster state is
129+
{
130+
ClusterId : "1",
131+
State : "Disconnected",
132+
Servers :
133+
[{
134+
ServerId: "{ ClusterId : 1, EndPoint : "localhost:27017" }",
135+
EndPoint: "localhost:27017",
136+
State: "Disconnected"
137+
}]
138+
}
139+
140+
- **Configuration object example**: an example configuration object,
141+
often represented in YAML or JSON, enumerating parameters and their types.
142+
143+
.. code-block:: ini
144+
:copyable: false
145+
146+
apiVersion: atlas.mongodb.com/v1
147+
kind: AtlasDeployment
148+
metadata:
149+
name: my-atlas-cluster
150+
spec:
151+
backupRef:
152+
name: atlas-default-backupschedule
153+
namespace: mongodb-atlas-system
154+
155+
.. _code-category-sample-app:
156+
157+
Sample Applications
158+
~~~~~~~~~~~~~~~~~~~
159+
160+
Sample applications are complete, runnable programs that connect multiple
161+
discrete pieces of code. Sample apps may include error handling, framework
162+
integrations, or frontend UI elements.
163+
164+
General Guidelines
165+
------------------
166+
167+
Our code examples should always follow generally accepted coding and security
168+
best practices, and all other applicable guidelines in this Style Guide that
169+
don't conflict with language-specific standards. Remember that users copy and
170+
use these code examples outside of our docs.
171+
172+
Keep the following in mind as you write code examples:
173+
174+
- Treat code like writing: Keep it simple, readable, and relevant to the task.
175+
- Write code that is easy to understand, even if it isn't the most efficient or
176+
clever.
177+
- Introduce each code block with context, as you would a list or table.
178+
- Include any prerequisites or code dependencies needed for a piece of code.
179+
- Use descriptive names that clearly convey the purpose of the code element
180+
(e.g. variable, function, class) or placeholder.
181+
- If a code is not intended to be directly used or adapted, such as a return
182+
object example snippet, make sure the code block is not copyable. To learn
183+
how to set the copyable option, see :ref:`code-block-reference`.
184+
- Don't write code examples for anti-patterns. If you need to note an
185+
anti-pattern or a commonly made mistake, use the
186+
surrounding text, an admonition, or a code comment, and ensure that
187+
it cannot be mistaken for a recommended pattern.
188+
- If a code example is not production-worthy in a significant way, communicate
189+
this to readers through code comments *and* the surrounding text.
190+
- Never use real customer data or hard code secrets in your code. If you're
191+
unsure how to handle secrets in your code, reach out to the DevDocs team.
192+
- Use code comments to explain or call out important details, including:
193+
194+
- Non-obvious logic or intent. Don't restate the code.
195+
- Omitted or truncated code sections. If you need to omit code, use a
196+
comment to indicate what is missing and why.
197+
198+
- Test every code example to ensure it works as intended:
199+
200+
- When writing or reviewing, run the code as it displays on the page.
201+
- For tutorials or multi-step examples, begin at the starting point and
202+
complete all steps exactly, including any prerequisites or setup. Don't
203+
skip steps or assume they're correct.
204+
- If you have to make changes or take additional steps to get an example to
205+
work, make sure those details are reflected in the documentation.
206+
207+
.. note::
208+
209+
For any questions or for help writing or testing code examples, reach
210+
out to the ``@DevDocs`` team or use the ``#ask-devdocs`` Slack channel.
211+
212+
Considerations for LLMs
213+
-----------------------
214+
215+
Unlike human readers, the robots that consume our docs (such as LLMs and other
216+
AI models) can't infer meaning or intent from context. Robot users struggle
217+
with ambiguity, unclear boundaries, partial or implicit information, and
218+
non-standard patterns.
219+
220+
To ensure that our code examples are robot friendly, keep the following in mind:
221+
222+
- Prefer atomic code examples. Code should be as self-contained and
223+
self-descriptive as possible so that robot users can understand the code more
224+
easily.
225+
- Explain intent, purpose, or expected code behavior through surrounding text
226+
*and* code comments.
227+
- Don't interrupt a single code block with text explanations. Keep explanatory
228+
text before or after the code block, and use code comments for explanations
229+
within the code block.
230+
- Prefer conventional file structure, names, and canonical patterns
231+
(for example, ``main()``, ``index.js``, ``connect()``). Avoid unnecessary
232+
aliasing or non-standard structures unless explained clearly.
233+
- Use consistent, descriptive names that indicate purpose. Avoid vague names
234+
like ``foo``, ``x``, or ``doStuff()``.
235+
- Specify the code language for every code block, including output examples in
236+
an ``io-code-block``. For a complete list of supported languages, see the
237+
`leafygreen-ui GitHub repository <https://github.com/mongodb/leafygreen-ui/blob/main/packages/code/src/languages.ts>`__.
238+
If you are unsure which language to use or need a new language added to the
239+
list, reach out to the DevDocs team.
240+
- Clearly mark and explain any placeholders, omissions, or truncated code
241+
through code comments.

0 commit comments

Comments
 (0)