Creating efficient coding in your scripts using VB Script is an art. Learning how to properly code, and what practices to instill in any particular script or routine is a matter of experience, patience and practice. Sometimes, reverse-engineering someone else’s coding can often suggest ways in which to improve one’s own scripting. The very first step in creating efficient code in an InduSoft Web Studio application is to first understand exactly what the code is attempting to accomplish. This is called “Script Planning.”
Sometimes creating a design for the planned routine in order to build a scope for the script is necessary. It can have the effect of reducing complex coding tasks down to a few component parts that are easy to “bite off in small chunks,” or even to understand exactly how many project tags a particular module will consume before they are even created. This process is like any other exacting design and construction process—if you can describe or “blueprint” what you want to do before you begin any scripting, it is quite likely that you will build it properly on the first try. If you are creating code that will need to be maintained by others or by yourself even a year or more from implementation, good documentation is virtually a necessity within complex coding structures so that you and/or others can understand the logic flow that was used to construct the script or routine, and where all the scripting is located (i.e., Background Task, Screen Script, Global Procedure, etc.
Another practice that is essential to the maintainability of code and scripts is the use of “Pretty-printing”. This term, “Pretty -printing” was coined in the old COBOL programming environment of the 1970s and 1980’s, and later adopted for use in other languages in order to keep from developers from creating completely unmaintainable and unintelligible “Spaghetti Code.” “Pretty-printing” refers to formatting the scripting in a number of ways so that it becomes easily readable. InduSoft helps you adhere to this practice by putting <tab> indents in obvious places in the scripts that you write, however you can make up your own formatting rules as needed, if it helps make the coding more readable or if you have to adhere to company practices for code or document formatting.
Additionally, another aspect of “Pretty-print” is “notating” your scripts liberally with comments which explain what each of the routines do (or are supposed to do), where values within tags might come from or where they might be sent, the places that values within a logic or decision control tag branches the code, and what other modules might be involved with the logic flow of the routine (such as a Scheduler Worksheet Change-Event-Created trigger or counter tag). An example of why this practice of “Pretty-printing” is useful is shown in a simple nested loop routine (below) that quickly loads 648 initialization values into an irrigation control system. The routine is formatted so that the indented inner loop can stand apart from the parent or control loop. It can be read easily, along with the developer’s comments. Note that on the unformatted script, it is difficult to follow along and understand what the routine is doing or what the developer had in mind. If even moderately obscure tag names are used in the script instead of meaningful tag names, the undocumented script routine becomes virtually indecipherable– either in regard to readability or understanding of its purpose. The following three script examples function exactly alike. They are easy to read and understand in the one extreme (“Pretty-print”), and completely unintelligible in the other (“Spaghetti Code”). Refer to the following examples for some ideas on “Script Planning” and “Pretty-print” to better understand the advantages of using these tools.
Here are some resources that you might find useful:
1) InduSoft Built-In Scripting (Video Training Module 20):
2) VB Script Interfaces in InduSoft Web Studio (Video Training Module 21):