I often have customers ask me if they should be using classes in in their LabVIEW application. Many of the people asking this question are long-term LabVIEW users who are new to the concept of an object-oriented development approach and they're struggling to identify how and where classes would benefit their application. If you've ever asked this question, or if you've ever wondered where in your application you would use classes, I'll do my best to shed some light on the topic and explain some of the primary benefits...
To put it simply, classes aim to make it easier to represent collections of items within software. Consider that real-world objects have attributes (colors, weight, size, etc..), and they also have things they can do (open, close, move, etc...). As you might expect, object-oriented programming allows you to define a class of objects, all of which have attributes (known as properties) and things they can do (known as methods).
Consider some contrived examples:
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):
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:
Personally, I recently adopted the use of classes in my projects - I went back in one of my largest projects and replaced an array of clusters with an array of classes. In my experience, the use of classes forced me to write code that has clearly defined 'ownership' - in other words, my code is cleaner and more modular because I've forced myself to say, 'this function is a member of this class - therefore, it is only responsible for operating upon the data available in this object' - this benefit is often referred to as encapsulation. This helps me avoid unnecessary coupling between sections of your application that make reuse difficult later on.
The concept of a class is not unique to LabVIEW. In fact, the use of classes is probably most commonly associated with C++, which was basically C with classes added. However, most modern programming languages, including Java and C#, heavily emphasize the use of classes. Classes were introduced to LabVIEW in 8.2, but have been continuously improved and refined since then.
For examples and illustrations of object-orientation at work, check out the following:
There is a lot more to be covered and discussed on the topic of classes in LabVIEW, including by-reference implementations and Endevo's GOOP toolkit. Look for more in future entries.
A final tip: there are a number of settings and options when creating and managing classes, some of which are intended for advanced use-cases. If you're having trouble figuring out how to configure something, I highly recommend consulting the LabVIEW documentation. Also, feel free to post your questions below.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.