In last weeks post we had a quick look at a little hack called digg.
In order for this to work we need to extend each class we want to digg with the
This leads to modifying core classes; and we should frown upon that. 😠
So what else can we do?
That would work. I bet this will very quickly lead to a “Util” class that become a bag of tricks. 👜✨ We don’t want that.
So how about adding some instantiation?
Alright! Now we can digg for multiple paths on the same set of data. 😎
But… This doesn’t feel right either. 😧 The act of digging is defined by what steps are taken. (e.g.: Pick up spade, stab in ground, lift, dump, repeat). It is not defined by the soil that we digg in.
See! That just doesn’t look good.
What if we turn that around?
Now we define “how to digg” when initializing. See how much better this looks for loops:
This way of thinking about your objects, by defining behaviour on initializing, has a lot of benefits.
Not only is it faster (in this case) (gist), but also more intention revealing. 📖
By instantiating behaviour you can start to compose your objects with more ease, or start thinking about caching or optimising the behaviour.
Let me know what you think. And if you apply this in your code I’d be happy to have a look at it.