From: John Joyce Date: 2007-03-22T08:10:47+09:00 Subject: Re: does Ruby not support multiple "initialize" methods for a class??? Why do people want to do this? Other than just to do it. What was it about C letting you shoot yourself in the foot, and C++ letting you blow your whole leg off... On Mar 22, 2007, at 6:28 AM, ara.t.howard@noaa.gov wrote: > On Thu, 22 Mar 2007, Florian Frank wrote: > >> I don't consider this to be a pattern, I consider overloading >> constructors >> an anti-pattern. >> >> One of the few good Java books (among all the mediocre or even >> worse ones) >> is "Effective Java" by Joshua Bloch: >> >> http://java.sun.com/docs/books/effective/toc.html >> >> In the first chapter (actually chapter 2) in the first item Bloch >> advises >> Java programmers to consider using static factory methods instead >> of public >> and/or overloaded constructors. He lists the following advantages: >> >> : One advantage of static factory methods is that, unlike >> constructors, they have names. >> : If the parameters to a constructor do not, in and of themselves, >> describe the object being >> : returned, a static factory with a well-chosen name can make a >> class easier to use and the >> : resulting client code easier to read. >> : [...] >> >> The same is true in Ruby, if you use class methods (methods of a >> class's >> eigenclass). The workarounds in the initialize method even *look* >> code >> smelly. In Java overloading a constructor once or twice doesn't >> seem to be >> such a bad idea. But other programmers tend to just copy and paste >> another >> constructor below the others. Once you have a class that comes >> into the two >> digit range of constructors, it gets quite difficult to remember >> all the >> different possible permutations of types. (Yes, I had to maintain >> code like >> that.) >> >> And I think, overloading in general isn't such a good idea either. >> It can >> lead to really funny semantic errors, if it teams up with Java's >> numeric >> type promotion for example. (Yeah, this happened to me, too.) > > and this doesn't even take into account the confusion that can be > heaped upon > the programmer when default parameters are thrown into the mix. if > parameter > overloading were allowed in ruby we could have this: > > def m int > p int > p 42.0 > end > > def m int, float = 42.0 > p int > p float > end > > m 42 > > now, which one is called? in order to understand this kind of > stuff in c++ > you have to become the compiler. > > it's even worse when you can do > > m *args # yikes, which one is called? > > no thanks! > > cheers. > > -a > -- > be kind whenever possible... it is always possible. > - the dalai lama >