Outside In Or Inside Out
Outside In Or Inside Out
What are the benefits to start programming from the outside in (big picture) or inside out (build around one behavior)?
Join the DZone community and get the full member experience.Join For Free
Most of the time when we’re building software, we’re thinking inside out. We focus on the behavior that we want to create, and build the system up from that. We do this because we’re anxious to get at the heart of the problem and solve the core issues. But there’s a problem with this approach because as we move up levels of abstraction connecting those core issues to a broader system, it can be hard to forge the right interfaces. This is inside out programming.
By contrast, outside in programming starts from the big picture, from the caller’s perspective, and drills down into the system.
One approach is not better than the other, they are both useful and valid, but we want to draw on both to build good robust systems that have strong interfaces.
Test first development is a powerful way to do outside in programming. As my associate Scott Bain likes to say, “Think of the test as your first client.” I love this statement because it really summarizes a whole range of benefits that you get from doing TDD. By writing the test first, you’re thinking from the client’s perspective, about what you want rather than how to get it, and this ripples through the design and implementation of the system, keeping it well encapsulated and easy to understand.
Sometimes we need to figure out an implementation and so we do inside out programming, but usually I want some context for an implementation so I start with the test.
Regardless of how I start, I generally want to think in both directions. How do clients want to call the service that I am about to create? And then once I call that way how can I best go about doing the task at hand?
This often reveals some intermediary steps that I want to separate out. For example, if a client wants me to process a document at a URL that it passes me I should separate out the retrieval of the document from the processing because by doing so, I make both steps far more testable and easier to integrate.
It’s generally advisable to separate out perspective in code regardless of what they are. The UML calls out three perspectives in code that are good to separate out. These are the conceptual, the specification, and the implementation perspectives.
The conceptual perspective shows what we want but not how to get it. This typically maps to abstractions or interfaces in code.
The specification perspective shows how the different entities communicate with each other in order to accomplish their tasks. This maps to method signatures that determine how methods are evoked and what they return.
Finally, the implementation perspective is the code itself.
It’s a good idea to separate these perspectives out when building software, and in fact if we look at the twenty-three design patterns from the Gang of Four we would notice that by and large these three perspectives are separated out, and this is one of the reasons patterns are so useful and so easily maintainable.
Published at DZone with permission of David Bernstein , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.