Flowchart Programming for Reuse
White Paper
Entivity, Inc. Ann Arbor, MI 48108
One of the most important benefits that Flowchart programming offers is the ability to create programs that can be re-used in many situations. This paper will explore the methods for creating reusable flowchart programs and the good programming practices that will make them useful. Advantages of Reuse The advantages of reusing bits of programming effort are well known. It is expensive in terms of engineering time and effort to capture a desired machine behavior in specific flowchart code. Many machines have similar behaviors and so it is very desirable to make the effort once and use the results of the effort in many places. The discrete benefits of this practice are: • Less engineering time is used reproducing work that has already been done. The time saved is available for improving the product. • If a particular function is implemented only once and then used many times, then it can be thoroughly tested and debugged, resulting in higher quality. • The knowledge embodied in particular implementations can be passed along to many people if it is embodied in a common form. Reuse does not happen automatically. It must be intentional. This paper outlines some rules Entivity engineers have found
Flowchart Programming for Reuse
helpful when creating flowchart programs in order to promote reuse. Modularize - Use Subprograms The most important rule in creating reusable programs is to break things up into component parts. Small, simple programs (in any language) are much easier to create, understand, document, and maintain than large complex programs. By combining small programs, complex behaviors can be programmed without requiring complex programs. In the Entivity VLC flowchart programming environment, this means that the preferred programming style is to use subprograms. The subprogram capability of the flowchart language provides you with a powerful tool for encapsulating simple concepts and then using them in many places. Flowchart subprograms are written in the same language as main programs. Two characteristics of subprograms, however, make them good tools for creating reusable code: • Subprograms can have parameters. Parameters allow the programmer to create a behavior, and then assign that behavior to different I/O points or variables (the next section discusses this topic in more detail). • Subprograms are self-contained. Simply copying the *.FLO file copies
2
everything needed for a particular subprogram to run. The file can then be directly attached to a VLC project and used as-is. NOTE: Subprograms are completely selfcontained only if they use only parameters and no global variables (this topic is also discussed below.) Use Parameters Parameters are how subprograms and the higher-level programs that call them pass information back and forth. Within the subprogram, parameters are used wherever a tag name is needed. The calling program supplies values that get substituted in place of the parameters each time the subprogram is called. In this way, the same subprogram can be used in many places to do many different things by simply supplying it with different parameters. Use Only Parameters Parameters are portable. This fact makes them the only way to go when creating subprograms for reuse. A Global Variable is a tag name that is defined within the VLC’s tag editing system. They are uses to represent the inputs, outputs, and internal data for a VLC project. However, using even a single global variable within a subprogram, destroys its portability, and hence its reusability.
Flowchart Programming for Reuse
Global variables are defined in the tag editor. There is only one global variable by a particular name within each VLC project. If a global variable is used in a subprogram, two bad things happen: The global variable must be defined in order for the subprogram to compile successfully. This means that the subprogram is no longer self-contained. A person wishing to reuse the subprogram must first discover what global variable is required and then define it in a separate step. There is only one copy of the global variable in the entire project. This means that if the subprogram is used in more than one place, all uses of the subprogram make reference to the same data. The different uses of the subprogram are not independent and can’t be used to do different things. Therefore, when creating a subprogram for reuse, strictly limit the tag names used in the subprogram to only parameters. Keep it Simple The real key to building subprograms for reuse is simplicity. There is a large difference in practice between building a subprogram that can be reused and building a subprogram that will be reused. When programmers are building their projects, they want to reuse pieces that fit nicely into their needs. The more complex a subprogram is, the less likely it is to fit their
3
needs exactly and the less likely it is to actually be reused. When faced with a choice between force-fitting an existing subprogram and writing a new subprogram, most programmers will choose to write their own. This, of course, defeats the whole purpose of creating reusable subprograms. Remember to keep the programs very simple when creating subprograms Use Subprograms within Subprograms As complex functions are broken down into simple pieces, it is usually true that some functions should be built of other functions. Package the simpler functions into subprograms and use them to build up more interesting subprograms. Doing this maintains the portability and flexibility of the reusable subprograms while taking advantage of reuse directly. Parameters can be (and should often be) passed on to lower level subprograms. Remember – no global variables in subprograms. Don’t Try to Duplicate RLL It is generally good programming advice for flowchart programmers to not attempt to duplicate their RLL programs in flowchart. The RLL language starts from a completely different model than the flowchart language. Trying to directly translate RLL programs into flowchart programs frequently ends with a tangled mess. It may work correctly,
but it will be so complicated that nobody will be able to maintain it. Instead of trying to translate RLL, start the flowchart project from an understanding of the machine you are trying to control. Divide the machine behavior into separate parts and write a flowchart to control each part. Use the flowchart language to its best effect by programming directly the behavior it is intended to accomplish. Reuse existing subroutines frequently in order to reduce the programming effort. Create Standard Rules A Rule is an agreement between a user of a subprogram and the subprogram itself about how a particular function should be done. These agreements should be made broadly and then applied to as many subprograms as possible. For example, make it a rule that all subprograms must have a parameter that they use to tell their calling program what the result of the call was. If the rule is that this parameter must always be present and must always be called “Result”, then other programmers who want to reuse the subprogram will find it that much more familiar and understandable. Document, Document, Document If a programmer is not able to tell at a glance what a subprogram does and how to use it, then it will not be used. When preparing a
Flowchart Programming for Reuse
4
subprogram for reuse, place large comment blocks at the top that give at least the following information: • • • • What is the subprogram used for? How does the subprogram work? What is each parameter used for? What is the intended use of this subprogram? • Does this subprogram require other subprograms? If so, which ones. Good documentation makes the difference between a bit of code that will get used and one that won’t. Make sure that the documentation with each subprogram is standardized and thorough.
Conclusion Reusable subprograms are one of the reasons that programming in the flowchart language is much more productive than programming in other languages. Using careful design practices and diligently documenting the results can achieve substantial time savings in the development of PC-based control systems using the VLC.
Flowchart Programming for Reuse
5