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

Ibrahim Elkhatib
0

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


Introduction

In the previous part of this series, we explored what clean code means in the context of PLC programming and why it’s essential for maintainability, reliability, and long-term success. Today, we’ll dive deeper into the practical side: how to apply clean code principles through naming conventions and commenting best practices. These foundational steps are crucial for writing PLC programs that are easy to understand, troubleshoot, and expand, regardless of the project's size or complexity.

PLC Programming


Article Contents

  • Introduction
  • Why Structure and Standards Matter in Industrial Automation
  • Pro Naming Conventions for PLC Programs
  • Pro Commenting Practices for PLC Code
  • Advanced Tips for Efficient, Maintainable PLC Code
  • Conclusion


Why Structure and Standards Matter in Industrial Automation

As industrial automation projects grow in size and complexity, the cost of poor code quality rises dramatically. Modern PLC projects often involve large teams, frequent updates, and long lifecycles. Without clear standards, code quickly becomes hard to read, debug, and modify, leading to lost production time, expensive errors, and frustrated engineers.

Pro Tip:
Start every project with a Functional Design Specification (FDS) and documented coding standards. This ensures everyone is aligned and provides a reference for future maintenance.


Pro Naming Conventions for PLC Programs

1. Use Descriptive, Consistent Names

  • Avoid raw addresses (e.g., I0.0, Q0.1). Instead, use names that describe the function, such as StartButton, TankLevelHigh, or Pump1_Run.
  • Stick to a naming convention (e.g., UpperCamelCase, lower_snake_case). Consistency makes code easier to scan and maintain.
  • Include units or types where relevant (e.g., Pressure_PSI, Temp_C).

Screenshot: TIA Portal variable table showing clear, descriptive names vs. raw addresses.

2. Avoid Duplicates and Overly Short Names

  • Ensure variable names are unique within their scope.
  • Choose names that are long enough to be meaningful but not cumbersome (typically 12–20 characters).

3. Use English and Avoid "Franco-Arabic"

  • Always use English for variable names and comments, even if your team is local. This ensures global readability and easier collaboration.

4. Don’t Hard-Code Values

  • Use constants or parameters for setpoints and thresholds, not magic numbers.


Pro Commenting Practices for PLC Code

1. Comment with Purpose

  • Describe intent, not just logic: Explain why a section exists, not just what it does.
  • Comment blocks, not every line: Focus on logical sections, functions, or rungs/networks.


2. Use Comments to Organize Code

  • Divide your program into logical blocks (e.g., Initialization, Safety Checks, Main Sequence, Alarms) with header comments for each.

3. Write Comments First

  • Outline your code with comments before you start programming. This helps structure your logic and serves as a checklist for development.

4. Keep Comments Up to Date

  • Regularly review and update comments as code evolves to avoid confusion and outdated information.


Advanced Tips for Efficient, Maintainable PLC Code

1. Modular Programming

  • Break your code into reusable function blocks, subroutines, or user-defined functions.
  • Each module should handle a specific, well-defined task (e.g., PumpControl, AlarmHandler).

2. Use Libraries and Templates

  • Develop and maintain a library of standard function blocks and templates for recurring tasks. This saves time and ensures consistency across projects.

3. Minimize Global Variables

  • Prefer local variables and pass data explicitly between modules. Too many global variables make debugging and maintenance difficult.

4. Robust Error Handling

  • Implement structured error handling and alarms for all critical operations. 
  • Use dedicated blocks for faults and ensure they are clearly separated from control logic.

5. Regular Code Reviews and Testing

  • Schedule peer reviews to catch errors early and share knowledge.
  • Test your code in simulation and on real hardware before deployment.

6. Documentation and Version Control

  • Keep a changelog and use version control tools to track code evolution and facilitate team collaboration.


Conclusion

Clean code in PLC programming isn’t just about style—it’s about building systems that are robust, safe, and easy to maintain for years to come. By adopting clear naming conventions, purposeful commenting, and modular design, you’ll create PLC programs that are a pleasure to work with and stand the test of time.



Share and Connect

If you found this lesson valuable, share it with your colleagues and team.
Feedback and questions are welcome—your engagement helps us continue this clean code series!


    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