The previous TechTip of this series introduced the concept of procedures, along with a simple example. This one will explain when you should (and shouldn't) create a procedure and reveal some pitfalls you need to avoid.
Let's start with a quick recap: procedures are "subroutines with parameters" that can exist outside the program that is calling them. For that to happen, the calling program needs to know how to use the procedure. This critical piece of information is supplied by a prototype definition (similar to the *Entry PList of a program). It's a good practice to use a separate source member to store the prototype definitions because they will be used in more than one program.
The next big question is when to create a procedure. How should an OPM program's subroutines be transformed into procedures? Should the procedures be exact copies of the subroutines?
Well, to answer these questions (and many others the readers have been asking me), let's try to rationalize the concept of a procedure: it's supposed to be reusable and work together with other procedures, like building blocks, to create modular solutions to the challenges today's RPG programmers face. This implies that the procedures should be small and simple so that they can be used in as many situations as possible. They should also be compatible with other procedures; in other words, they should be independent and, within reason, self-sufficient.
Our example since the first TechTip of the series (and slightly modified in the previous TechTip) has been the inventory programs scenario depicted in Figure 1:
Figure 1: This is the evolved OPM scenario.
Here we have two main programs, A and B, which manage the inventory master file in different ways. While A imports inventory items from a cargo manifest CSV file, B handles the user inventory management via screen interaction. They both call some external programs, identified here as BR1, BR2…BRx. These last programs are the perfect candidates for procedures, but here is where you need to be careful. If the idea is to build something that you can reuse, you need to keep it as simple as possible.
Figure 2: BR2 is transformed into a module with several procedures.
Figure 2 depicts a scenario in which BR2 is transformed into procedures. Imagine that program BR2 has a subroutine, among others, that checks whether an item already exists in stock, and it also updates the inventory. When BR2 was turned into a module, it was split into several procedures. As I explained in the previous TechTip, a module can have one or more procedures. BR2's subroutine "Check if item exists in inventory and update it" became procedures "Check if item exists in inventory" and "Update inventory" because there will be some situations in which only one of these two operations will need to be performed; somewhere in program A or B you might have a piece of code that checks if an item exists in inventory but doesn't update it or vice versa.
Procedures should also be compatible with one another: if you have several procedures that might be used together, they should share some common parameters or some sort of key to be able to access the data they'll be performing operations on. Let's say that program B needs to remove an item from inventory. It will first call the "check if item exists in inventory" procedure that spawned from BR2 and then it will call the "update inventory" procedure. These two procedures might have existed as a subroutine in BR2, but when they were transformed into procedures they became independent from one another. However, they need to share the Item ID as a key so that they can work together.
Finally, procedures should be, within reason, self-sufficient. In other words, a procedure should be able to perform whatever operation it needs to perform without calling other procedures. Of course, there will be situations in which this is simply not possible (hence the "within reason" I used before), but you should try to minimize them. How? Well, having procedures that perform a simple and well-delimited operation, like the two examples presented before, is a way. This is important because when you are compiling your code you might run into a "the chicken or the egg" situation: you need to recompile module X (which I've explained how to do in the third TechTip of this series), but this module has a procedure that uses another procedure or function from module Z. If module Z also uses something module X uses, you won't be able to recompile either of them! Also keep in mind the aforementioned need for simplicity and reusability; if your procedures are small and self-contained, they will be easier to understand and use over and over again instead of writing new (or duplicated) code.
A final note: you may or may not have noticed that the "Update inventory" procedure in Figure 2 is not the same color as the other procedures. If fact, it's the same color as the "Database Operations" subroutines in Figure 1. This was not by chance: I'll give you a detailed explanation later, but procedure "Update inventory" shouldn't be in module BR_INV because it's a database operation procedure and BR_INV is a business rules module. Updating the inventory is definitely not a business rule; it depends on one or more business rules, but it's not a business rule, so it shouldn't be there. For the moment, let's keep it simple. I'll come back to this detail later on this series and explain where it belongs and why in more depth.
The next installment of the RPG Academy Series will be about functions! I hope you're enjoying this series and, as usual, if you have any questions, suggestions, or doubts feel free to contact me!
LATEST COMMENTS
MC Press Online