Cedric Beust
Posts: 140
Nickname: cbeust
Registered: Feb, 2004
|
|
Re: Programming with "Duh" Typing
|
Posted: Jul 30, 2007 10:21 AM
|
|
> > Any automated refactoring is superior to manual > > refactoring, even if it's a simple string > search/replace > > that asks for confirmation from the user, as is the > case > > in dynamically typed languages. > > The Ruby refactoring browsers uses the AST to do common > refactorings. > > http://r2.ifs.hsr.ch/screencasts/extract_method.htm > http://r2.ifs.hsr.ch/screencasts/rename_method.htm
This is actually a perfect illustration of the dangers of dynamic typing.
Here is why.
The video is showing you three classes:
A B, which extends A C, which is unrelated to A and B
There three classes define a method "eat". The video shows that if you rename this method to "munch", only A#eat and B#eat get renamed but C#eat is left untouched. Fine.
What the video doesn't show is how this can go wrong.
By virtue of duck typing, I can write the following code:
@animals = [] @animals << A.new @animals << B.new @animals << C.new
@animals.each { |a| a.eat }
C is unrelated to A and B, but since it has an eat method (duck typing), this code will work.
Except that... it will break after the refactoring since it has now become:
@animals.each { |a| a.munch }
and that the third element of @animals is an instance of C, whose "eat" method hasn't been renamed to "munch".
-- Cedric http://testng.org
|
|