3-Lib banner

The Absolute Beginners Guide to OPL

Part 5 - An organised approach


Having worked through the last 4 sessions and ended up with several screenfuls of OPL code, you'll probably appreciate that keeping it tidy and understandable is a major problem. You've only entered 30 or so lines of code so far and it looks messy already. Imagine how you'd cope (or not) when your program runs to several thousand lines! It is every programmer's aim to write tidy code that others (and himself 6 months later!) have a fighting chance of making sense of.

There are two general solutions to this problem. The first is to make sure that you put plenty of comments in your programs. We've already done this several times, annotating lines which might confuse us when looking again at the program in the future. The second technique is to split the program up into chunks. Each chunk is called a 'procedure' and has the job of performing a single function.

For example, rather than having 9 lines of (fairly messy) code that picks unique lottery numbers, why not have a single procedure (e.g. called picknum:) that we can call instead. All the code is then hidden away inside the procedure and our main loop would look much tidier.

If you're on the ball you'll stick your hand up at this point and say something along the lines of "But Steve, if I move bits of my code down into a 'procedure', how does the procedure know about the numbers%() array? And come to think of it, how does the main program know what number the procedure has picked?" Good questions, and to answer them I'll introduce a new concept.

At the top of the code, change the third line to read:

GLOBAL numbers%(6) rem Array to store picked numbers

As you might guess from the name, the GLOBAL bit tells the program that the numbers%() array is accessible from everywhere inside your program, not just in the procedure you first found it in. Thus a number-picking procedure can just as easily read the contents of the array as when the code was in the main loop itself. And conversely, of course, the LOCAL keyword tells the program that the variable that follows is only valid within the procedure it is found in:

Screen shot

The next stage is to create a home for the new procedure. Right at the bottom of your code, press Enter a few times and type the following:

PROC picknum:(number%)
rem To choose a unique ball number for position no. number%

RETURN
ENDP

Your screen should now look something like:

Screen shot

Time to fill the procedure with some code. Move the cursor up to the line which starts "picknum::" and highlight that whole section, finishing with "ENDWH" as shown below:

Screen shot

"Cut" the text and "Paste" it into the body of the procedure already created. Just to be tidy, delete all the extra leading spaces as well. The picknum: procedure should now look like this:

Screen shot

Of course, there's some serious editing to be done, as the i% variable used in the main loop isn't valid here. Instead, note that I defined a so-called 'parameter', (number%), after the procedure name. This means that when we call the procedure, we can pass into it an integer number which gets stored internally under the name 'number%'. Thus the main loop is going to pass the value of i% as the parameter and we can use it as number% inside the picknum: procedure.

Change all the references to i% in the procedure to number% instead. Note also that we're using the integer variable j% in the procedure and so we need to define it. Just after the "rem" at the start of the picknum: procedure, add:

LOCAL j%

Screen shot

That's the picknum: procedure all done, we now need to call it from somewhere. Go back up to the main loop and type the following into a blank line after the "WHILE" line:

picknum: rem Pick a unique number for the i%-th ball

Screen shot

Before we hit the moment of truth (running the program), take 5 minutes to make another procedure of all the graphics commands at the start of the program. Highlight everything from "gGREY 1" to "gGREY 1 :gFILL 300,70,1 :gGREY 0" and cut and paste it into a new procedure called "setup:". The screen shots show the new procedure in place and the call to setup: at the start of the application, so copy them if you're not too sure what goes where....

Screen shot

Screen shot

Translate and run the program and you'll find it operates in the same way as before, despite all our tinkering. Although on the surface we haven't improved the program this week, the fact that you now know something about procedures will pay dividends in the sessions to come and will ensure that the code is always as readable as possible!

By the way, if you feel you've lost the plot with these tutorials and are no longer sure whether your code matches mine, you can download the code as it should be (at the end of this session) right here, in plain text format as myfirst.txt.

In session 6, we'll tackle the basics of adding a simple menu....


Go to next lesson | Programming index