Clean Code vs. Dirty Code in PLC Programming – Part One

Ibrahim Elkhatib
0

Clean Code vs. Dirty Code in PLC Programming – Part One



Introduction

In the world of computer programming, the concept of “clean code” is well established. However, in the field of PLC programming, these best practices are often overlooked. This is largely because many PLC programmers come from an electrical engineering background and specialize in industrial automation, where the Ladder Diagram (LD) language is popular due to its similarity to traditional electrical control diagrams.

But Ladder Diagram is a programming language, not just a schematic. Over the decades, PLC programming languages have evolved, and today, writing clean, maintainable code is just as important in automation as in any other software field.

In this article, we’ll introduce the core concepts of clean code in PLC programming and share practical tips. In future articles, we’ll dive deeper with real-world PLC examples.


Article Contents

  • IEC 61131-3 Standard: The Foundation of PLC Languages
  • Why Clean Code Matters in Industrial Automation
  • What is Clean Code?
  • Essential Tips for Writing Clean PLC Code
  • Conclusion


IEC 61131-3 Standard: The Foundation of PLC Languages

The IEC 61131-3 standard is the international benchmark for PLC programming languages. It defines five official languages, divided into two categories:

Graphical Languages:

  • Ladder Diagram (LD)
  • Function Block Diagram (FBD)
  • Sequential Function Chart (SFC)

Textual Languages:

  • Instruction List (IL)
  • Structured Text (ST)

This standard ensures consistency across manufacturers and makes PLC programs more portable and maintainable, especially with modern platforms like CODESYS.



Graphic: Table showing the five IEC 61131-3 PLC programming languages and their categories.


Why Clean Code Matters in Industrial Automation

In industrial environments, change is constant. Machines are upgraded, sensors are replaced, and production requirements evolve. This means your PLC code will almost certainly need maintenance, adaptation, or troubleshooting in the future.

Benefits of Clean Code:

  • Easier, faster maintenance and troubleshooting
  • Reduced downtime and errors
  • Simpler onboarding for new engineers
  • Greater flexibility for system upgrades

Tip: Clean code is not just for experts—every engineer, from beginner to pro, should strive for readable, maintainable code.


What is Clean Code?

Clean code is code written according to best practices, making it:

  • Clear and easy to read
  • Simple to understand and follow
  • Easy to modify or extend
  • Well-structured and organized
  • Open to improvement

Essential Tips for Writing Clean PLC Code

1. Start with an Algorithm

Before you write any code, develop a clear algorithm or flowchart for your machine or process.

  • Understand the process, inputs, outputs, and required logic.
  • Use tools like flowcharts or SFC diagrams to map out states and transitions.

Graphic: Flowchart or SFC diagram for a simple machine sequence.


2. Divide Your Program into Logical Sections

Organize your code into clear sections or Program Organization Units (POUs), such as:

  • Main: Initialization, mode selection (manual/auto), and calling subroutines
  • Inputs: Mapping and debouncing inputs, analog scaling
  • Sequence: Main logic and state transitions
  • Outputs: Output mapping and control
  • Alarms: Fault and alarm handling

Tip: Modular code is easier to read, test, and modify.


3. Name Everything Clearly

Use descriptive names for variables, functions, and routines.

  • Avoid cryptic abbreviations.
  • Follow a consistent naming convention.


4. Map Inputs and Outputs

Create dedicated sections or tables for input/output mapping.

  • Makes it easy to update addresses if hardware changes.
  • Reduces errors and speeds up troubleshooting.


5. Comment Your Code

Document your logic with comments:

  • Add comments for each rung, network, or function.
  • Explain the purpose of each section and any complex logic.


6. Group Related Code by Function

Keep related logic together.

  • Improves readability and makes it easier to trace the flow of your program.


7. Use Structured Programming Techniques

  • Use subroutines, function blocks, and state machines where appropriate.
  • Avoid duplicating code—reuse logic when possible.


8. Test and Review

  • Simulate and test your code before deployment.
  • Ask a colleague to review your code for clarity and logic.


Conclusion

Clean code is the foundation of reliable, maintainable industrial automation. By following these best practices, you’ll make your PLC programs easier to understand, modify, and troubleshoot, saving time and reducing errors in the long run.

Stay tuned for future articles with practical PLC code examples and advanced clean coding techniques.


Share and Connect

If you found this article useful, please share it with your colleagues and leave your feedback in the comments.
Your support helps us continue providing valuable automation content.


Tags
PLC

Post a Comment

0 Comments

Hi, please leave a comment.

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!
To Top