|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON General Java
NetRexx Programming for the JVM
By: Rick Hightower
Digg This!
What This Series Is About
In the first two articles in this series Java was presented as the system language, and the higher-level language was presented as the glue language. This allows you to define frameworks, libraries and components in Java and glue them together to make applications. This was described in detail in the February JDJ (Vol. 5, issue 2). Though it wasn't on the first suggested list, NetRexx received a lot of votes. Those votes count, as evidenced by the fact that NetRexx is covered here - the last time I checked it was running neck and neck with JPython.
NetRexx 101
"NetRexx is a human-oriented programming language that makes writing and using Java classes quicker and easier than writing in Java," from www2.hursley.ibm.com/netrexx/. NetRexx is a REXX variant; REXX is a popular scripting language for IBM operating systems like OS/2 and increasingly for other platforms as well. NetRexx is a lot like Java, although, unlike JPython, not more dynamic than Java. Instead, it's an easier syntax that makes system programming easier. NetRexx mixes nicely with Java - for example, it can subclass Java classes and implement interfaces. Also unlike JPython, which augments Java, NetRexx takes Java on head to head as an easy-to-use systems language while maintaining some of the features that made REXX programming popular. At first this may seem to put NetRexx at a disadvantage. However, NetRexx does a nice job of being a better REXX than REXX and a better Java than Java. What does human-oriented mean? I was originally fooled into believing that NetRexx was a scripting language, like its parent. It isn't. It's described as making things easier on the programmer and harder on the compiler. You have the option of running NetRexx in a mode that's more like a scripting language or more like a systems language. This option manifests itself in the NetRexx syntax via the option keyword (similar to the option keyword in Visual Basic). NetRexx advocates claim NetRexx is easier to learn than Java. I tend to agree, with some reservations. If you're a novice programmer, NetRexx is easier to learn than Java. Also, if you come from a Python, JavaScript, Visual Basic or Delphi background, then NetRexx would be easier to learn than Java. However, if you're from a C++ background, Java is probably easier to learn. Of course, if you're from a REXX or ObjectRexx background...well, you get the picture. I found NetRexx extremely easy to learn and use. And the code I created was easier to read than the equivalent Java code. NetRexx has every language feature that Java has, plus some additional features that make JavaBean development easier.
Rosetta Stone
Listing 1 is a sample class in NetRexx and Listing 2 is the equivalent class in Java. Use of the NetRexx class is the same as for the Java class. Let's break this down and compare it to the Java equivalent. To declare private instance variables in Java you'd do this:
private String firstName, lastName; In NetRexx you'd do this:
Properties Private To create a bean property in Java you'd do this:
private Employee manager;
public Employee getManager(){
public void setManager(Employee manager){ In NetRexx you'd do this:
Properties indirect As you can see, NetRexx is much less verbose. The handling of properties reminds me of Delphi. Instance variables and class variables are called properties in NetRexx. Note two things: (1) the use of the indirect keyword with properties, and (2) the call to ron.getManager(). The indirect keyword is a way to declare private variables that can be accessed outside of the class. In other words, the indirect keyword is shorthand for Java properties. There are four forms of visibility for instance variables in NetRexx: public, indirect, inheritable and private. The NetRexx public and private visibilities are just like their Java counterparts. The inheritable visibility is like Java's protected visibility, and the indirect provides bean properties. The indirect automatically provides assessor methods via the JavaBean "design patterns" for bean properties. If you add your own getter and setter methods to the class, they override the default. Actually, if you override your own, NetRexx won't generate the default assessor methods for that property. Compare the listing for the NetRexx Employee class to the listing for the Java Employee class. The former class is shorter and in my opinion more readable. Can you imagine a class with five to 10 bean properties? The NetRexx class is significantly shorter. NetRexx also has support for indexed properties. It takes up to four Java methods to do what one indexed property declaration does! If you use properties a lot, NetRexx is an easy sell. To create an instance of an Employee and print it to the screen, you'd do the following: say Employee() The equivalent Java statement would be: System.out.println(new Employee()); Next we create two instances of employee called Joe and Ron and print them to the console. We print Joe, who is Ron's manager, by invoking the getManager method of Ron. First in NetRexx, then in Java:
NetRexx:
Java: The syntax of NetRexx is close to Java (but actually closer to Python). One feature NetRexx has is default values. As mentioned in the March article on JPython, this feature can save some coding effort, not to mention some headaches. Have you ever had several different versions of the same method? And you just wanted to have different default values? Every default value is another overloaded method, which can get messy. I like that NetRexx has default values.
Rosetta Stone GUI
In the JPython article we covered the development of the GUI in the interactive interpreter, which is great for prototyping. This isn't currently possible with NetRexx, but Mike Cowlishaw, creator of NetRexx, is adding an interactive interpreter to NetRexx as the next feature. The EmployeeForm is simple: it has two text fields and an OK button. The first text field is used for the name. The user enters the first and last names in the first text field. When the user hits the OK button, the EmployeeForm handleOkay method parses the text for the first and last name. Listings 3 and 4 show the NetRexx and Java versions of EmployeeForm. The keywords are similar between the Java and NetRexx versions. For example, they both use import-to-import classes. A couple of key differences will be highlighted. NetRexx has inner classes, called minor classes in NetRexx-speak. However, it doesn't have the concept of anonymous inner classes - I like anonymous classes for handling methods. Thus I had the EmployeeForm implement the ActionListener in the NetRexx version. However, in the Java version I used an inner class.
Thus the NetRexx event handling looks like this:
class EmployeeForm extends JFrame implements ActionListener
method actionPerformed(event=ActionEvent) While the Java version looks like this:
public EmployeeForm(){ Of the two approaches I prefer Java because I don't like to clutter the interface of my class with unnecessary methods (like actionPerformed). I could have declared an inner (minor) class in the NetRexx version and then used the minor class as the event handler, but I honestly don't think I'd bother. I like anonymous inner classes and am quite spoiled using them. The Java and NetRexx versions of the EmployeeForm parse the string from the name text field in entirely different ways. The latter uses the REXX object, which is a cross between a string, a collection, an associative array (like Perl) and a generic data type. I'm not sure if I love the REXX object or hate it, but it sure makes parsing strings easy. Here's the NetRexx version of parsing the name.
_name = Rexx(this.name.getText()) Compare the ease of the above approach to the Java version of the same routine.
name = this.name.getText(); Of the two, I prefer the NetRexx approach - the REXX object is growing on me.
Rosetta Stone Statistics
Implementing getRange
First try implementing getRange (see Listing 5), which uses the java.util.Collections class to get minimum and maximum value. When the getRange function is done it returns the min, max and the range in an ArrayList instance. Thus, to declare the min, max and ranges variable in Java, do this:
double min, max; In NetRexx, do this:
min=double Note that in NetRexx declaring the variable before assigning it is an optional step, as we'll show later. To get the value min and max values from the min and max methods of the java.util.Collections class, do this in Java:
min = ((Double)Collections.min(nums)).doubleValue(); In NetRexx you'd do this:
min = ( Double Collections.min(nums)).doubleValue() This is fairly similar to the Java way. (The Java versions of Listings 5-8 can be found on the JDJ Web site in the March 2000 [Vol. 5, issue 3] digital edition, www.JavaDevelopersJournal.com.)
Implementing getMean
This example shows sample usage of:
method getMean(nums=ArrayList, sample=boolean 1) public static returns double This method is passed an ArrayList (nums) and a boolean flag (sample). Notice that the visibility and modifiers are at the end. Also notice that the sample argument has a default value. Thus, if the second argument isn't specified when calling this method, the value of 1 (true) will be implied. The equivalent Java code would look like this:
public static double getMean (ArrayList nums){
public static double getMean (ArrayList nums, boolean sample){ In Java you'd have to declare two methods. As you can see, if you had a method with four default values, the equivalent Java code could get quite verbose. The getMean method defines three local variables called average, sum and value, which hold the sum, average and current iteration value.
value=Double To iterate through the ArrayList we need to get the iterator. The method gets an iterator from the ArrayList argument passed to the method as follows: iterator = nums.iterator() Notice that we didn't declare the iterator first. Now we can use the iterator to iterate through the numbers in the nums ArrayList while adding the numbers together.
loop while iterator.hasNext() To cast the value from the iterator.next from an Object into a java.lang.Double we do this:
value = Double iterator.next() The equivalent Java code would look something like this: value = (Double) iterator.next(); Next, the getMean method checks to see if this is a sample mean. If it is, the method figures the average by dividing the sum by the number of items in nums less one, or else it divides the sum by the number of items in the nums ArrayList.
-- Check to see if this is a sample mean
Last, the method returns the average:
return average
The foregoing is not much different from what you'd do in Java. It's actually very similar. In fact, you could probably read and understand the NetRexx code without much coaching.
Implementing getMode
This example shows example usage of:
Implementing getMedian
Listing 8 shows example usage of:
Comparing Java to NetRexx
Notice that in the NetRexx version the public static methods don't have to be inside a class declaration. If static methods are defined outside a class definition, the methods are treated as though they're part of a class definition corresponding to the name of the source file. Notice also that the RunReport isn't in a static main method. If code is in a file outside a method declaration, the code is assumed to be in the main method. These features make writing scripts easier and less verbose.
Vote for Your Favorite
Scorecard
AUTHOR'S SCORECARD FOR NETREXX
Related Links
Random Thoughts
If NetRexx is geared for nonprogrammers or new Java programmers coming from a fancy 4GL with a fancy IDE, then its install is really lacking. There's no real install program - just a zip file with some instructions. The NetRexx user base is strong and involved. While there are many NetRexx tutorials, most of them aren't linked to or from the main NetRexx site. And strangely enough, there's no real strong tutorial on the main NetRexx Web site, just a getting-started page and the language reference (which is very well written). This is a big gap and a real problem. There's also an IBM Redbook on NetRexx, but it seems pretty dated. If you want to use the latest NetRexx features, you need to refer to the language reference. It would be nice if someone at IBM could loan Mike Cowlishaw a resource or two to come up with a good language tutorial. (A book on NetRexx written by Mike Cowlishaw probably fits the gap nicely.) In short, you're going to have to hunt and peck at many sites to get a good feel for NetRexx programming. I did, and it wasn't too difficult, but I'm lazy and would like to do one-stop shopping for information like this. Also, this may throw someone who's new to NetRexx - I assume the goal is to increase the NetRexx user base, not deter new people. If you don't make it easy, they'll go somewhere else. I like NetRexx a lot in its current incarnation but it's not different enough from Java. I think it needs to expand on cool features like indirect properties. I want to see more. I'd like to see language support for events so that one line of NetRexx code replaces the need to declare a listener interface, event class and add/remove listener methods. The event syntax could be similar to the way Visual Basic (5.0 and above) defines events for ActiveX components. The fact that NetRexx is a new beast and doesn't have to be backward compatible with another language is good - unlike JPython, which endeavors to be compatible with Python. This gives NetRexx a real advantage. (Python has its advantages as well - numerous advantages. Read the March JDJ [Vol. 5, issue 3] to learn more about JPython.) NetRexx needs an interpreter but Mike is taking care of that. In addition, NetRexx also needs additional language syntax for dealing with collections, iterators and so forth, á la JPython. Last, if IBM isn't going to dedicate more resources to NetRexx, then they should really open-source it. NetRexx has a strong user base that's involved and dedicated. It would do well in the open-source world. I'd like to see IBM open-source and add more resources.
Parting Shots
However, unlike JPython, which has to be compatible with Python, NetRexx doesn't have to be compatible with ObjectRexx. This gives NetRexx a real advantage as it can add new language features that make sense in the Java Virtual Machine (e.g., indirect properties). As you may know, we had an informal poll and JPython and NetRexx were the top two languages for the JVM. I also solicited feedback from developers who had used both JPython and NetRexx. The feedback I got was this: if you're from a REXX background, use NetRexx. If you're just looking for a good scripting language and don't know REXX, consider using JPython instead. NetRexx has a lot of promise, and I look forward to its evolving into an even better and more productive tool. If VisualAge supported NetRexx, I think a lot more people would use it in place of Java - including maybe me.
. . .
In the next article we'll cover the Bean Scripting Framework from IBM. The BSF promises to unify integration of scripting languages into applications. It also promises, in the future, to provide universal debugging support for most programming languages for the JVM. LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING JAVA NEWS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||