Code Less: A Language Keystroke Experiment

Code Less: A Language Keystroke Experiment

Paul Pagel
Paul Pagel

June 08, 2008

When I first started writing code in Ruby, it was a breath of fresh air after writing C# code for a year. Ruby had a thesis, a clear purpose, rather than a hodgepodge of features strung together. It was a language obviously written by someone who cared about what the code looked like.

So I jumped into it and loved writing Ruby code (still do!). Recently I had to write a project in Java using IntelliJ, and the powerful IDE was also a breath of fresh air after using text editors to write Ruby code.

The IDE helps me in the same way Ruby helped me. I write less code that does the same thing, without loosing expressiveness or transparency of intent. Less code in this case meaning less keystrokes.

So, I am going to do a little experiment using two editors and languages: Ruby with Textmate and Java with IntelliJ. I want to see how many lines of code I have to write. The application I am going to start is a simple baseball at bat scorer. First, lets see the Ruby version:

class AtBat

		def initialize
				@strikes = 0
		end

		def pitch(pitch_type)
				@strikes += 1 if pitch_type == :called_strike
		end

		def result
				return :strikeout if @strikes == 3
		end

end

describe "At Bat Scorer" do

		it "should call out at 3 strikes" do
				at_bat = AtBat.new
				at_bat.pitch(:called_strike)
				at_bat.pitch(:called_strike)
				at_bat.pitch(:called_strike)
				at_bat.result.should == :strikeout
		end

end

We have about 27 lines of code written, and other than the describe method, which I have a macro for, I typed them all by hand. Let’s look at the Java code:


import junit.framework.TestCase;

public class AtBatTest extends TestCase {
				public void testThreeStrikesAndYourOut() throws Exception {
								AtBat atBat = new AtBat();
								
								atBat.pitch("called_strike");
								atBat.pitch("called_strike");
								atBat.pitch("called_strike");
								
								assertEquals("strikeout", atBat.result());
				}
}

This is just the JUnit test I wrote, there is no production code. It is about 16 lines of code, which I wrote all of but the import statement. By doing a few alt-enters on the squiggly lines, I can get a stub of a class looking like this:

public class AtBat
{
				public void pitch(String pitchType)
				{
				}

				public String result()
				{
								return null;
				}
}

That is 13 more lines of code, without really typing, just hitting enter a few times. Now, lets start to make the method pass. I am going to write the algorithm without worrying about all the type definition Java wants. Here is what it looks like:

public class AtBat
{
				public void pitch(String pitchType)
				{
								if(pitchType.equals("called_strike"))
												strikes++;
				}

				public String result()
				{
								if(strikes == 3)
												return "strikeout";
								return null;
				}
}

So the code written is around 4 lines of code. Lets use the IntelliJ autocomplete features to help us make a passing test, which looks like:


public class AtBat
{
				private int strikes;

				public void pitch(String pitchType)
				{
								if(pitchType.equals("called_strike"))
												strikes++;
				}

				public String result()
				{
								if(strikes == 3)
												return "strikeout";
								return null;
				}
}

So roughly, the Ruby version was 23 lines of written code and the Java version was about 20 lines of written code. There is an expense to doing all of the alt-enters to auto-generate the method and variable stubs, but IntelliJ is pretty smart about what it generates.

Also, I understand a single test doesn’t tell the whole story, but it is a good indication. I found as I wrote more tests for this application, a smaller amount of keystrokes was needed for the Java version, and the same amount was needed for Ruby.

This is due to the refactoring tools, and Intellisense. As applications get larger, I find the IntelliJ refactorings become more useful. Inversely, Ruby refactorings become more painful, as they are mostly done by hand.

So, in the end the constraints on a static language allows the IDE to make the refactoring tools better. Specifically, when writing Java code, I can lean on the IDE to generate all the uninteresting stubs for me.

All I do is fill in the algorithms: the fun part. When I start to see places where my code can be cleaned up a bit, I can lean on the IDE again to do those refactorings for me once I recognize the need for them.

Removing and optimizing the code is something which is easily deducible in static languages, as it is mostly pattern matching, with no monkey patches, meta-programming, and evals to worry about.

This is a limitation which frustrates me as a developer who wants to have a large set of tools in my bag, but is helpful when it comes to developing a powerful IDE.

The number of lines of code I need to type isn’t the reason I choose a language over another. In fact, it would be pretty low on the list of deciding factors. However, it is interesting to see what each language and its sets of tools do best.

Hopefully the Ruby community can take some notes from them. I would rather solve the other end of the equation, get a powerful Ruby IDE. I know Eclipse and Net Beans have some preliminary refactoring tools, but they are still a ways from being as seamless as their Java counterparts.