PLCcheck

SCL Best Practices: Naming Conventions, Structure, and Comments

SCL programming best practices based on the Siemens Programming Guideline. Covers naming conventions, code structure, REGION keyword, commenting, language selection, and common mistakes to avoid.

·10 min read
SCLbest practicesnaming conventioncode structureREGIONcommentsprogramming guidelineSiemensTIA Portal

Diesen Artikel auf Deutsch lesen

SCL Best Practices: Naming Conventions, Structure, and Comments

These best practices are based on the Siemens Programming Guideline for S7-1200/S7-1500 (Document ID 81318674) and the associated Programming Style Guide — the official Siemens recommendations for writing maintainable, portable, high-quality SCL code.

Naming Conventions

Identifier Rules

Siemens recommends lowerCamelCase for all identifiers. The initial letter is lowercase, each new word starts with an uppercase letter, and no separators (underscore, hyphen) are used.

RecommendationExampleAvoid
lowerCamelCaseconveyorSpeedConveyor_Speed, CONVEYOR_SPEED
Max. 24 charactersmotorTempAlarmmotorTemperatureAlarmHighLevel
Descriptive namesfillValveOpenM10_0, a, x1
No abbreviation chainsmotorTempmTmpAlm

Prefix Convention (Siemens Style Guide)

PrefixMeaningExample
(none)Standard variableconveyorSpeed
inst / InstInstance (single/multi)instMotorControl
statStatic variablestatLastValue
tempTemporary variabletempCalcResult
c / CConstantcMaxSpeed

Block Naming

Block TypeConventionExample
FBDescribes the functionFB_MotorControl
FCDescribes the calculationFC_ScaleAnalog
DB (global)Describes the data contentDB_RecipeParameters
DB (instance)Auto-generated or inst prefixinstMotorControl_DB
OBKeep Siemens default namesMain [OB1]

Code Structure

Use REGION to Structure SCL Blocks (V14+)

REGION in SCL is comparable to a network in LAD/FBD. It creates collapsible sections with descriptive titles:

REGION Motor Control
    // Start/stop logic for conveyor motor
    IF startButton AND NOT fault THEN
        motorRunning := TRUE;
    ELSIF stopButton OR fault THEN
        motorRunning := FALSE;
    END_IF;
END_REGION

REGION Temperature Monitoring
    // Scale and check temperature
    currentTemp := INT_TO_REAL(rawTemp) * 100.0 / 27648.0;
    tempAlarm := currentTemp > cMaxTemp;
END_REGION

Why this matters: When someone opens a 500-line SCL block, REGIONs let them collapse everything and see only the section titles — just like network titles in LAD.

One Function per Block

Each FB or FC should do one thing. If you find yourself writing a block with 10 REGIONs covering unrelated functionality, split it into separate blocks.

ApproachRecommendation
One OB1 with all logicSplit into called FBs/FCs
One FB that does everythingSplit by machine function
Shared utilities (scaling, limits)Separate FC library

Use FBs for Stateful Logic, FCs for Stateless

Prefer Multi-Instances Over Single-Instances

When an FB contains inner FBs (e.g., timers, counters), declare them as multi-instances in the static section rather than creating separate instance DBs. This keeps all data for one machine function in one DB.

Comments

What to Comment

Always CommentNever Comment
The purpose of each block (header comment)Obvious operations (i := i + 1; // increment i)
Why a specific value was chosenEvery single line
Non-obvious logic or workaroundsCommented-out old code (delete it)
Interface parameters (Input/Output purpose)
REGION titles (visible when collapsed)

Commenting Style

// Single-line comment — use for brief explanations

(* Multi-line comment
   Use for block headers or detailed explanations
   of complex algorithms *)

Language Rule

The Siemens Style Guide requires: Do not mix languages within a project. If the project language is English, all comments, variable names, and block names must be in English. Mixing English variable names with German comments creates confusion.

Common Mistakes to Avoid

1. Global Variable Access Inside FBs/FCs

The Siemens Guideline states: "Tags are only used locally. Access to global data is not permitted within FCs and FBs." Pass all data through the block interface (Input, Output, InOut) — this makes blocks reusable and testable.

// WRONG: direct access to global DB inside FB
IF "DB_Global".startCommand THEN ...

// RIGHT: pass through interface
// Block interface: Input startCommand : BOOL
IF #startCommand THEN ...

2. Modifying FOR Loop Counters

The Siemens Guideline explicitly forbids modifying the loop counter inside a FOR loop body. The behavior is undefined and varies between CPU types.

// WRONG: modifying loop counter
FOR i := 0 TO 10 DO
    IF condition THEN i := i + 2; END_IF;   // UNDEFINED behavior
END_FOR;

// RIGHT: use WHILE if you need variable stepping
i := 0;
WHILE i <= 10 DO
    IF condition THEN i := i + 3;
    ELSE i := i + 1;
    END_IF;
END_WHILE;

3. Using Bit Markers (M) Instead of Global DBs

On S7-1500, global data blocks with optimized access are faster and safer than bit markers. Markers (M) have overlapping byte/word/double-word access — MW 10 overwrites MB 10 and MB 11. Global DB variables are independent.

4. Hardcoded Values

Replace magic numbers with named constants:

// WRONG
IF temperature > 85.0 THEN alarm := TRUE; END_IF;

// RIGHT
IF temperature > cAlarmTempHigh THEN alarm := TRUE; END_IF;

Which Language for Which Task?

TaskRecommended LanguageReason
Standard blocks, calculationsSCLBest readability, no performance disadvantage
Binary logic, simple interlocksLAD or FBDEasier online diagnosis
Call environments (OB1 structure)LAD or FBDVisual block interconnection
Sequential processesGRAPHPurpose-built for step sequences
Legacy compatibility (S7-300/400 only)STL/AWLOnly when required

The Siemens Style Guide states: "The preferred programming language for standard blocks is SCL. It provides the most compact form and best readability of all programming languages."


Let PLCcheck Pro Check Your Code Quality

PLCcheck Pro identifies naming inconsistencies, dead code, missing comments, hardcoded values, and global variable access inside blocks — automatically.

Upload your code for quality analysis →

Part of the SCL Reference. Based on Siemens Programming Guideline (81318674). Maintained by PLCcheck.ai. Not affiliated with Siemens AG.

Related Articles

Analyze your PLC code with AI

PLCcheck Pro explains, documents, optimizes, and migrates PLC code — automatically.

Try PLCcheck Pro →
← Back to Blog

Not affiliated with Siemens AG. S5, S7, STEP 5, STEP 7, and TIA Portal are trademarks of Siemens AG.