A feature I don’t see used very often in WX is internal procedures.
Let’s take a minute to look at all the levels that procedure can be declare in WX.
Global procedures are global to the entire application and are declared as part of a global procedure set.
Local Procedures are declared at the window/page level and are local to that window or page. You can also create local procedures for control templates.
Internal Procedures are declared in code are only available within that code. Once of the nice things about internal procedures that have access to all of the variables etc. that are in the same code as they are declared. The folks that came from Clarion will recognized these as being very similar to Routines.
Perhaps the reason I don’t see Internal Procedures used very often is because we just don’t have to write that much code in WX! But every once in a while we actual have to write a few lines of code, and when we do it is important to know all the tools that are in our toolbox, and Internal Procedures is one of those tools.
So how and why would you use internal procedures? Here is a example from my coding session this AM. We have a process that is connecting to a server downloading and uploading files on a timer basis. The connection to the server is established at the beginning of the code, however there are several spots in the code were we exit, if we run into communication issues, etc. In each of these exit points we need to disconnect from the server.
The code to disconnect is really simple, it is only 3 lines of code.
IF myFTP > 0 THEN FTPDisconnect(myFTP) END
You might be tempted to copy those 3 lines of code into all the exit points of the code. Anyone that has worked with me for long at all, knows that I am a bit obsessive about code refactoring and reuse. Anytime I see the same line of code in more than one place, I am looking for a way to refactor it. It is all part of my core belief that every line of code I write is one more opportunity for me to mess up something, so less code = less opportunity to screw up!!!
Copy those 3 lines in 3 or 4 places and life is good and you move on. A year from now you discover you get a request to write a log entry every time to close the FTP connection. Well chances are you will miss one of the places, or have some other unintended consequence of editing code in 3 or 4 places. If it is in an Internal Procedure, you have one place to change the code and you are done.
Here is a snippet of the code we are talking about showing 3 of the exit points. The Disconnect is my Internal Procedure.
To declare an internal procedure all you do is type the keywords, Internal Procedure and hit enter. You will notice that it includes an END statement, so signify the end of the Internal Procedure, since you could have more than one in your code. And they don’t have to be at the beginning or end of you code, they could even be in the middle of your code (for instance where you first issue the code). But for the love of Uncle Pete, don’t do that!!! Put them at the beginning or the end so there is some organization.
Here is how that Internal Procedure looks in my code. The bordering around it is done by the IDE to help the Internal Procedure stand out.
Also notice that I am using the variable myFTP, which was declared in this code. As mentioned above the internal procedure as access to everything that would be available if the code were written directly into the rest of the code.
If I had created a Local Procedure, I would have needed to pass that value as a parameter. Not to mention that a Local Procedure would feel like a bit of overkill for that little bit of code. Just don’t let the use of local data get out of hand, if you have several 100 lines of code and a bunch of variables, understanding what value the internal procedure has when it is called might be hard to follow for the guy that comes behind you. So if it get’s overly complex, I still recommend using parameters, which internal procedures allow the same as any other procedure.
So add Internal Procedures to your toolbox and go out there and eliminate some duplicated code.