September 2005

Where are we going?

A colleague of mine recently predicted that Ruby would overtake PHP in the next 2 years. Another colleague recently commented on the way Java is trying to makeover its midlife crisis “Java 5 is not innovation; it is contact lenses and a toupee. ”

One thing I know is that Java will not always be the language of choice for serious web development. Anyone who says otherwise needs to just remember five letters: C.O.B.O.L. My guess is that this transition could take place as quickly as three years from now. Will Ruby replace it? Well, with Rails it has a shot. Its such an easy environment that it could potentially take on Java.

I believe Java’s community is why it has had enormous growth over the last ten years - it grew a base of people very quickly that contributed a large amount of free code that could easily (I’m stretching a bit here) be included in a project. Ruby seems to have the same thing going for it. There is a friendly community that is growing very rapidly, with an ever expanding set of libraries.

Then again, with the velocity that Java had in the beginning, I would not be suprised that some other language currently being dreamed up somewhere could hit the sweet spot of expression and APIs, along with that killer app that pushes it over the edge (servlets for Java), making it the “New Thing”. We’ll see soon enough.

General
Java
Ruby
Software Development

Comments (2)

Permalink

Is Ruby better than Java? No (and Vice Versa)

Pragmatic programmer Dave Thomas (Prag Dave) has written another entry in his excellent blog about the differences in languages ["Is Ruby Better than... ?"], particularly the punditry around comparing Ruby to Java. Something that I have recently been somewhat guilty of.

If you ask any of my colleagues about my choices in language, they would (hopefully) tell you that I am also a pragmatist when it comes to languages. I will write your software the way you want with the language you desire. But when it comes to my personal software, stuff I write for myself, I generally decide the language based on my current needs. For instance, until recently when I’ve needed a general utility, I almost always have written it in Java (or maybe in Jython), as it is the language (really its the API) that I am most familiar with. Some of my (much) older code is in C++. I still play around with VB when I want to do GUI screen layouts. However I am learning Ruby right now, and so for all new code that I write, I am choosing Ruby, unless there is a really good reason not to. This is not because I think Ruby is the answer to all programming ills, it is because I am trying to really learn it, and I will not be fortunate enough to get much on the job training for it.

Really it comes down to figuring out the best tool for the job. As Dave puts it “Is your chisel better than my hammer? If I’m forming dovetail joints, yes. If I’m nailing two-by-fours, no.”

General
Java
Ruby
Software Development

Comments (2)

Permalink

No Fluff Coming to Omaha

I’m getting anxious for the No Fluff Just Stuff [link] conference. It’ll be here October 7-9th, and will be a great event. I’m also a bit nervous, as my company is not exactly sure they are willing to send me again (went to the Des Moines show in July), and said that they would not decide until Monday, the final day of the Early Bird discount. I am going no matter what, but I would have liked a little bit more time, in case they decide to hold off a day or something. And, there are 4 other people that will probably not go if they say no.

I’m really looking forward to Dave Thomas’s Ruby talks, as I did not get to see those in Des Moines. Hopefully they will shed a little more light on that language for me. The more important talks for me though are the Refactoring and Unit testing talks, given by Andrew Glover, who was here on Tuesday for our OJUG meeting (Thanks Andrew, your presentation Rocked!). I really nee help trying to come up with some strategery on making my current project better.

General
Java
OJUG
Ruby
Software Development

Comments (1)

Permalink

Ruby Simplicity

Now, I am not an expert at Ruby. I know enough of the language to make simple scripts work, and I keep my trusty pickaxe handy in case of (frequent) emergencies. What I needed was a http server that posted back a series of XML files (based on a timestamp in the XML) to the requester to simplify testing of the system. A really simple app is all that is needed. Here it is:

require ‘webrick’ include WEBrick # get directory to get files from dir = (ARGV[0] || “.”) # change directory, read in files Dir.chdir(dir) puts “Processing files from directory [#{Dir.getwd}]” filenames = Dir.glob(”*.{xml,XML}”).sort files = Array.new for x in 0…filenames.size puts “Adding file: #{filenames[x]}” files[x] = File.read(filenames[x]) end # filecount is used for handling the rotation of files filecount = 0 # Create the server, add the servlet s = HTTPServer.new( :Port => 8080 ) s.mount_proc(”/FileServ”){|req, res| res['Content-Type'] = “application/xml” res.body = files[filecount % files.size] filecount += 1 } # start the server trap(”INT”){ s.shutdown } s.start

