Share code snippets and samples in the community

In community questions and as a response we often see code snippets from Automation Studio programs. Sometimes even whole programs, libraries or projects are shared here, which is great!

Here are some tips, how you can improve the readability and usability of code snippets.

Use the preformated text feature

The forum supports preformated text in two ways. To use such blocks, you can use the editor button to start a block, or use the backtick character (`) manually.
image

The advantages are proper highlighting, code copy buttons and better layout also on devices with smaller screens.

Inline code blocks

One format type is inline blocks, which can e.g. be used for very small snippets which should be highlighted as code, such as variable names, data types…
If you press the preformated text button within a sentence, this format type is used. The code block is just written between single backticks.

This is a text with a small `code block` within a sentence.

Example:
You have to set the function block input MC_BR_ProcessConfig.Execute := TRUE in order to change the configuration.

Multi line code blocks

Whenever you want to share multiple lines of code, you should use multi line code blocks.

For this you have to press the preformatted text button on an empty line. In the message editor the block starts and ends with three (or more) backticks on an empty line.

```
Code block contents
```

The forum engine tries to distinguish the used programming language automatically and will offer syntax highlighting in the published message. It is also possible to set the language manually by writing the language id next to the opening backticks. I would recommend to manually define the language in order to prevent detection issues, especially on small code blocks.

```iecst
IF diSwimmingPoolFull THEN
    doWaterPump := FALSE;
END_IF
```

Useful languages in our context are:

  • iecst - Code snippets in IEC structured text, variable files, type files
  • c or cpp - C or C++ code and headers
  • xml - XML contens such as mapp View code snippets

Example:

VAR
    diSwimmingPoolFull : BOOL := FALSE; (*Pool water level full sensor input*)
    doWaterPump : BOOL := TRUE; (*Pool water pump switch output*)
END_VAR
(* Prevent pool overflow *)
IF diSwimmingPoolFull THEN
    doWaterPump := FALSE; // TODO: we should turn on the pump when empty
END_IF

Write code snippets in lists

Sometimes we write listings, e.g. for step-by-step guides. In such cases we can also write code lines by letting an empty line after the list element and indent the code lines by 7 spaces.

In editor:

1. Add variables to the .var file

       VAR
           diSwimmingPoolFull : BOOL := FALSE; (*Pool water level full sensor input*)
           doWaterPump : BOOL := TRUE; (*Pool water pump switch output*)
       END_VAR

Result:

  1. Add variables to the .var file

    VAR
        diSwimmingPoolFull : BOOL := FALSE; (*Pool water level full sensor input*)
        doWaterPump : BOOL := TRUE; (*Pool water pump switch output*)
    END_VAR
    

Share code as code

For bigger projects and binary files, it may be convenient to just upload a ZIP file of your project. But be aware that downloading a ZIP file from the internet can pose a security risk and may hinder people from watching your answer. I would recommend writing the code directly as code blocks whenever applicable.
If you provide a whole library or sample project, of course you should not copy / paste every file as code blocks :rofl:. Just upload a ZIP then or consider using GitHub or another code sharing service.

4 Likes