You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Always follow best practices for security in your code and in any user
202
-
recommendations or instructions. For example, never hard code secrets or
203
-
credentials in an example.
205
+
Our code examples should always follow generally accepted coding and security
206
+
best practices, and all other applicable guidelines in this Style Guide that
207
+
don't conflict with language-specific standards. Remember that users copy and
208
+
use these code examples outside of our docs.
209
+
210
+
Keep the following in mind as you write code examples:
211
+
212
+
- Treat code like writing: Keep it simple, readable, and relevant to the task.
213
+
- Write code that is easy to understand, even if it isn't the most efficient or
214
+
clever.
215
+
- Introduce each code block with context.
216
+
- List or note any prerequisites or code dependencies, especially in a tutorial.
217
+
- Use descriptive names that clearly convey the purpose of the code element
218
+
(e.g. variable, function, class) or placeholder.
219
+
- If a code is not intended to be directly used or adapted, such as a return
220
+
object example snippet, make sure the code block is not copyable. To learn
221
+
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.
204
225
- If a code example is not production worthy in a significant way, communicate
205
-
this to readers clearly.
206
-
- Don't write code examples that demonstrate the wrong way to do something. In
207
-
the rare instances that justify calling out how *not* to do something (eg, to
208
-
caution against a common bad practice or frequently made error), do so in the
209
-
page copy, in an admonition, or through inline code comments that can't be
210
-
mistaken for part of the code.
211
-
212
-
Provide Context and Comments
213
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214
-
215
-
Try to include enough information to ensure reader comprehension without
216
-
stating the obvious or overwhelming with unnecessary details.
217
-
218
-
- Introduce each code block with a high-level explanation of what the example
219
-
does, and why it's useful.
220
-
- List specific prerequisites or dependencies.
221
-
- Include code comments directly in the code block to explain or call out
222
-
important details, such as non-obvious logic or placeholder values that users
223
-
need to modify. Don't restate the code.
224
-
225
-
.. TODO To learn more, see `TBD - Comments <PLACEHOLDER>`__.
226
-
227
-
Test Every Example
228
-
~~~~~~~~~~~~~~~~~~
229
-
230
-
Non-working or incorrect examples negatively impact our credibility. Testing
231
-
user-facing code examples ensures that they do what we say they do and work
232
-
as expected. This is especially important for code that you didn't write
233
-
yourself, such as an example provided by a SME or stakeholder.
234
-
235
-
.. TODO update this section and the callout to DevDocs
236
-
237
-
.. note::
238
-
239
-
For information or assistance with testing code examples, reach out to the
240
-
DevDocs team in the ``#ask-devdocs`` Slack channel or through the
241
-
``@DevDocs`` GitHub team tag.
242
-
243
-
- Writers: Always compile and test your code examples. Your code should pass as
244
-
written for the documentation.
245
-
- Reviewers: Run all code examples as part of your review.
246
-
247
-
For tutorials or content with multi-step examples, confirm that the code works
248
-
by walking through every step exactly as written:
249
-
250
-
- If there are any prerequisite steps or additional setup, begin at the assumed
251
-
starting point and complete the prerequisites exactly. Confirm that every step is correct, comprehensive, and necessary.
252
-
- If you have to make changes or take additional steps to get an example to
253
-
work, make sure those details are reflected in the Doc.
226
+
this to readers through inline code comments *and* in the surrounding text.
227
+
- Never use real customer data or hard code secrets in your code. If you're
228
+
unsure how to handle secrets in your code, reach out to the DevDocs team.
229
+
- Use inline code comments to explain or call out important details, including:
230
+
- Non-obvious logic or intent. Don't restate the code.
231
+
- Omitted or truncated code.
232
+
- Test every code example to ensure it works as intended:
233
+
- When writing or reviewing, run the code as it displays on the page.
234
+
- For tutorials or multi-step examples, begin at the starting point and
235
+
complete all steps exactly, including any prerequisites or setup. Don't
236
+
skip steps or assume they're correct.
237
+
- If you have to make changes or take additional steps to get an example to
238
+
work, make sure those details are reflected in the doc.
254
239
255
240
Considerations for Robot Users, LLMs, and Other AI Models
256
241
--------------------
257
242
258
243
.. note::
259
244
260
-
This guidance uses phrases such as "robots *understand*" or "models *interpret* code" as convenient shorthand when discussing robot users. interpreted to mean that robot users have human-like reasoning capabilities.
261
-
262
-
Robot users, such as LLMs (large language models), web crawlers, and other AI models, also consume our Docs content. However, robot users don't understand, think, or learn like a human user. They don't "read code" or "get context." Instead, they are predictive engines that pattern-match, tokenize, and respond based on probability and training data.
245
+
For any questions or help writing or testing code examples, reach
246
+
out to the ``@DevDocs`` team or use the ``#ask-devdocs`` Slack channel.
263
247
264
-
To write code examples that robot users can understand quickly and interpret accurately, keep the following considerations in mind:
265
-
266
-
- Code examples are as complete as possible. If there are omissions or truncations in an example, indicate the omissions clearly (for example, a code comment ``// imports omitted for brevity`` at the relevant line).
267
-
- Ensure that transitions to code examples are obvious and explicit.
268
-
- Never use inline code markup for code examples. Always use a code block directive, preferably as a shared include.
269
-
- Use placeholder variables only when necessary, and ensure they are clearly commented or explained. Avoid placeholder variables that are not self-explanatory.
270
-
- Do not insert explanations mid-block. Commentary should be before or after the code block, or as inline comments.
271
-
272
-
273
-
274
-
- Code examples are self-contained and provide enough information for robot users to understand their purpose without relying on surrounding text or context.
275
-
- code examples that provide enough information for robot users to understand their purpose without relying on surrounding text or context.
276
-
- Code is consistently formatted and uses syntax-highlighted Markdown fences (eg., java).
277
-
- Uses canonical imports, namespaces, and entry points (no unnecessary aliasing).
278
-
- Placeholder variables are either avoided or clearly commented/explained.
279
-
- Code includes concise, purposeful inline comments explaining intent or function.
280
-
- File names and structure follow standard conventions (e.g., main.py, index.js, etc.).
281
-
- Code blocks are single-language — no mixing languages inside one fence.
282
-
- All code fences are properly closed to avoid truncating downstream content.
283
-
284
-
285
-
Nice-to-Have: Improves Model Understanding and UX
286
-
- Function/variable names are descriptive and realistic (connectToMongoDB() vs foo()).
287
-
- Includes expected output blocks after commands or API calls when relevant.
288
-
- Uses consistent multi-language tab structures for examples (e.g., JS/Python/Java).
289
-
- Avoids over-abstraction — keeps just enough context to make examples meaningful.
290
-
- Keeps logic explicit, even if it's less clever (avoid nested ternaries, dense one-liners).
291
-
- Experimental, deprecated, or unsafe examples are clearly annotated with warnings.
292
-
- Avoids mixing config, shell, and code in the same block — separate into clearly labeled sections.
293
-
- Examples reflect the latest stable version of the tooling or API unless otherwise stated.
294
-
295
-
296
-
When writing code examples, there are a few considerations to keep in mind for robot users that also consume our Docs content. Unlike human users, robot users cannot interpret meaning or context directly.
297
-
298
-
299
-
300
-
.. collapsible::
301
-
:heading: Prerequisites
302
-
:sub_heading: Complete the previous steps in the tutorial and install dependencies
303
-
:expanded:
304
-
305
-
Body content goes here.
306
-
307
-
.. AI models and web crawlers--such as LLMs, Googlebot, or machine translation engines--are referred to as robot users. They also consume our Docs content, so it's important to ensure that our Docs content, including code examples, effectively meets the needs of both human and robot users. Many of these considerations apply to code examples the same way they do to the rest of our Docs.
308
-
309
-
.. For example, the following are applicable to both human and robot users:
310
-
311
-
.. - Clear, descriptive headings and subheadings make it easier for users to understand the overall structure and purpose of the page and its elements, including code examples.
312
-
.. - Enforcing a consistent, standard style and formatting, for page copy and code, ensures that users can parse the content easily and effectively.
313
-
314
-
.. - Use clear and descriptive variable names, function names, and class names. Avoid using generic or ambiguous names that don't convey the purpose of the code. This helps robot users understand the code's functionality and intent.
315
-
316
-
.. The considerations specific to robot users are related to how LLMs and other AI models interpret meaning, which is very limited compared to human users can. Robot users can't reliably understand or assume context or intent, extrapolate extract or assume meaning from the surrounding text or context.
317
-
318
-
319
-
320
-
.. The following are considerations to help optimize code examples for robot users, especially LLMs:
321
-
322
-
323
-
.. -
324
-
325
-
.. - Provide context for every code example through introductory text and succinct code comments. See the
326
-
.. `Provide Context and Comments <#provide-context-and-comments>`__ section
327
-
.. above for more information.
328
-
329
-
.. - For longer code examples (such as a single function), opt for keeping it as a single code block with explanatory inline comments as needed. Avoid splitting it into smaller blocks interspersed in the page text.
248
+
Considerations for LLMs
249
+
-----------------------
330
250
331
-
.. .. TODO Add explanatory image
251
+
The robot users (such as LLMs and other AI models) that consume our docs are unable to infer meaning or intent from context.
252
+
253
+
To ensure that our code examples are robot friendly, keep the following in mind:
254
+
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.
0 commit comments