Friday, August 6, 2010

Class Explosion and the dynamic world

I just read a blog post from Ayende titled Data access is contextual, a generic approach will fail and it has me thinking about class explosion that can occur in static languages. Now I'm not a fan of trying to use a single anemic model everywhere or just having a model and then a serializable version of it. I actually like (and this is how I originally learned and thought about DTOs) having many DTOs and contextual action/task based APIs. Certainly leads to a lot of code but hopefully (as Ayende mentioned) a lot of simple code.

Now why do we do this explosion of interfaces, classes, mappers, etc? We generally do it to try and stay flexible. I think we all know that direct coupling of implementations is generally a bad idea, so we use interfaces. Now we have the interface, implementation, and wiring code to keep track of. We know that DTOs are good for sending information about a model across a boundary. Now we have the model, DTOs, and mapping code. This is a lot of code certainly but I personally find great value in these kinds of separations. You'll generally find yourself less frequently in a situation where you have to turn down an idea due to the code being too hard to manipulate (or at least delay that from happening longer).

Thinking about all of this reminded me about how usually you don't see this kind of separation in code written in a dynamic language. I think that is because (and I'm quoting a friend here) "you are doing interface-less interface programming". At any point you can perform a mapping, or wiring. You don't have to create a new class or refactor to an interface. The code can go in any place at any time. You don't need an IOC container, the language has already kept your code flexible. Now of course I'm not sure currently how you keep all this straight in the dynamic world nor how it plays out in large sophisticated applications.

I have been playing around with Python for a little over a year now and I have to say I see things a little differently. I start seeing things in a manner of "oh I'll just map that over to this here" or "ah just tack this on and pass it through that". I find myself sometimes frustrated in C# because of the lack of flexibility (of course sometimes I feel scared because of the lack of enforced structure in Python but that is starting to fade).

An area that I have heard that dynamic languages greatly help is with writing and maintaining test code and specifically when doing Test Driven Development. Generally I seem to find that people who have done testing in both static and dynamic languages usually say that the static world is a pain in the ass. Perhaps all this enforced structure is why only the more disciplined among us write and keep up with tests, sophisticated models, or try to always use an IOC container and interfaces.

Well I intend on trying to get in the habit of TDD and understand its benefits. As Dave has mentioned before we have started a Code Dojo and the first goals of this are to learn Ruby together, use TDD, and complete a project collaboratively in Ruby.

I write this post to see if anyone has any thoughts about why we do the things we do in the static world and also to say that I'm hoping to write more posts about my thoughts on TDD in Ruby. I can compare it to writing tests in C# and I hope to transfer my experiences in Ruby over to C# in hopes of trying to improve my habits and coding abilities in the static world.

No comments:

Post a Comment