Thats it. 31 lines including whitespace and comments (20 actual source lines). Does exactly what I needed, with an optimization of making the XML filenames include the date for easy sorting (used same technique in the Java example as well). It should also be noted that this is a standalone script… I can just double-click on it in windows and its running.

Now lets look at the Java code:

package net.secosoft.fileserv; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FileServlet extends HttpServlet implements Servlet { static int fileCount = 0; static byte[][] files = null; public FileServlet() { super(); } public void init(ServletConfig config) throws ServletException { super.init(config); File dir = new File(this.getInitParameter(”filesPath”)); File[] fileList = dir.listFiles( new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(”.xml”); } }); Arrays.sort(fileList); files = new byte[fileList.length][]; for(int f = 0; f < fileList.length; f++ ) { try { files[f] = load(fileList[f]); System.out.println("File [" + fileList[f].toString() + "] loaded."); } catch(IOException ioe) { System.out.println("Error loading file [" + fileList[f].toString() + "]" ); } } } protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int currentFile = fileCount % files.length; response.setContentType("application/xml"); response.setContentLength(files[currentFile].length); OutputStream out = response.getOutputStream(); out.write(files[currentFile]); out.flush(); fileCount++; } private byte[] load(File file) throws IOException { BufferedInputStream in = new BufferedInputStream( new FileInputStream(file)); byte[] buffer = new byte[4096]; ByteArrayOutputStream baos = new ByteArrayOutputStream((int)file.length()); while(true) { int len = in.read(buffer); if ( len == -1 ) break; baos.write(buffer,0,len); } return baos.toByteArray(); } }

84 lines with comments and whitespace, 49 without (comments, whitespace or import statements). But wait, theres more! This is a servlet… we cannot forget about our trusty web.xml file! Add an extra 17 for that… and the fact that I had to deploy this to a server. Thank goodness Eclipse has a export War tool that can put the war file directly in Tomcat’s webapps directory, otherwise I would have had an Ant script as well.

Before you all go crazy trying to count the lines of code, understand that I only removed the whitespace and commented lines from the above blocks and then counted what remained. I coded these as I always code, so while there are obvious ways of shrinking the number of lines for each of them, I think the loc : expressiveness ratio would decrease. Again, thats subjective, and this is my code the way I write code, so just take this as an example, not a rigorously controlled test scenario.

Given that, both code blocks are pretty straightforward. Ruby, with over 50% less lines (21 vs 49), has a major advantage above just LoC: the fact that I do not need to set up a servlet container (not that that is hard), nor do I need to go through the extra step of deploying a WAR file to said container. The point is: there is simplicity there… And for what I needed: perfect.

It took me only about 30 minutes to make the java version (already had servlet container set up, etc). The Ruby one took quite a bit longer, though the reasons for that are solely my inexperience with the language and its APIs. I think the code was mostly done in around 10 minutes, but it took a lot of time to find simple little things that I already learned how to avoid a long long time ago in Java.

Also, I’m sure that I’ve missed a couple of usefull shortcuts in both the Ruby and the Java code… if you have pointers for me I always appreciate learning something new.

General
Java
Ruby
Software Development

Comments (8)

Permalink

Ruby, My Dear

I have been playing with Ruby off and on for a while now (since the Des Moines No Fluff Just Stuff conference) and am finally starting to become productive with it. My copy of pickaxe came in early last week, so things have really picked up. On Friday I wrote my first “real” script (i.e. actually does something useful for me, not just a tutorial).

It’s a very easy language, from the standpoint that I do not really know the syntax, yet it seems to write naturally. It is much easy to get started on a problem, where as in Java it always starts with:

class DoSomethingOrOther public static void main(String[] args) { System.out.println(”Hello, please help me”); } }

And in Ruby its:

puts “Ruby, my dear”

Of course, that is overly simplistic… or is it? Writing a simple web app with a database using rails is so trivial I still have a hard time believing it can be that easy. Take a look at this, this and this.

Another scripting language I really like is Jython, which has the really handy ability to use (and simplify!) the Java standard APIs, which I have been using daily for the last 5 years. I like the expressiveness of Jython, which is really just Python on the Java VM and in my limited experience, similiar to Ruby. But it suffers from being considered an “add-on” to Java, and having to compete for a community with the other VM-based scripting languages. I will not be investing anymore time in Jython, unless I have a specific need for it. And at that point, I would probably look at Groovy, if I need Java libraries. Otherwise, its Ruby for me.

General
Java
Ruby
Software Development

Comments (7)

Permalink