The Artima Developer Community
Sponsored Link

Weblogs Forum
Programming with "Duh" Typing

370 replies on 371 pages. Most recent reply: Aug 8, 2007 9:54 AM by James Watson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 370 replies on 371 pages [ « | 1 ... 288 289 290 291 292 293 294 295 296 ... 371  | » ]
Cedric Beust

Posts: 140
Nickname: cbeust
Registered: Feb, 2004

Re: Programming with "Duh" Typing Posted: Jul 30, 2007 10:21 AM
Reply to this message Reply
Advertisement
> > 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

Flat View: This topic has 370 replies on 371 pages [ « | 288  289  290  291  292  293  294  295  296 | » ]
Topic: Programming with "Duh" Typing Previous Topic   Next Topic Topic: Python 3000 Plea for Help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use