Guru: Procedure Driven RPG And Adopting The Pillars Of Object-Oriented Programming
February 19, 2024 Gregory Simmons
The four pillars of object-oriented programming (OOP): abstraction, encapsulation, inheritance, and polymorphism, were not created at a single point in time. They evolved gradually over several years, with contributions from various researchers and programmers. Here’s a brief overview of their evolution:
-
Abstraction:
- Alan Kay is credited with introducing the concept of abstraction in the 1960s with his work on Simula.
- Abstraction gained further traction with the development of Smalltalk in the 1970s.
-
Encapsulation:
- David Parnas, in his 1972 paper “On the Criteria To Be Used in Decomposing Systems into Modules,” laid the groundwork for encapsulation by emphasizing the importance of information hiding and data protection.
- Encapsulation became a core principle of OOP with the development of languages like C++ and Java in the 1980s and 1990s.
-
Inheritance:
- The concept of inheritance has its roots in Simula as well.
- It became widely used in object-oriented languages like C++ and Java, allowing developers to create new classes based on existing ones.
-
Polymorphism:
- Polymorphism emerged in the 1970s with languages like Lisp and Smalltalk.
- It became a key feature of OOP languages like C++ and Java, enabling different objects to respond to the same message in different ways.
Importance of OOP in Program Development
OOP offers several advantages over traditional procedural programming paradigms:
- Increased modularity: OOP allows developers to break down code into smaller, self-contained units called objects, making it easier to understand, maintain, and reuse code.
- Improved code reusability: By inheriting from existing classes, developers can reuse code and avoid writing redundant logic.
- Encapsulation: Encapsulation protects data from unauthorized access and modification, leading to more robust and secure programs.
- Enhanced maintainability: OOP code is typically easier to maintain and modify due to its modularity and encapsulation.
- Flexibility and extensibility: OOP programs are more flexible and extensible due to their ability to inherit and adapt existing code.
While RPGLE doesn’t fully embrace all tenets of object-oriented programming, it does support two of the four pillars:
Encapsulation
Encapsulation involves bundling data and the code that operates on that data within a single unit, protecting the data from external modification and promoting modularity. This is how RPGLE implements encapsulation:
- Procedures: RPGLE procedures effectively encapsulate code and data, acting as self-contained units that can be reused and managed independently.
- Prototypes: Prototypes act as blueprints for procedures, defining their structure and accessibility, further enforcing encapsulation.
Inheritance
Inheritance allows new classes (or data types in RPGLE) to inherit properties and methods from existing ones, fostering code reusability and hierarchical relationships. This is how RPGLE implements inheritance:
- Data types: RPGLE supports inheritance through sub-data types, enabling new data types to inherit characteristics (fields and subfields) from base types.
Below is an example of inheritance:
1 **FREE 2 Ctl-Opt Main(demonstrate_inheritance); 3 dcl-ds Parent Qualified Template; 4 Name char(30); 5 Age int(10); 6 end-ds; 7 dcl-ds Child Qualified; 8 base_Info likeds(Parent); 9 Grade Char(2); 10 end-ds; 11 dcl-proc demonstrate_inheritance; 12 dcl-ds ParentObj likeds(Parent); 13 dcl-ds ChildObj likeds(Child); 14 ParentObj.Name = 'John Doe'; 15 ParentObj.Age = 40; 16 ChildObj.base_Info.Name = 'Jane Smith'; 17 ChildObj.base_Info.Age = 10; 18 ChildObj.Grade = 'A'; 19 dsply ('Parent Name: ' + ParentObj.Name); 20 dsply ('Parent Age: ' + %char(ParentObj.Age)); 21 dsply ('Child Name: ' + ChildObj.base_Info.Name); 22 dsply ('Child Age: ' + %Char(ChildObj.base_Info.Age)); 23 dsply ('Child Grade: ' + ChildObj.Grade); 24 end-proc demonstrate_inheritance;
In the above example, starting at line 3 I have a simple data structure ‘Parent’ with Name and Age subfields. At line 7 I have a ‘Child’ data structure, which will have the same subfields as ‘Parent,’ but also has a ‘Grade’ subfield. In this fashion, we can say that the ‘Child’ data structure inherits its first two subfields from the ‘Parent’ data structure. As I always tell my students, ‘…but don’t take my word for it, code this up yourself and inspect it closer in debug.’
In summary, while RPGLE doesn’t offer a complete object-oriented programming experience, its support for encapsulation and inheritance enables developers to achieve some benefits of OOP, such as code reusability, modularity, and organization.
Until next time, happy coding.
Gregory Simmons is a software engineer with PC Richard & Son. He started on the IBM i platform in 1994, graduated with a degree in Computer Information Systems in 1997 and has been working on the OS/400 and IBM i platform ever since. He has been a registered instructor with the IBM Academic Initiative since 2007, and holds a COMMON Application Developer certification. When he’s not trying to figure out how to speed up legacy programs, he enjoys speaking at COMMON and other technical conferences, running, backpacking, SCUBA diving, hunting, and fishing.
RELATED STORIES
Guru: Getting Started With The Code 4 i Extension Within VS Code
Guru: Procedure Driven RPG Means Keeping Your Variables Local
Guru: Procedure Driven RPG With Linear-Main Programs
Guru: Speeding Up RPG By Reducing I/O Operations, Part 2
Guru: Speeding Up RPG By Reducing I/O Operations, Part 1
Guru: Watch Out For This Pitfall When Working With Integer Columns
An example where inheritance (data and procedures) is used in RPG can be found in the OSSILE git repository on github: https://github.com/OSSILE/OSSILE/tree/master/code_examples/rpg/interfaces
All too often, the check to determine if a procedure or subroutine is to be performed is done outside of that object. Encapsulating that check at the beginning of that object puts that rule inside the object where it should belong.
Re: “Alan Kay is credited with introducing the concept of abstraction in the 1960s with his work on Simula.”
Alan did work with Sinula but the concept of abstraction is credited to Barbara Liskov, https://en.wikipedia.org/wiki/Barbara_Liskov?wprov=sfti1#