February 01, 2011

Special variables in Java

Wow. This is how complicated it gets to have he functionality of special variables in a language that really doesn't want them (and it's brought to you by Google): Google Guice


Bruno said...

I think your dependency injection framework forgot to inject a title for this post. ;-)

Seriously though: there is quite an acceptable mechanism for injection in the language which is called "constructor arguments". You have to write boilerplate constructors that copy these arguments to private fields, but otherwise it isn't so bad.

Pascal Costanza said...

Hi Bruno,

Thanks for the hint that a title was missing from my post. This is corrected now ("Special variables in Java").

Niko said...

How would special variables solve the issue? What do you mean by special variable?

Pascal Costanza said...

Hi Niko,

I'm talking about dynamically scoped variables, also known as special variables in Common Lisp, or fluid variables or parameter objects in some Scheme implementations. See this chapter from Peter Seibel's book on Common Lisp for one introduction.

With special variables, you can make decisions in one place of your code that gets picked up in some other place, without the nastiness involved in side effecting global variables.

Unknown said...

Actually, I don't think so.

The advantage of guice is that guice has an injector which *creates* the objects for you. With special variables, you are still responsible for creating them yourself.

Jason Wilson said...

The only reason that it matters that Guice creates the objects for you is that every object ends up taking a million constructor args. If you used fluid variables instead, you really only need to supply a couple of args and spelling out just those may actually make your code easier to read. Also, I see less of a need for objects when procedural code would work just fine but Java forces objects on you.

Vladimir Sedach said...

I've been saying this for years: Dependency Injection is the dumbest fad in object-oriented programming.

WRT object creation, there is nothing stopping you from using dynamic scoping to also supplant the service locator pattern (two OO albatrosses with one stone, three if you count factories):


Guice just takes the idiocy to a whole new level. It's amazing what people come up with (and think is a good thing!) when they don't suspect that the problem they're trying to solve is completely arbitrary and trivial to sidestep.

Robert Wensman said...

A little bit off topic. But I just happended to google my name and saw a comment made by you 2008 mentioning me.

Remember me, Robert Wensman (and Bjorn) from ETAPS in warsaw? We had that Fierabend group where we discussed the future of software etc...

Anyway, you can easily find me on Facebook.

Here are some (hopefully cool) stuff I have been thinking about in the recent years:


I am planning to create a programming language of sorts soon, however... It might take a while :-)