System i modularization -- part 2

Modularization is key to reaping the benefits of ILE. It is the major factor in cutting development time and increasing programmer productivity – the more effective the modularization, the more productive the programming staff. But it's important to arm yourself with a reliable method when modularizing your apps. In part 1,, we discussed the first few tips. Let's now get into the next couple, which will include some code examples.

More Information

Keep procedure interfaces simple and pithy

The biggest contributor to a complicated interface (i.e., a long complicated parameter list) is usually a complicated procedure with too many options and permutations. Keep it simple. Don't try to do too much with one procedure. Use multiple procedures instead with each procedure having one or two specific purposes.

For example, don't create a single procedure ConvertCase that converts to uppercase and to lowercase depending on the value of one of the parameters. Instead, create two separate procedures, one to convert strictly to uppercase and the other to convert strictly to lowercase. Not only does this simplify the procedure interface, it also makes the calling programs more readable and self-documenting (instead of seeing ConvertCase, you see either ToUpper or ToLower).

In situations where you have to have a long parameter list, you should consider "packaging" related parameters into a single data structure. This does two things:

1. It shortens the parameter list and makes the procedure interface less complicated.

2. Calls to the procedure will be faster because of fewer parameters.

To simplify using a procedure with a data structure parameter, set up a "template" data structure in an include file (i.e., a /Copy source member), preferably the same one in which you have put the procedure prototype. Then, use the LIKEDS keyword in your program to make a copy of the template data structure for your program to use. This way, the programmer calling the procedure will not have to define the data structure manually. See the sidebar "How to use the LIKEDS keyword" (below) for more information on using the LIKEDS keyword.

It's never too late to modularize

If you see an opportunity to modularize in an existing app, seize it. It is never too late to strip some code out of an existing program and make a separate procedure with it, especially if the code is reusable. Doing so can improve productivity drastically.

Let's look to the April 2007 issue of Inside Version 5 for a perfect example of this. In "Embedded SQL programming without SQL/400 – A working example" (see sqlcli.txt and #sqlcli.txt), we showed you a fairly long program that uses the SQL CLI APIs to achieve embedded SQL. Most of the code is for SQL environment setup and cleanup and is the exact same process any program would have to go through before actually running an SQL statement. This, then, presents a perfect opportunity to modularize after the fact.

First we strip out the setup code and create a procedure called SQLSetup. Then we strip out the cleanup code and put it in a procedure called SQLCleanup. These two procedures, along with the ChkSQLRC procedure, are put into a separate module called SQLCLIXTRA (see sqlclix.txt). To support this module, we either need to add to the previous include file (i.e., #sqlcli.txt) or create a separate one (see #sqlclix.txt).

The same working example program shown last month is now considerably shorter and much easier to read (see sqlcli2.txt). Plus, if coded from scratch, it would have taken much less time.

Keep posted for the rest of the tips next time.

This was first published in March 2007

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.