Recently some of our developers ran into the accusation that with a dynamic language like ruby you use so many dynamic type checks that you end up effectively writing your own type system. So they thought, since we've written a lot of real ruby code - how often do we make dynamic type checks? Michael Schubert gathered up the data.
The table below contains the data. We define a dynamic type check
as the use of the methods
instance_of?. The lines of code come from the
standard rake stats command in rails.
|Project ID||Code||Test||LOC / |
|test LOC /|
The moral of this data is that you shouldn't expect to see a lot of type check calls in your ruby code base. This, of course, is true of any dynamic language. It was generally considered bad form in Smalltalk circles I inhabited too.
The methods that were checked for in this data aren't the only ones
that can be considered a dynamic type check. Other cases are
aClass === anInstance. Our
folks felt that these cases were no more common than the ones they
Most uses are those of dealing with liberal input - eg where a method parameter can be a string, symbol, or array. These crop up in DSLish situations where you want liberal input for the high readability.