Design patterns and anti-patterns

IT and Engineering Insight: Good software design solves hard problems in a standard way and is well documented and easy to maintain and extend, as opposed to code that seemed reasonable at some time but has spread and become a nightmare to maintain or extend.


Does a part of your system or part of your code seem to always be broken, or is it so complex that no one wants to work on it because it always breaks? If so, then you may be suffering from the bad habit of anti-patterns. An anti-pattern is the evil twin of a good design pattern. A good design pattern is a reusable requirement, design, or implementation that solves hard problems in a standard way, which is well designed, well documented, easy to maintain, and easy to extend. An anti-pattern is a design pattern that may have seemed reasonable at some time but has spread into many different parts of the system or code and is now a nightmare to maintain or extend.

20% of the code, 80% of the problem

Anti-patterns are often the root cause of the 20% of a system that cause 80% of the problems.

Anti-patterns can show up in requirements, design, or code. In requirements, they are inconsistent or conflicting requirements written in different parts of the requirements specification. Each requirement seems reasonable on its own, but when they are combined they result in overly complex systems to try to resolve the conflicts.

In design, anti-patterns usually arise from copying a pattern that was required in a previous system, such as handling of strings in a PLC or copying files in a PC, but that is no longer needed and adds complexity and reduces performance. A typical anti-pattern involves handling of exceptions. Modern languages contain “catch” and “try” methods to simplify exception handling, but copied patterns from older systems will have deeply nested conditionals that are nearly impossible to debug or change. In addition to error handling, anti-patterns may also be related to security setup, complex data manipulation, and string data manipulation. Anti-patterns show up in code through poor programming techniques. These anti-patterns can be as simple as using single characters for variable names, because “that’s the way we did it in Fortran”;  exceedingly long modules and functions, because “that’s the way we had to do it in Basic”; or byte-at-a-time interfaces, because “the old network only supported one-byte-at-a-time transfers.”

Familiarity, without review

Anti-patterns are really just a bad habit, in which patterns from previous projects are reused without review. They are not reviewed because everyone says, “that’s how we have always done it, so we don’t have to look at that part.”

Anti-patterns may not always be the sign of a poor programming or design techniques; often they are the choice of a bad algorithm. In an analysis of a system with over 1 million lines of code and hundreds of modules in one project, we found that a small percentage of modules were causing the majority of problems, even correcting for the size of the modules. We measured size in several ways: lines of code, number of statements, and number of variables per statement. In all cases the same modules came up as outside of the normal range. There was no relationship to the developer or to any specific part of the system. After analysis, we discovered that all the problems were related to poor algorithm choices. A poor algorithm, even programmed by a good programmer, still caused more problems that should be expected. If there was one bad reason for the anti-pattern, it was the lack of review of the algorithm, and the tradition of just using the same pattern in multiple places.

It is easy to identify the anti-patterns in your system. Ask the maintainers to identify the anti-pattern in the code, the 20% of the code that causes 80% of the problems. Ask the coders to identify the anti-pattern in the design, the 20% of the design that takes up 80% of the code. Ask the designers to identify the anti-pattern in the requirements, the 20% of the requirements that make up 80% of the design complexity. Anti-patterns are usually not visible by the team creating them, but they are visible to the next team in the system lifecycle; all you have to do is ask them.

Allocate to fix old code

Fixing anti-patterns can be difficult. Usually the cost to fix an anti-pattern after discovery must be weighed against any continual maintenance expenses or redesign expenses. If you expect to extend or fix the anti-pattern section more than a few times, it is usually more cost effective to redesign and re-implement. Unfortunately, this is a difficult decision to justify because the payment can be slow. Fortunately, removing anti-patterns does not have to be done all at once. A reasonable rule of thumb is to allocate 10%-20% of your maintenance budget to replacing anti-patterns with good design patterns. Over time you will find that your maintenance and support costs will stabilize or decrease as bad patterns are replaced with good ones.

Eliminating anti-patterns will give you a system that is easier to maintain and extend, but be sure to look for anti-patterns in all parts of your project, requirements, design, and code. Don’t let the bad habit of no reviews seed your system with future anti-pattern problems.

- Dennis Brandl is president of BR&L Consulting in Cary, N.C., His firm focuses on manufacturing IT. Contact him at Edited by Mark T. Hoske, content manager CFE Media, Control Engineering. 

No comments
The Top Plant program honors outstanding manufacturing facilities in North America. View the 2013 Top Plant.
The Product of the Year program recognizes products newly released in the manufacturing industries.
The Engineering Leaders Under 40 program identifies and gives recognition to young engineers who...
A cool solution: Collaboration, chemistry leads to foundry coat product development; See the 2015 Product of the Year Finalists
Raising the standard: What's new with NFPA 70E; A global view of manufacturing; Maintenance data; Fit bearings properly
Sister act: Building on their father's legacy, a new generation moves Bales Metal Surface Solutions forward; Meet the 2015 Engineering Leaders Under 40
Cyber security cost-efficient for industrial control systems; Extracting full value from operational data; Managing cyber security risks
Drilling for Big Data: Managing the flow of information; Big data drilldown series: Challenge and opportunity; OT to IT: Creating a circle of improvement; Industry loses best workers, again
Pipeline vulnerabilities? Securing hydrocarbon transit; Predictive analytics hit the mainstream; Dirty pipelines decrease flow, production—pig your line; Ensuring pipeline physical and cyber security
Upgrading secondary control systems; Keeping enclosures conditioned; Diagnostics increase equipment uptime; Mechatronics simplifies machine design
Designing positive-energy buildings; Ensuring power quality; Complying with NFPA 110; Minimizing arc flash hazards
Building high availability into industrial computers; Of key metrics and myth busting; The truth about five common VFD myths

Annual Salary Survey

After almost a decade of uncertainty, the confidence of plant floor managers is soaring. Even with a number of challenges and while implementing new technologies, there is a renewed sense of optimism among plant managers about their business and their future.

The respondents to the 2014 Plant Engineering Salary Survey come from throughout the U.S. and serve a variety of industries, but they are uniform in their optimism about manufacturing. This year’s survey found 79% consider manufacturing a secure career. That’s up from 75% in 2013 and significantly higher than the 63% figure when Plant Engineering first started asking that question a decade ago.

Read more: 2014 Salary Survey: Confidence rises amid the challenges

Maintenance and reliability tips and best practices from the maintenance and reliability coaches at Allied Reliability Group.
The One Voice for Manufacturing blog reports on federal public policy issues impacting the manufacturing sector. One Voice is a joint effort by the National Tooling and Machining...
The Society for Maintenance and Reliability Professionals an organization devoted...
Join this ongoing discussion of machine guarding topics, including solutions assessments, regulatory compliance, gap analysis...
IMS Research, recently acquired by IHS Inc., is a leading independent supplier of market research and consultancy to the global electronics industry.
Maintenance is not optional in manufacturing. It’s a profit center, driving productivity and uptime while reducing overall repair costs.
The Lachance on CMMS blog is about current maintenance topics. Blogger Paul Lachance is president and chief technology officer for Smartware Group.