Q: What is almost as bad as a Singleton?

Posted by Dominik Tue, 06 Mar 2007 12:20:00 GMT

A: A class that has only static methods but also has (static) state.

In Java you often see XyzUtil or XyzHelper classes which only provide static methods (like, e.g., StringUtil from jakarta commons lang). These classes provide helper methods which usually act on an object which is passed along as an argument to the methods.

(Excursion: Java needs these static utilities because we can neither add new methods to a class without subclassing it and often cannot subclass the class we want to extend, like String, which is final. Ruby [and other languages] do not have this problem, since they provide the possibility to open a class and modify it or to use modules to mix in more behavior)

I don’t want to go into the question why I deem Singletons to be evil (I’m not the only one, just google for “Singleton evil”).

Now, a class which only has static methods and has a state is (imho) as bad as a Singleton, because it actually only is a Hidden Singleton, a way to work around the fact that the coding guidelines for a project don’t allow Singletons.

In the case I just stumbled upon such a hidden Singleton was used to retrieve a configuration object and to lazily create it, if it was not created before. So the first call to getConfiguration() created and stored (on a static field) a Configuration instance, and each further call just returned that instance. Now, first up, a getter should be idempotent anyway, but lets leave that aside. The class worked well in production settings. The first time the configuration was requested, it was created (which takes some time), afterwards the cached instance was returned. But during testing everything goes wrong. Using such a Hidden Singleton allows for the testing of only one Configuration. I can’t say: run this testcase with that configuration and this other testcase with that other configuration. The configuration was already loaded and won’t be modified.

So I either need a way to null the cached configuration on the Hidden Singleton, or need to get rid of it and replace it with some other pattern. What you usually want in such a setting is IoC, someone should simply set the configuration on all instances which need it (and such making those objects conform to the law of demeter, because they don’t need to fetch the Configuration from some other class). If you don’t have an IoC container, you might use a factory pattern to alleviate the pain the Hidden Singleton is causing. Use a factory to create the objects which need the Configuration. The factory can set the configuration on the objects during creation, and the configuration can be passed to the factory method (and, if really necessary, cached inside the factory until a different configuration is passed along…)

Posted in  | Tags , , ,  | no comments | no trackbacks

Browsing through the local bookstores

Posted by Dominik Sun, 18 Feb 2007 12:23:00 GMT

I’ve been browsing through the local bookstores yesterday and have been looking through the programming and IT sections. The two big bookstores I’ve been in both still haven’t got a section for Ruby. There are sections for everything you might (or then, might not) be looking for, Perl, PHP, Python, Java, C#, Joomla, Mambo, ActionScript etc. At the first bookstore I found one book about Rails, the other had four or five. Both had them filed under general programming. Both did not have a single book about Ruby (“The Ruby Way”, the “Pickaxe”, nothing).

That makes me question:

  • How big is the acceptance of Ruby in Germany? and
  • How big is the acceptance of Rails in Germany?

I have seen a few articles about Rails in german language magazines, but I somehow have got the feeling, that Ruby and Rails haven’t yet arrived in Germany yet.

Am I right? Or do I just look in the wrong places? At least there are 139 people from Germany registered at workingwithrails (as of today).

Workingwithrails lists two other developers in Freiburg, Germany. Maybe three is enough to start a ruby brigade? We’ll see.

Posted in  | Tags ,  | no comments | no trackbacks

Gem Survey

Posted by Dominik Mon, 12 Feb 2007 09:17:00 GMT

In response to Mike Clark’s question, here is what I’ve got on my machine:

wei-fieg@nono ~ $ sudo gem list --local | grep '([0-9]\.'
actionmailer (1.3.2, 1.3.1)
actionpack (1.13.2, 1.13.1)
actionwebservice (1.2.2, 1.2.1)
activerecord (1.15.2, 1.15.1)
activesupport (1.4.1, 1.4.0)
acts_as_taggable (2.0.2)
ajax_scaffold_generator (3.1.11, 3.1.10)
capistrano (1.4.0, 1.3.1)
cheat (1.2.1)
coverage (0.3)
deprec (1.2.3, 1.2.1)
facets (1.8.20, 1.7.46)
ferret (0.10.14)
flickr (1.0.0)
highline (1.2.7, 1.2.5)
hoe (1.1.7)
log4r (1.0.5)
mocha (0.4.0)
model_security_generator (0.0.9)
mysql (2.7)
needle (1.3.0)
net-sftp (1.1.0)
net-ssh (1.0.10)
openid_login_generator (0.1)
rails (1.2.2, 1.2.1)
rake (0.7.1)
RedCloth (3.0.4)
ruby-activeldap (0.8.1)
ruby-openid (1.1.4)
ruby-yadis (0.3.4)
rubyforge (0.4.0)
RubyInline (3.6.2)
sources (0.0.1)
streamlined_generator (0.0.5)
tattle (1.0.1)
termios (0.9.4)
wirble (0.1.2)
xml-simple (1.0.10)

Hmm, the number matches that of Mike Clark’s list (but is really low compared to Chad Fowler’s), the individual gems don’t all match up, though. But then, diversification is a good thing.

Posted in  | Tags ,  | no comments | no trackbacks