@@ -29,51 +29,35 @@ A code example is any size block of code set apart from regular text using a
29
29
code-block directive. For details on valid directives, see the
30
30
:ref:`code-block-reference` reference.
31
31
32
- - Use code-block directives for all code examples. Code blocks are visually
33
- distinct, easier to read, and better interpreted by robot users (such as
32
+ - Use code-block directives for all code examples. A code block is a visually
33
+ and functionally distinct page element that defines and displays its content
34
+ as code. It is easier to read and better interpreted by robot users (such as
34
35
screen readers, AI models, and crawlers).
35
- - Do not use inline code markup to format code examples in ``monospace``. Use
36
- inline code for short references within text (such as method names).
36
+ - Do not use inline code markup to write code examples. Use
37
+ inline code to format code-related references within text in ``monospace``
38
+ (such as method names).
37
39
38
40
1. Inline Code
39
41
#. Code Block
40
42
41
43
.. figure:: /images/code-inline-vs-block.png
42
44
:alt: Comparison of rendered inline code and a code block
43
45
44
- Inline Code
45
- ```````````
46
-
47
- Inline code (sometimes referred to as "code in text") refers to code-related
48
- words or phrases, such as a method name in a sentence, rendered in
49
- ``monospace``. To learn more about when to use ``monospace`` text, see
50
- `Text Formatting <https://www.mongodb.com/docs/meta/style-guide/style/text-formatting/#text-formatting>`__.
51
-
52
- Inline code is not considered a type of code example.
53
- Inline code is not a code block, and it does not have the same formatting or functionality as a code block. Unlike code blocks elements, inline code isn't a different element. It is still paragraph text, similar to text formatted with **bold** or *italic* markup. To learn more, see `Inline Markup <https://www.mongodb.com/docs/meta/style-guide/markup/inline/#inline-markup>`__.
54
-
55
- Code Block
56
- ``````````
57
-
58
- Code examples are always formatted in code blocks, which are distinct from the
59
- surrounding text. Code blocks support additional functionality, such as syntax
60
- highlighting or a caption label. For more information on the rST directives
61
- used to render a code block, see
62
- `Code Examples <https://www.mongodb.com/docs/meta/reference/code-blocks/>`__.
63
-
64
46
Types of Code Examples
65
47
----------------------
66
48
67
- .. NOTE related page: `Reference/Code Examples <https://www.mongodb.com/docs/meta/reference/code-blocks/`__
49
+ .. note::
68
50
69
- Although "code example" and "snippet" are often used interchangeably, this guidance uses "snippet" to mean a context-less block of code.
51
+ Although "code example" and "snippet" are often used interchangeably, this
52
+ guidance uses "snippet" to mean a context-less block of code.
70
53
71
54
We categorize code examples into the following types:
72
55
73
56
- :ref:`Usage Examples <code-category-usage-example>`: Standalone code blocks
74
57
that show how to perform a task, including the relevant setup and context.
75
58
- :ref:`Snippets <code-category-snippet>`: Code that illustrates a specific
76
- concept or detail in the context a larger example, tutorial, or reference page.
59
+ concept or detail in the context of a larger example, tutorial, or reference
60
+ page.
77
61
- :ref:`Sample Applications <code-category-sample-app>`: Runnable applications
78
62
demonstrating broader use cases.
79
63
@@ -82,10 +66,11 @@ We categorize code examples into the following types:
82
66
Usage Examples
83
67
~~~~~~~~~~~~~~
84
68
85
- Usage examples are complete , actionable code blocks that demonstrate how to
69
+ Usage examples are self-contained , actionable code blocks that show how to
86
70
accomplish a specific task using MongoDB tools, drivers, or APIs. Usage
87
- examples are self-contained--they include the context needed to understand,
88
- run, and adapt the code in the code block (e.g. variable declarations).
71
+ examples include enough context to understand, run, and adapt the code
72
+ contained in the code block (for example, the code includes any variable
73
+ declarations).
89
74
90
75
.. code-block:: csharp
91
76
@@ -99,49 +84,40 @@ run, and adapt the code in the code block (e.g. variable declarations).
99
84
.. _code-category-snippet:
100
85
101
86
Snippets
102
- ````````
87
+ ~~~~~~~~
103
88
104
- Snippets are narrowly scoped code blocks that illustrate a specific concept or
105
- detail, typically as part of a broader explanation or tutorial. Unlike usage
106
- examples, they are informational, rather than actionable .
89
+ Snippets are narrowly scoped code blocks that help explain a specific concept
90
+ or detail. They are typically used as part of a broader explanation or
91
+ tutorial, and are often meaningful only within that context .
107
92
108
- Snippets are intended to support other content, and they rely on the
109
- surrounding text or context to be meaningful . In some cases, a snippet may
110
- contain intentionally incomplete or invalid code for demonstration purposes
111
- (for example, a snippet showing all possible argument options for a command).
93
+ Snippets are intended to provide information. They aren't required to be valid
94
+ or runnable code . In some cases, a snippets may contain intentionally
95
+ incomplete or incorrect code for demonstration purposes (for example, a snippet
96
+ showing all possible argument options for a command).
112
97
113
98
Snippets fall into one of the following subtypes:
114
99
115
100
- **Non-MongoDB command:**: a command-line (CLI) command for any non-MongoDB
116
101
tooling (e.g. ``mkdir``, ``cd``, or ``npm``), often used in the context of a
117
102
tutorial.
118
103
119
- Snippets can fall into one of the following subcategories:
104
+ .. code-block:: shell
120
105
121
- - **Non-MongoDB command:** code blocks containing command-line commands for
122
- non-MongoDB tooling, typically in the context of a larger tutorial-style
123
- page. For example, terminal commands like ``mkdir``, ``cd``, ``npm``, etc.
106
+ dotnet run MyCompany.RAG.csproj
124
107
125
- .. code-block:: shell
126
108
- **Syntax example**: an example of the syntax or structure for an API method,
127
109
an Atlas CLI command, a ``mongosh`` command, or other MongoDB tooling.
128
110
129
- dotnet run MyCompany.RAG.csproj
111
+ .. code-block:: text
130
112
131
- - **Syntax example:** a tightly-scoped code block showing syntax for an API
132
- method, an Atlas CLI command, a ``mongosh`` command, or other MongoDB tooling.
113
+ mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net
133
114
134
- .. code-block:: text
135
115
- **Return example**: an example of an object, such as a JSON blob or sample
136
116
document, returned after executing a corresponding piece of code. Commonly
137
117
included as the output of an ``io-code-block``.
138
118
139
- mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net
140
-
141
- - **Return example**: a code block containing a JSON blob, example document, or
142
- other return object type. Use this snippet type to demonstrate what a user might expect from executing a piece of code.
119
+ .. code-block:: text
143
120
144
- .. code-block:: text
145
121
A timeout occurred after 30000ms selecting a server using ...
146
122
Client view of cluster state is
147
123
{
@@ -157,38 +133,16 @@ Snippets can fall into one of the following subcategories:
157
133
- **Configuration object example**: an example configuration object,
158
134
often represented in YAML or JSON, enumerating parameters and their types.
159
135
160
- A timeout occurred after 30000ms selecting a server using ...
161
- Client view of cluster state is
162
- {
163
- ClusterId : "1",
164
- Type : "Unknown",
165
- State : "Disconnected",
166
- Servers :
167
- [{
168
- ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }",
169
- EndPoint: "Unspecified/localhost:27017",
170
- ReasonChanged: "Heartbeat",
171
- State: "Disconnected",
172
- ServerVersion: ,
173
- TopologyVersion: ,
174
- Type: "Unknown",
175
- HeartbeatException: "<exception details>"
176
- }]
177
-
178
-
179
- - **Configuration object example**: a code block showing an example object,
180
- often represented in YAML or JSON, enumerating parameters and their types.
136
+ .. code-block:: ini
181
137
182
- .. code-block:: ini
183
-
184
- apiVersion: atlas.mongodb.com/v1
185
- kind: AtlasDeployment
186
- metadata:
187
- name: my-atlas-cluster
188
- spec:
189
- backupRef:
190
- name: atlas-default-backupschedule
191
- namespace: mongodb-atlas-system
138
+ apiVersion: atlas.mongodb.com/v1
139
+ kind: AtlasDeployment
140
+ metadata:
141
+ name: my-atlas-cluster
142
+ spec:
143
+ backupRef:
144
+ name: atlas-default-backupschedule
145
+ namespace: mongodb-atlas-system
192
146
193
147
.. _code-category-sample-app:
194
148
@@ -212,16 +166,17 @@ Keep the following in mind as you write code examples:
212
166
- Treat code like writing: Keep it simple, readable, and relevant to the task.
213
167
- Write code that is easy to understand, even if it isn't the most efficient or
214
168
clever.
215
- - Introduce each code block with context.
216
- - List or note any prerequisites or code dependencies, especially in a tutorial .
169
+ - Introduce each code block with context, as you would a list or table .
170
+ - Include any prerequisites or code dependencies needed for a piece of code .
217
171
- Use descriptive names that clearly convey the purpose of the code element
218
172
(e.g. variable, function, class) or placeholder.
219
173
- If a code is not intended to be directly used or adapted, such as a return
220
174
object example snippet, make sure the code block is not copyable. To learn
221
175
how to set the copyable option, see :ref:`code-block-reference`.
222
- - Don't write code examples for anti-patterns. If readers should be aware of an
223
- anti-pattern or a commonly made mistake, communicate this clearly in the
224
- surrounding text, an admonition, or an inline code comment.
176
+ - Don't write code examples for anti-patterns. If you need to note an
177
+ anti-pattern or a commonly made mistake, use the
178
+ surrounding text, an admonition, or an inline code comment, and ensure that
179
+ it cannot be mistaken for a recommended pattern.
225
180
- If a code example is not production worthy in a significant way, communicate
226
181
this to readers through inline code comments *and* in the surrounding text.
227
182
- Never use real customer data or hard code secrets in your code. If you're
@@ -237,32 +192,32 @@ Keep the following in mind as you write code examples:
237
192
- If you have to make changes or take additional steps to get an example to
238
193
work, make sure those details are reflected in the doc.
239
194
240
- Considerations for Robot Users, LLMs, and Other AI Models
241
- --------------------
242
-
243
195
.. note::
244
196
245
- For any questions or help writing or testing code examples, reach
197
+ For any questions or for help writing or testing code examples, reach
246
198
out to the ``@DevDocs`` team or use the ``#ask-devdocs`` Slack channel.
247
199
248
200
Considerations for LLMs
249
201
-----------------------
250
202
251
- The robot users (such as LLMs and other AI models) that consume our docs are unable to infer meaning or intent from context.
203
+ Unlike human readers, the robots that consume our docs (such as LLMs and other
204
+ AI models) can't infer meaning or intent from context. Robot users struggle
205
+ with ambiguity, unclear boundaries, partial or implicit information, and
206
+ non-standard patterns.
252
207
253
208
To ensure that our code examples are robot friendly, keep the following in mind:
254
209
255
- - Write code examples that are self-contained and provide enough information
256
- for robot users to understand their purpose without relying on surrounding
257
- text or context .
258
- - Don't split code into multiple pieces.
259
- - Use inline comments to clearly mark any omitted or truncated sections.
260
- - Explain intent, purpose, or expected code behavior through surrounding text *and* inline comments .
261
- - File names, file structure, and code follows idiomatic, canonical patterns
210
+ - Prefer atomic code examples: code should be as self-contained and self-descriptive as possible so that robot users can understand the code more easily.
211
+ - Explain intent, purpose, or expected code behavior through surrounding text
212
+ *and* inline comments .
213
+ - Don't interrupt a single code block with text explanations. Keep explanatory
214
+ text before or after the code block, and use inline comments for explanations
215
+ within the code block .
216
+ - Prefer conventional file structure, names, and canonical patterns
262
217
(e.g. ``main()``, ``index.js``, ``connect()``). Avoid unnecessary aliasing or
263
218
non-standard structures unless explained clearly.
264
219
- Use consistent, descriptive names that indicate purpose. Avoid vague names
265
220
like ``foo``, ``x``, or ``doStuff()``.
266
221
- Make sure the correct language is specified for every code block.
267
- - Clearly mark placeholders and explain how to replace them through an inline
268
- comment .
222
+ - Clearly mark and explain any placeholders, omissions, or truncated code
223
+ through inline comments .
0 commit comments