Code markup: Correct style

Since there have been several code examples posted that were not formatted quite right, I thought I would give an example of why it matters.

So let’s go on the assumption that “code should be formatted differently from text” – let’s type in a line of code and try a couple of formatting options.

In the formatting bar, I see (left to right):

  • A quote balloon = “quote whole post” – that depends on some other post, so, not relevant.
  • Boldface – nah
  • Italic – nah
  • Link – nah
  • Big quote mark – ok, let’s try that
  • </> – ok, let’s try that
  • Upload – nah
  • Lists, smileys etc. – nah

So I type in "Hello World!".postln;, select it, and click the big quote mark. This puts a > before the text.

“Hello World!”.postln;

In the post preview, I see the code with gray shading – so something is different.

Now pretend you’re forum user who wants to run the code. So you copy the code out of the preview (this is what other users will see), paste into SC and run it:

“Hello World!”.postln;

->
ERROR: syntax error, unexpected CLASSNAME, expecting $end
  in interpreted text
  line 1 char 14:

  “Hello World!”.postln; 
           ^^^^^
-----------------------------------
ERROR: Command line parse failed

Notice that the quote marks around "Hello World!" are different. The original code has straight quotes. The forum reformatted those to curly typographic quotes.

The curly quotes are not valid string delimiters in SC.

So, forum users who want to try your code have to do some extra find/replace just to get the code to run at all. Not a good use of time.

So that didn’t work. What about </>? That seems promising, since it looks like HTML delimiters.

"Hello World!".postln;

Now: There’s a gray background, and the typeface is different (monospaced, which is usually used for code) – and the quotes are still straight style.

Copy that to SC:

"Hello World!".postln;

->
Hello World!
-> Hello World!

No problem.

For multi-line blocks, one line with triple-backticks, then the code, then another line of triple-backticks:

```
"Hello World!".postln;
```

Appears like:

"Hello World!".postln;

A little attention to this makes it easier for other people to help you.

hjh

4 Likes