A lot of people are posting code here on the forum, which is great, but can lead to really messy topics. Here are a few markdown pointers for creating readable code posts.
Use code blocks for short snippets
Here’s an example of how to format some python code with triple backticks:
# Utility routine for printing the input vector
def formatRow(x):
s = ''
for c in range(len(x)):
if c > 0 and c % 10 == 0:
s += ' '
s += str(x[c])
s += ' '
return s
All you have to do is add three backticks on a line by themselves before and after your code. Here is what the actual markdown text of the post looks like:
```
# Utility routine for printing the input vector
def formatRow(x):
s = ''
for c in range(len(x)):
if c > 0 and c % 10 == 0:
s += ' '
s += str(x[c])
s += ' '
return s
```
Declare the programming language
You may also specify a programming language, like this:
function deduplicateCollaborators(collaborators) {
var foundLogins = [];
return _.filter(collaborators, function (collaborator) {
var duplicate = false
, login = collaborator.login;
if (foundLogins.indexOf(login) > -1) {
duplicate = true;
} else {
foundLogins.push(login);
}
return ! duplicate;
});
}
The forum software knows this is javascript because I told typed the word “javascript” immediately after the first set of backticks. The post text looks like this:
```javascript
function deduplicateCollaborators(collaborators) {
var foundLogins = [];
return _.filter(collaborators, function (collaborator) {
var duplicate = false
, login = collaborator.login;
if (foundLogins.indexOf(login) > -1) {
duplicate = true;
} else {
foundLogins.push(login);
}
return ! duplicate;
});
}
```
Use Gist or Pastebin for longer code blocks
If you have a large code block to paste, you can use gist or pastebin to store the code. When you paste the URL to those pages, they will embed nicely like this: