If you were to create a reusable code fragment, would you use an App Class or a Function Library? We asked our LinkedIn audience, and this was the result:
Do these results surprise you? Here are some insightful comments from the poll participants:
"Application Classes... For so many reasons:
- Application Classes hold state across method calls
- You get maintenance savings from classes
- Application Packages can bundle related code
- Classes give you public, protected, and private methods
- Encapsulation and state
- Clear Namespaces"
"I have always preferred application classes over function libraries because I find the discoverability of functions to be severely lacking. They can be placed just anywhere in any field in any record. And I can never remember the format for the declare statement."
"Don't forget about the variable declarations and type checking, which can avoid lots of problems at Runtime. That is on top of a clear inventory of available methods at the top of the class."
"Hands down application class..."
"My personal problems with functions:
- Keep forgetting library records
- Can’t group code without creating a new library
- Can't use them for configuration such as IB Handlers"
"I haven't willingly created a new function library in years.
- Application classes are much neater
- Nicer to implement
- Like the OO[P] reference
- Not nested in other code"
"A bonus of using Application Classes is being able to test any of your code from a delivered component in the PIA with the Application Class Tester. Navigation: Enterprise Components > Component Configurations > Application Class Tester. One of the best development tools that Peoplesoft delivers!"
Let's do a side-by-side comparison of the differences identified in the comments:
| App Classes | Function Libraries | |
|---|---|---|
| All variables must be declared | ✓ | |
| Stateful | ✓ | |
| Testability | ✓ | |
| PeopleCode-specific container | ✓ | |
| Dynamic execution | ✓ | |
| Object-oriented concepts such as Inheritance and Composition | ✓ | |
| May be used by frameworks (IB, etc.) | ✓ | |
| Stateless | ✓ |
While the App Class column has the most checkmarks, is it the right answer for every situation?
All Variables Must Be Declared
Function libraries allow us to use variables without declaring them. An undeclared variable becomes implicitly declared as a generic data type upon first use. The problem with implicitly defined variables is that they support typos. It is really easy to mistype a variable name, which then becomes a new implicitly defined variable.
Application Classes will not compile and save unless you declare every variable. This avoids implicit typos.
At JSMpros, we teach that all variables should be defined at all times. The compilation output window, therefore, becomes a worklist. If a variable appears in the list, either declare or fix it. At the end of the day, anything in the list is a typo.
State
Application Classes are stateful, whereas Function Libraries are stateless. Both have their place. Application Classes must be instantiated prior to use. There are times when I have a code fragment that doesn't require state. Instantiating an object to perform a stateless operation seems like a waste. If the logic does not require state, then Application Classes may be overkill.
Testability
With Oracle-delivered solutions, such as the Application Class Tester, and community projects, such as PSUnit, it may seem that Application Classes are easier to test. But testability has more to do with coding practices than the container. For example, code you plan to test should avoid context-aware functions and variables. Whether you are testing App Classes or PeopleCode functions, you will write test case PeopleCode, which can be as simple as an App Designer-run App Engine.
PeopleCode-specific Container
Function Libraries are PeopleCode functions attached to events in Record Field PeopleCode. This can be challenging to manage. Having a PeopleCode-specific Container, such as an App Class makes a lot of sense. I do wish Function Libraries were stored in their own container. This doesn't impact their functionality, but it does affect lifecycle management and reuse.
Dynamic Execution
As noted in the comments, function libraries must be declared before they are invoked. We could argue that Application Classes have the same requirement in the form of import statements, but that's not exactly the case. One of the most powerful features of Application Classes is that we can instantiate objects, invoke methods, and set properties at runtime without knowing the implementing class at design time. We do this through the PeopleCode functions CreateObject, ObjectDoMethod, and ObjectSetProperty. These three functions allow for fully dynamic PeopleCode. In short, Application Classes don't need to be declared. It is this characteristic that allows PeopleSoft to build frameworks, such as Approval Workflow Engine, Event Mapping, and Integration Broker.
Object-Oriented Concepts Such as Inheritance and Composition
This is a key component to making Dynamic Execution work. We can create frameworks that code to interfaces and abstract base classes, and then use dynamic functions to instantiate full implementations at runtime.
Overall, object-oriented programming is not considered better or worse; it's just different. Poorly designed inheritance hierarchies can cause more problems than they fix. Perhaps what makes them challenging is that a solution is not static. We might create an amazing hierarchical design for the first build, but over time, that design becomes corrupted through changes. Composition is an Object-oriented concept that attempts to overcome the challenges of Inheritance.
May be used by frameworks (IB, etc.)
This feature brought to you by Dynamic Execution. Because we can code to an interface, we don't need implementation details until runtime. This makes App Classes perfect for AWE, IB, Event Mapping, PSUnit, Landing Pages, and much more!
In conclusion, each strategy has its place. Choose Function Libraries in the following situations:
- There are times when you must use Function Libraries. Signon PeopleCode is a great example.
- I also choose Function Libraries for stateless routines. If your reusable code fragment is stateless, then the overhead of App Classes may be overkill.
Choose App Classes:
- If you are building a framework, such as AWE, and want to register future event handlers, Application Classes are the right tool. You, the framework builder, would code to an interface, and later implementations provide the runtime details.
Which do you prefer? Let us know in the comments.
We teach both topics in our on-demand PeopleCode course. Already familiar with Function Libraries and want more information on Application Classes? Enroll in our on-demand course or join us for our next live virtual session!
