When I'm doing design and refactoring, part of me worries that I'm just creating a mess of objects and classes. One of the kinds of mess that I feel I make the most are lateral abstractions. What I mean by lateral abstraction is an abstraction that is at the same level as the code I abstracted it out of. It doesn't add very much to the conversation, and it doesn't introduce a finer vocabulary than was already there. When looking over the code I wrote in Writing a Better Code Narrative, I get a slight feeling in the back of my head that some of that code is lateral abstraction.
Without re-posting all of the code (which can be found here), I started with a CommandParser class that read a string of character commands and "executed" those commands to produce a new output string. The original code was intentionally hairy, but I refactored it down into a series of Command classes to encapsulate the individual behaviors, and a CommandFactory to encapsulate the algorithm to map character commands to Command objects. The Command class provided a nice abstraction over the individual algorithms, but I worried a bit that the CommandFactory was a lateral abstraction. How is a CommandFactoryreally different from a CommandParser? Isn't a CommandParser's job to map character commands to Command objects?
Early on while reading GOOS, the authors have an aside where the talk about the classic topics of coupling and cohesion. In that aside, they also state:
At the other extreme, a class that parses only punctuation in a URL is unlikely to be coherent, because it doesnt' represent a whole concept.
– Growing Object-Oriented Software Guided by Tests, page 12
Here they almost1 define a new concept that, honestly, should be an equal to coupling and cohesion: coherence, the idea that a class should represent not just a single responsibility, but all of that single responsibility. This is the Single Responsibility Principle from the other end. A class should have one responsibility, not two, and not one-half.
Now we can circle around to lateral abstractions again. Some lateral abstractions encapsulate a whole and complete idea that just happens to sit at the same level of abstraction. We can think of a file and a network socket as being at the same level, since they are both I/O resources, but they represent different ideas. Other lateral abstractions, though, disrupt coherence.
The CommandParser above sits at one level of abstraction. It maps a list of character command strings to an output string. It uses a CommandFactory to handle mapping individual character codes to Command objects. The levels of abstraction are very close here, and we could go either way:
The CommandParser and CommandFactory sit at slightly different levels of abstraction. The CommandParser deals with strings and Command objects. The CommandFactory sits at a slightly lower level and deals with characters and Command objects. By pulling CommandFactory out, CommandParser can manage a higher level of abstraction of looping and piecing together the outputs of Commands.
or...
The CommandParser and CommandFactory sit at the same level of abstraction, meaning CommandFactory is superfluous. Instead of extracting a whole new class, we could have just as easily (or perhaps more easily) extracted the complicated code into a private method. The CommandParser#parseCommands method would be just as clear as with a separate class, but without the overhead.
To be honest, I can't say exactly which is the best answer here. I went with the CommandFactory originally, so I probably wouldn't change that, but I could just as easily see a method doing the same work. There is a case where the separate class has a distinct advantage: dependency injection. If I expected the CommandParser to need to deal with arbitrary command policies, I would make that dependency injectable through a class. Then the commands could be swapped in and out without any changes to the code (after making the dependency injectable). That ventures into speculation, though, and we need to be pragmatic.
As software developers, we see errors every day. They manifest as exceptions or segfaults or error codes, telling us that our code has gotten into a state we didn't expect. Their appearance often portends bugs. Though we groan at an unexpected stack trace, we should see an error as a form of automated feedback . Feedback can be fast or slow. We can put the discovery of errors onto a timeline. Errors appear at many points over the lifetime of the code, starting at the moment it is compiled. The further to the right that an error appears, the longer it takes for the feedback to appear. Compile time Compile time is the earliest we can receive feedback about an error. The compiler automatically does a number of checks to make sure the code makes some semblance of sense. The most powerful tool for compile-time feedback is the type-checker. The type-checker makes sure that only values of the expected type are passed around to the places that expect them. This guards ...
Writing code is all about making assumptions. Sometimes those assumptions are explicit, but more often we make those assumptions implicitly. We make assumptions about what kinds of parameters we will receive. We make assumptions about what kinds of values methods and functions will return. We make assumptions about the global state. The correctness of our code relies on the correctness of these assumptions. Bad assumptions can wreck our applicatons. We assumed that method never returns null, but turns out it does. Now we have an unexpected NullPointerException crashing through our stack. We assume that we will always average at least one element. Turns out someone wants to average zero elements. Boom, division by zero, ArithmeticException . We make so many assumptions through our codebases, at least a few are going to be wrong. Really, there are many, many assumptions that are wrong. These are bugs. These bad assumptions are all of our bugs. Usually, our assumptions are i...
As hurricanes approach the shores of North America, we are barraged with maps of possible tracks those storms can take. Those tracks start pretty close together, but diverge widely the further into the future they predict. Simplified, those storm tracks form a cone of uncertainty . A storm in the Atlantic could slide into the Gulf of Mexico and land anywhere from the Texas-Mexico border to Panama City Beach, FL. It all depends on a myriad of tiny factors to turn the storm this way or that. As time progresses, the storm moves along, and the cone of uncertainty shrinks. Its eventual location becomes clearer to predict, and we can more effectively target resources. Cone of Uncertainty for Hurricane Ike, source Much like hurricanes, running code lives in a cone of uncertainty. A function or method call is a mysterious black box until we have a result. If something goes wrong, we must delve into this cone of code uncertainty to find the problem. We use log statements and de...
Comments
Post a Comment