This post originated from an RSS feed registered with Agile Buzz
by Martin Fowler.
Original Post: DynamicTypeCheck
Feed Title: Martin Fowler's Bliki
Feed URL: http://martinfowler.com/feed.atom
Feed Description: A cross between a blog and wiki of my partly-formed ideas on software development
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 is_a?, kind_of?,
and instance_of?. The lines of code come from the
standard rake stats command in rails. (There's an argument that
respond_to? and aClass === anInstance
should be checked as well, but this is the data I have. The
qualitative view is that their use is equally uncommon.)
Project ID
Code
Test
LOC / type check
test LOC / code LOC
type checks
Lines of Code
type checks
Lines of Code
A
16
13318
0
9856
1448
0.74
B
14
19138
0
17123
2590
0.89
C
0
2607
0
2981
∞
1.14
D
7
4265
3
4069
833
0.95
E
32
29619
60
97688
1384
3.3
F
18
~9500
N/A
N/A
528
N/A
G
0
2455
0
3290
∞
1.34
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.
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.