Yeah, I know its sound pretty stupid... because we're all different, right? But for small things like "It such nice weather. I really like an icecream. I wonder if I have to stand in a long queue to buy one" it work fairly well. In this example, I probably would have to wait a while to get an icecream, because if I want an icecream other people will as well.
To get to the point, when applying this "theory" on my latest micro-project I realized that having to call the done() method to close a dynamic scope annoy a lot of people. Why? Because it annoys me. Here are a few reasons for that:
- it's an implementation detail that is irrelevant to the service the Scope class provides;
- in some sense it exposes implementation;
- it's a detail that is easy to forget;
- forgetting to call done() will not break your code in all cases, thus, doing so is a hard-to-find bug.
Instead of explicitly pushing and poping objects to the Stack<Object> that Scope contains I'm using the call stack of the current thread. That is, when the Scope.of method is called it looks at the current call stack and finds where a new scope was created. This makes the done() method redundant and fixes the problems with the Scope class that annoyed me.
Searching through the call-stack is heavier on the CPU, but it's easier on the programmer - a trade-off I'm willing to make. There is also a bit of memory overhead because Scope now contains a map that holds object that otherwise would not exist at all or be possible to garbage collect. Again, a trade-off I'm willing to make.
To conclude, with the new version of Scope it's now possible to do
new Scope(someObject) { {
methodCallingMethodCallingMethodCallingMethodUsingThatObject();
} };
instead of
new Scope(someObject) { {
methodCallingMethodCallingMethodCallingMethodUsingThatObject();
} }.done();
which is a Good Thing.
No comments:
Post a Comment