|
|||||||
|
|
![]() |
|
|
Tech Insight: Making the Move from RPG to Java by Douglas Smith Have you ever tried to learn how to program in Java? Did you succeed? Or did you start but give up, overwhelmed by the complexity of the Java language API or the lack of features we take for granted when programming in RPG? The journey is difficult, and one few midrange RPG or COBOL programmers complete. As one who has made the transition from RPG to Java, I would like to point out some common hurdles and offer advice on making the experience smoother and more enjoyable. The barriers to learning fall into two broad categories: emotional and intellectual. Fortunately, both sets of issues have solutions, if a person is motivated to make the effort. The emotional hurdle is the most important and the hardest to overcome. When management asks a senior programmer/analyst to take a course in Java for a new project, they are saying, "We want you to step outside your comfort zone, where you feel competent, productive, and secure. We want you to temporarily become an incompetent, unproductive, and insecure Java programmer." This is asking too much for most people. No one likes to feel inadequate. If you only know one major language--and CL doesn't count!--you can easily feel threatened. The OS/400 programming community is largely comprised of journeyman developers. We know how to code, but we also know how to design business applications, to integrate packages with custom code, and to work closely with users. We have been doing our job well for more than 20 years. Why should we change now? What is needed is a strong motivational impulse to master Java. Absent this motivation, failure to learn is virtually guaranteed. My motive was built upon the classic fear that the AS/400 might die as a platform for new development but that, even if it didn't, I wanted to be one of the first AS/400 programmers to master Java. Why Is Java Hard? The intellectual challenge of learning Java can be intimidating for someone without experience in an object-oriented language, like C++ or Smalltalk. Some advise using an integrated development environment (IDE) tool, like VisualAge for Java or JBuilder, to hide the tawdry details. I recommend the opposite approach. Use a good editor (I like TextPad) for at least one year before using an IDE. The basic idea in programming Java is simple: Define a class, create an object instance of the class, and ask the object to do something. The biggest problem is the sheer size of Java. The Java language is quite small. It has 50 reserved keywords and about 40 operator symbols, half of which are commonly used in business applications. In this regard, it is similar to RPG, which has about 120 op codes. The problem is the Java API, a set of packages containing classes that solve common coding problems in a standardized, platform-neutral manner. When Version 1.0 came out, in 1995, there were eight packages, each containing an average of 20 classes, with each class containing an average of 20 methods (that is, op codes). In Version 1.1, there were 22 packages. In Version 1.2, there were more than 60. And it now stands at more than 100 in Version 1.4. So the Java API now includes an estimated 40,000 op codes. However, I routinely use only five packages: java.lang (the base package), java.sql (for database access), java.math (for zoned and packed fields), java.util (for dates), and java.text (for field formatting). The real issue is not learning Java; it's learning what you don't need to learn. This is not so dissimilar from RPG. You wouldn't expect a newbie to master the matching-records or level-break capabilities during his first few weeks on the job. But just when you start breathing easier at the thought of not having to learn the entire Java API, you realize you need to learn more than Java: You need to learn a host of supporting technologies. If you are going to use Java servlets or JavaServer Pages for Web applications, you will need to learn how to create tables and forms in HTML. You will need to become comfortable using SQL statements. You must be comfortable navigating the Integrated File System (IFS) root directory hierarchy in OS/400. While those issues are worthy of discussion in themselves, I would like to describe how the minor features of the Java language were stumbling blocks in my own journey. There were several technical aspects of Java that got on my nerves, causing me to walk away muttering "stupid language" more than once. Tilting against these windmills added months to my learning experience and made the whole effort much harder than it needed to be. The technical issues had become emotional issues I had to work through. Case Sensitivity I loved that I could write code in lowercase, and to finally "STOP SHOUTING" when I programmed. I hated that the compiler, unlike RPG, didn't automatically set the code to uppercase to prevent customer and CUSTOMER from being considered two separate variables. The Java naming convention came to my rescue. By naming classes starting with uppercase, such as Tax, and variables and methods starting with lowercase, such as taxRate and calculateSalesTax() respectively, the errors did not occur. Curly Braces The use of { }, or curly braces, to enclose chunks of code is logical enough. What I objected to was that nested logic would look like a series of curly braces, one brace per line. I truly missed the ENDIF and ENDDO op codes I had used since the early 1990s. Indenting helps, and when I get really nostalgic, or the code becomes truly complex, I admit to adding short comments like this:
} // endif invoice status
} // endwhile rs.next( )
} // end of myMethod( )
Functions In calculus, the simple equation y = f(x) is used to illustrate that the value of the variable y is a function of the variable x. To solve for y means providing the function f with the value of x, which itself is not to be changed. Java embraces the concept of functions in the use of methods. Arguments (or parameters) are optionally passed to a method, and a result variable is returned. Of course, to make life interesting, some methods do not need arguments, and some return the special keyword void, meaning that nothing is returned. My first problem was fighting the fact that only one variable could be returned. I was accustomed to calling external programs with a list of several input and output parameters. How could I code with this ridiculous limitation of only one variable being returned? Then I learned that you can cheat in Java by passing an object reference. The method can change any attribute belonging to the object. Since arrays are objects, you can change every element of the array. Multiple Functions per Line In Java, a method ends with a semicolon (;). It is possible to put more than one method on the same line of text in modern text editors, which are not limited to a line width of 80 characters. For example:
attribute1 = someMethod( ); attribute2 =
anotherMethod(attribute1);
Fortunately, this technique is not considered good style and is rarely encountered in practice. Carriage returns are dirt cheap these days. Use them often. Nested Functions In RPG, things are simple: one op code for one concept. In Java, you can have a method return a value that is immediately treated as an argument to an enveloping, or wrapper, method. For example:
salesTax = calculateSalesTax(saleAmount,
getTaxRate(stateCode));
The compiler simply unwraps your code and keeps track of the working variables. In this example, the getTaxRate( ) method is executed first and the returned value is used as the second argument to the calculateSalesTax( ) method. Chained Functions Another feature of Java is the ability to sequentially pass a working variable between methods. This is similar to pipelining file I/O with Unix or DOS commands. In Java we use a period (.), or a dot, to signify the chaining. However, confusion can occur, since dot notation is used for other purposes. This example shows all three types of use: someObject.someField = resultSet. getString(custname).trim( ).toUpperCase(); The first dot indicates that someField is an attribute of someObject. The dot after resultSet means that the getString( ) method is a method of the class to which the resultSet object belongs. The next two dots indicate that the string obtained from getString( ) is to be trimmed of leading and trailing blanks, then converted to uppercase characters, before assigning the variable to someField. Cool! So Is It Worth It? Is it worth the effort to learn Java? The answer depends on your personality and your age. Are you satisfied with maintaining RPG code? Or do your juices start flowing at the thought of developing new applications? Java isn't the only way to create network-based, graphical applications, but it is a very good way that is truly portable, unlike RPG or Visual Basic. The good news is that there has never been more training material available to learn a language than with Java. The better news is that Java programs can run on devices from credit cards to mainframes. The best news is that everything you need to begin is free. What is stopping you? Douglas Smith is a 20-year veteran of programming RPG on IBM midrange computers. He slowly taught himself Java and became certified in 2001. Doug is now managing partner of Netroutines Software. E-mail: dsmith@netroutines.com
|
Editor
Contact the Editors |
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |