Compile time type checking

From: Dave Thomas [mailto:Dave@PragmaticProgrammer.com]
Sent: Tuesday, August 27, 2002 2:12 PM
To: ruby-talk@ruby-lang.org
Subject: Re: compile time type checking

“Volkmann, Mark” Mark.Volkmann@AGEDWARDS.com writes:

I’m curious how some of you that have written real
applications in Ruby have
dealt with this. Don’t you have problems where an
application crashes on a
user because the wrong type of object gets passed to a
method and it tries
to invoke a method of the object that it doesn’t have?

I love Ruby syntax, but this issue may be one that keeps
many people using
Java instead.

But Java has run-time type checking as well. Consider an average
running Java program. How many objects are extant? 1,000? 2,000? How
many variables do you have? 20? 30? So, where are all the other
objects? In collections. And how do you get to them? By casting at
every access. The type gets discovered at run time.

How many class cast exceptions do you see in Java? In reality, not
many.

That matches my experience. I don’t see ClassCastExceptions all that often.
However, I think I would if it wasn’t for compile-time type checking.

In the same way, I don’t often see the wrong kind of object being used
in Ruby. However, to ensure that, I have to exercise the various paths
through the code, so I rely on unit tests to give me the confidence
that my code is reasonable.

Would you agree that with Ruby you have to write more unit tests than you do
in Java? You need to test that the types of parameters being passed to every
method are what was expected. It seems that this would cut into the
productivity gains in writing the actual code when compared to Java.

Even better than compile-time type checking is the fact that my IDE, IDEA,
flags parameter type issues before I even compile. I wish there was a Ruby
IDE that could do that.

I really want to be convinced that this whole type checking thing is a
non-issue for Ruby, but I’m not quite there yet.

···

-----Original Message-----


WARNING: All e-mail sent to and from this address will be received or
otherwise recorded by the A.G. Edwards corporate e-mail system and is
subject to archival, monitoring or review by, and/or disclosure to,
someone other than the recipient.


“Volkmann, Mark” Mark.Volkmann@AGEDWARDS.com writes:

Would you agree that with Ruby you have to write more unit tests
than you do in Java?

Per function delivered? No, I don’t think so. For example, the Java
equivalent of

names = people.map {|p| p.name }

would probably merit a unit test. In Ruby it doesn’t. I’d say my
productivity is still way ahead in Ruby. Even testing is easier, as I
don’t have to worry about producing complex mock environments.

You need to test that the types of parameters being passed to every
method are what was expected. It seems that this would cut into the
productivity gains in writing the actual code when compared to Java.

Why would I need to check the types of parameters? (Or, I guess more
accurately, I’ve never felt the need to check the types of
parameters). It just isn’t a problem that crops up in my experience.

I really want to be convinced that this whole type checking thing is
a non-issue for Ruby, but I’m not quite there yet.

I was there with you: I came from a strong-typing background and
looked askance at languages such as Ruby and Smalltalk. Experience
writing large Perl programs only reinforced my biases. However,
writing Ruby code has made a convert of me. I’m just finishing a
fairly large Ruby web application (22kloc of Ruby, which is just below
the sentient level). The only time I missed compile-time type checking
was when I made major refactorings and wanted to ensure I’d changed all
the uses of a particular class. However, I still feel that there’s no
way I could have delivered this kind of functionality this quickly in
(say) Java.

My advice (having been there) is to try it. Give it a number of
months, and try doing a decent sized project. Then form a personal
opinion. There’s no correct answer: it comes down to personal
preference.

Cheers

Dave

The only time I missed compile-time type checking
was when I made major refactorings and wanted to ensure I’d changed all
the uses of a particular class.

I’ve found the same for me in some recent C# work – it was handy to have
the compiler quickly rip off all the places I still needed to change the
expected object in a method, or the new name of a method or somesuch.

Chris