Which text language works best?
You can employ C, C++, Delphi, Java, or Visual Basic in your test-automation applications.
Jeff Laney, National Instruments, Austin, TX -- Test & Measurement World, 4/15/2002
Even though graphical development tools (Ref. 1) have become prevalent in the test industry, you shouldn't abandon your skills as a text programmer. At times, you may find it's easier to write a program in a text language you already know than to take time to learn a special graphical tool. And you may want to consider expending the effort to learn a new text language: Most likely, you can use the language to write programs for a variety of applications, not just for test setups.
If you want to use a text-based language, look for one with a future—one that will continue to find use in a variety of applications. Five such languages can handle test tasks: C, C++, Java, Delphi (Pascal), and Visual Basic. The new .NET languages, such as C# (pronounced C-sharp), offer promise, but they haven't yet entered the mainstream (see What the future holds ).
C remains a powerhouse
|
Key benefits of C include the number of libraries available and the number of developers in the world who know the language. Almost every hardware company offers drivers with C interfaces, and you can find a C routine to do almost anything, including playing MP3s if you want some background music for your test application. Many engineers take some C programming classes in college, and others learn it on the job to become more efficient or to advance their careers.
Of course, not everyone should revert to programming in C—it does have some pitfalls. Its lack of object-oriented features, while an advantage for short programs, can make it unsuitable for developing extremely complex applications. Furthermore, you can't expect new features to be added to it.
C++: The object of desireIn contrast, new features will be added to C++. C++ is a superset of C that takes the best of C's structured-programming features and extends it to embody OOP. C++ is the most advanced development language you can use today; it lets you develop complex applications while maintaining low-level access to hardware resources on every available computer platform.
The OOP of C++ forgoes the traditional approach of code working on data. Instead, OOP encapsulates state and behavior information into individual functional objects. For instance, in a test application, an object may represent an entire instrument, with properties and methods associated with the instrument's functionality, or an object may represent an aspect of that instrument, such as the data the instrument is acquiring. Each object can communicate with and respond to other objects, which you can piece together to create applications with complex interactions, and you can reuse your code with minimal rewrite.
C++ isn't as fast as C, but by no means is it slow. With each new release, C++ compilers are shortening the execution time of run-time code. If you're employing new computers with processor speeds pushing 2 GHz, you'll find the difference in execution speeds between C and C++ is negligible. The more important time factor is development time.
Object technology is great for dealing with complex application interactions, but it requires you to do a lot of planning before you can write a successful application. Many C++ environments try to make up for the language's complexity by providing wizards and boilerplate code to simplify development. With the openness and extensibility of the C++ language, you run the risk of making many mistakes and making your code unreadable. As an example, you could overload the "=" operator—that is, give it different meanings depending on context—thereby making it difficult for another person to understand the code.
In addition, not many true C++ class libraries exist. Most companies claim that they support C++, but they do so with a C library that can also run in C++. You do not get any advantages of the object-oriented model when using these libraries, and usually, you'll need to do some extra work to wrap these libraries so they behave as objects within your design pattern.
Java becomes the hot languageJava (www.sun.com/java) has become the most popular language for the Internet and for doing any kind of Web commerce, because you can write a Java application (Listing 1) once, and it will run on almost all PCs and workstations. Java, like C++, is an object-oriented language in which you build applications by combining different objects that interact with each other to create the desired output. Java not only extends OOP to the Web, but it solves many of the problems with C++. It eliminates some of the complexities of C++, making it easier for you to manage memory and work with pointers, and it improves error handling.
Java does have some drawbacks, though. Because Java is an interpreted language that gets just-in-time compiled, some operations can be noticeably slower than they would be with fully compiled code, even on the fastest processors. Also, Java is touted as being hardware independent—without a strong connection to any specific hardware—but the test-and-measurement industry depends on making physical measurements with specific pieces of hardware. There are almost no Java libraries for interacting with hardware and no clear way to interact with standard libraries such as C DLLs. To get around this limitation, you could employ a separate interface that exposes the hardware through some transfer mechanism. For example, you could set up a TCP/IP or UDP (User Datagram Protocol) connection that would buffer Java from the actual hardware, and you could write a client for the networking protocol in Java.
Pascal for todayDelphi (www.borland.com/delphi) mixes OOP with an easy-to-use development environment that makes everything possible and some things easy. To create Delphi, Borland took the standard Pascal and left it open so you can tweak it and do low-level operations, as you can with C or C++. Then, Borland created a development environment that simplifies most common programming tasks.
Delphi includes a set of programming tools that work with all platforms and standards, so you can develop and integrate your application for Microsoft, Sun, and Linux platforms. Delphi has its own component package called visual component library (VCL), which is both good and bad. It's good because the VCL is optimized for the compiler; it's bad because there is not a lot of support for the VCL. Delphi also works with ActiveX controls, which is a plus because most companies do have some sort of ActiveX libraries available. Of course, because Delphi is built upon Pascal—which is not the most popular language—you'll find a smaller community of Delphi developers than you'll find with C and C++.
More than BasicVisual Basic (msdn.microsoft.com/vbasic ) gets its roots from the old Basic language, but other than retaining a simple syntax it leaves the original far behind. Visual Basic (Listing 2) is essentially a development environment that makes it easy to develop common Windows applications. Visual Basic handles memory management, event programming, and Web-page creation. Microsoft has even gone as far as trying to make data type unnecessary, by having a variant type that can be used as any data type. (It's best to avoid that variant, though, because it can make errors difficult to find at compile time and because it can cause problems during interactions with non-Visual Basic code. In fact, the new .NET version of Visual Basic does not permit such variants.) The ease of development of the environment has enabled poor programmers to successfully hack together common applications.
Microsoft, of course, gets the lion's share of attention in nearly any market it participates in, so it's not surprising that whole communities have been built around using Visual Basic as the native environment for developing COM/ActiveX controls. You will likely find at least two suppliers of controls for anything you want to do, from instrument connectivity to voice recognition.
There are some drawbacks to this ease of development. Microsoft hides much of what is going on underneath. The abstraction may make something easy to do at a very high level, but if you need some granular control, you might be out of luck. Also, Visual Basic is not truly object oriented. It's a close approximation that takes many of the concepts from OOP and integrates them into the environment.
Visual Basic is limited to development for Windows; the language offers no hope of platform independence. Visual Basic is based upon an interpreted language, and therefore has a stigma for being slow. But with processors becoming so fast, the difference between Visual Basic and C/C++ is unnoticeable in most cases.
What's best for you?Your selection of a language or programming environment should be based on two factors: which language you already know and which is the best language for the project.
Even if you are working on a small project, if you already know how to develop in C++, then use C++. You can shorten the development time for a simple project by not planning your use of objects as you would on a larger project. Using the powerful C++ makes more sense than taking the time to learn a simpler language. Stick with the language you know if it can do the job.
If you do not have a preference or extensive knowledge in one language over the other, then you should choose based on what you are trying to accomplish. If you need to do a Web project, you should choose Java to take advantage of all of the Java-enabled servers in the world. Ultimately, choose whatever tool—whether it be a language, a spreadsheet, or a complete application—that makes your project the most successful.
| Author Information |
| Jeff Laney is Measurement Studio development engineer at National Instruments, Austin, TX. |
| References |
|
|

















