Object-Oriented Programming (OOP) is a programming technique that lends itself to structured and modular software code. Modular coding can be implemented with virtually any programming language from ladder to function block, but OOP was designed from the ground up with modules in mind.
This means that OOP tools and programming languages naturally lead to discrete modules or blocks of code linked together with rules that are part of OOP programming languages.
The flip side to modular programming is simple, monolithic or "spaghetti" style programming. These types of programs don't use modules but rather have all code in one continuous string. Monolithic programming skips advanced programming techniques and goes straight for the quick fix. This often means programmers are writing code without first thinking about the overall structure of the program. This may appear an expeditious way to write a program, but problems often result.
A program written without a pre-defined structure can become very complex, and even incomprehensible to all except the original programmer. Even the original programmer can become confused as the program grows more complex or when the program needs to be revisited after a period of time.
By contrast, structured and modular programming requires thought and planning prior to coding. If structure and modules are desired, then OOP is an ideal tool.
Modular coding in general and OOP in particular offer many advantages over monolithic code including ease of test, reuse of code and portability to different hardware target platforms.
OOP was developed for good reasons, and under the right circumstances it can produce excellent results and do things that are virtually impossible sans OOP. With OOP, a skilled programmer can program an application in an organized manner.
It only takes one skilled programmer to properly develop an OOP framework. That framework can then be used by other programmers. The beauty of OOP lies in its ability to manage complex tasks, its flexibility, and its ability to make efficient use of hardware.
OOP is a high-level extension of the modular code concept. By building cleverly thought out, reusable modules of code, repetitive programming is avoided. Code also becomes more organized and readable.
Individual modules of code, or classes, can "inherit" each other's contents. A child class inherits the contents of a parent class. Inherited contents can be modified by adding new code or improving old code. For example, a parent class can be written to allow a PC to exchange data with a particular brand and model PLC.
A child class can also be created for a different PLC, and with the child inheriting the parent's common functionality while still providing a programmer with a way to add hardware specific details.
An object is an instance of a class, and it contains data and code that operates on that data. Multiple objects or modules are linked together to build programs.
Objects can be linked together in a tree structure by having them contain each other's memory addresses. Those addresses can then be followed from object to object. This functionality is called traversal. As the program traverses, each object is accessed and the program gathers information about that object or provides services such as settings management to that object.
For example, the traversal function can be used to build a test and validation document for the entire program by traversing a tree of objects that contain the file name of a test and validation document that describes the object and its requirements. The traversal function collects all test and validation information together in an organized fashion.
By traversing a similar tree of objects, a programmer can gather all of the pertinent information describing those objects. This information could then be used to build a new program with a similar structure on another machine, a technique called serialization.
An entire Graphical User Interface (GUI) page can be transported from one hardware platform to another by serializing an object that contains a description of the GUI page.
When to OOP
OOP does take more work up front than monolithic code, so OOP should only be applied when dictated by circumstances.
OOP systems are particularly useful when all the eventual uses and applications of a program can not be known in advance. Some OOP languages are also well-suited for situations where a software program is needed to control a variety of hardware. For example, a corporate-level software engineer is responsible for implementing an advanced control algorithm across several plants. These facilities contain a variety of software operating system and hardware platforms. OOP's inherent flexibility would allow the software engineer to create a base program that could then be easily modified for each platform.
Another situation where OOP excels is when equipment or processes need to be connected together in unforeseen ways. Each module can be written to control a particular process, and these modules can then be linked together as needed.
Building with modular connectable blocks lets one swap only the required modules out if and when a process changes. Existing blocks from old projects can also be used to program new projects quickly.
If a module is structured correctly the first time, a permanent solution is created that can be used again and again. If mistakes are found in a module or if a module is improved, it is a simple task to update the module in all of the programs in which that module exists.