Monday 19 November 2012

Community: LabVIEW Development Best Practices: When Should You Use LabVIEW Classes?

Community: LabVIEW Development Best Practices: When Should You Use LabVIEW Classes?:


Now, you might be thinking to yourself, "Can't I just use a cluster?"  The answer is generally 'yes.'   In fact, if you've recently begun developing an application that relies upon a large, complex cluster that you're passing around your application, it may be an excellent candidate for replacing with a class.  This is especially true if you're passing around an array of clusters that are used to represent items that your program needs to be able to communicate and interact with. Making the switch from a cluster to a class offers several benefits - I want to highlight the following (although there are many more):

  1. Inheritance - so far, I've described explained that you can define a class of objects and give it properties and methods.  What makes this even more powerful is the ability to define children of that class, which have the same properties and methods, as well as some of their own.  They may also want to override the methods or properties of their parent class.  In my list of examples, I mentioned 'vehicles.'  As you can imagine, there are many different types of vehicles, some of which have very unique properties and things that they can do.  If you consider 'pickup truck,' as an example, one of it's properties might be 'bed size,' which wouldn't make any sense when applied to a car or a motorcycle.  
  2. Dynamic dispatching - If we have an array of similar objects, they likely share methods and properties.  In LabVIEW, we create wrappers using VIs to access and modify these values.  If we want the wrapper VI for a specific child class to do something different, we can create a VI to override the parent VI automatically.  LabVIEW will automatically run the version of the VI that is appropriate for the current class.  To put it simply, LabVIEW dynamically runs the VI based upon the class of the object supplied to it - this assumes that the object is a child of the generic class.

As a software engineer, it's important to be able to recognize the potential benefits and when the use of classes may make more sense.  LabVIEW is almost always used to interface with hardware, so the I/O in your application may be the perfect place to start.  Consider these examples:

  1. Hardware Abstraction Layers - This white paper on hardware abstraction layers illustrates some excellent examples of the benefits of classes and how they can be used to mitigate the risk of hardware obsolescence.  Classes have been defined for certain subsets of functionality (ie: a generic Scope).  When a specific instrument is connected or added, it inherits the properties and methods of the parent class, but may add additional functionality (ie: an Agilent Scope could be replaced with an NI Scope, or visa versa).
  2. Devices Under Test - Consider the task of testing a large number of very similar, yet slightly different devices such as cell phones.  One production line may be responsible for testing a variety of different types of phones, but they almost certainly all have similar properties.  The code can be written using a generic cell-phone class, but the method to execute a specific operation may require slightly different commands be sent to the test executive.  This can easily be added later on without major modifications to the code through the creation of a child-class.



'via Blog this'

No comments: