Skip to content

Commit 83785a8

Browse files
authored
Recommend snippet syntax (#1209)
* update template to recommend code snippets We prefer them to inline code that doesn’t run through the CI build. * Fix contributing link * update list of languages to the correct smaller set. * respond to feedback * respond to feedback.
1 parent 956a076 commit 83785a8

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

styleguide/template.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,53 @@ You can use a [Markdown table generator tool](http://www.tablesgenerator.com/mar
202202

203203
## Code
204204

205-
### Code blocks with language identifier
205+
The best way to include code is to include snippets from a working sample. Create your
206+
sample following the instructions in the [contributing guide](../CONTRIBUTING.md#contributing-to-samples).
207+
208+
You can include the code using include syntax:
209+
210+
```
211+
[!code-csharp[<title>](<pathToFile>#<RegionName)]
212+
```
213+
214+
The example above shows C# syntax, but other languages are supported.
215+
Use `code-fsharp` for F# samples; use `code-vbnet` for Visual Basic samples.
216+
Other languages that are supported are:
217+
* C++: `code-cpp`
218+
* HTML: `code-html`
219+
* JavaScript: `code-javascript`
220+
* Powershell: `code-ps`
221+
* SQL: `code-sql`
222+
* XML: `code-xml`
223+
224+
225+
226+
The text you place for `<title>` shows up as a rollover on the text. The `<pathToFile>`
227+
is the path to the source file. The `<RegionName>` should be a region in your source
228+
code that should be included. Use the `#region` and `#endregion` preprocessor syntax
229+
to specify the region of code to include.
230+
231+
For cases where regions don't work, you can specify the start and end of a snippet
232+
using an XML element name in a single line comment. For example, you could write this in C#:
233+
234+
```csharp
235+
// <CodeToInclude>
236+
int j = 5;
237+
int i ; 10;
238+
int sum = i + j;
239+
// </CodeToInclude>
240+
```
241+
242+
In other languages, use the comment syntax for that language.
243+
Finally, you can use line
244+
numbers: `#L1-L10` would include lines 1 through 10. We discourage line numbers
245+
because they are very brittle.
246+
247+
Including snippets from full programs ensures that all code runs through our Continuous Integration (CI)
248+
system. However, if you need to show something that causes compile time or
249+
runtime errors, you can use inline code blocks.
250+
251+
### Inline code blocks with language identifier
206252

207253
Use three backticks (\`\`\`) + a language ID to apply language-specific color coding to a code block. Here is the entire list of [GFM language IDs](https://github.com/jmm/gfm-lang-ids/wiki/GitHub-Flavored-Markdown-(GFM)-language-IDs).
208254

0 commit comments

Comments
 (0)