This past week I got into a small discussion about the importance of loosely coupled code. Specifically, I was looking at event handler code that did nothing more than change the size of another element on the screen. But the event handler code was in the controller which in the particular implementation we are using was the event handler portion of our Model in a MVVM architecture. The question becomes does this code belong in the view, or does it belong in the controller?
The question of where code belongs leads eventually to arguments for loosely coupled code. If I put code in my controller that is manipulating the view, then I either need to mock out my view in order to test my controller or I have to have an instance of my view available to test my controller. Having coded enough systems to know that both of those choices are problematic, I opt for placing the view specific code in the view. Another place where you might place this code would be in a View Specific event handler class. But that would only be for the purposes of clean separation. Something we might consider doing if the View were plain HTML. But in our particular case, the view is generated from code, so placing the handlers in that same code seems to be the most appropriate location for it.
But all of this leads to a larger question. Why should your code be loosely coupled at all?
There is a guy I’m working with who is trying to wrap his head around design principles. He’s been watching a lot of PluralSight videos. As he was processing information about Inversion of Control, he asked the natural question I’ve never actually considered before. “Why is it called Inversion of Control? Normally, when you talk about ‘Inversion’ you are talking about reversing something or negating something. That isn’t what we are doing here.”
I’ve had two incidents recently that have shown me how TDD impacts Software Architecture. Both of these are with code I’m working on.
What Software Architecture Might Do
Software architecture might specify how is put together at a very high level. For example, software architecture might specify that we use a three tiered approach or an n-tiered approach. This approach places our view code is at one level, our business rules are at another level, and our data access at yet a third level.