Understanding Cohesion - Grasping at the abstract

Thinking in the abstract is absolutely nessesary for a developer. We constantly deal with abstract concepts, weather you know it or not.

Unfortunately many times we dont recognize concepts, but rather just think of code as “If this… then that.. If this… then that.. else this…” and your increadibly long chain of “logic” builds up in your memory until… Finally! You know where to wirte the next line of code. It’s coding like this.

alternate text

When this first became popular I was a little annoyed. I was already a decent programmer, and my first reaction to this was “Yeah, I used to code like that… right out of school. When I did not know any better!” That great big long chain of “logic” should have been broken up into more cohesive abstractions.

When coding’s abstract nature becomes just a little bit more concrete… code feels more like well architected building. With rooms, doors, hallways. Perhaps a courtyard, and a shed. It all makes sense. You know where you are! You don’t “get lost”

We humans deal in the abstract ALL - THE - TIME! Langauge is pure abstraction. Our linguistic faculties are amazing! Children learn to understand what is going on and speak without even trying.

Every concept is “abstract” when we first come across it. Some things can be harder to understand then others, but once we “get it” we can we use the abstraction without even thinking.

For example.. Driving a car. At first it may seem scary. When you were a child you may have been amazed at how your Mom and Dad could do something like that. Yet you learned to drive a car. You learned what every switch, button, leaver, light, slider represented and how it affeted the car. But do you think of any of those things now? or do you just drive a car? Sometimes I get in the car to go home, and I get there on “auto-pilot”. Without even thinking about it.

Abstractions become “concrete” once we “get it”. Once we can name it. Talk about it. Reason about it. But if “it” is never identified… you will just end up with long chains of abstract “logic”

Another example. Lets start with an abstraction we are all familiar with: A Kitchen. What is a kitchen? How would you answer that? Perhaps you will say “It is a place where we store, prepare, and consume food”. What is “storage”, “preparation”, “consumption”? Well, we store the food in the fridge, we prepare the food on the counter, we consume the food on the table with forks, spoons, knives, plates, bowls”. We dont have to define all these things. We know what they are.

Now imagine the Code Kitchen.. all of this stuff is in our program. We hire a new developer who vaguely knows about Kitchens. They may even know what food means. They start contributing to the code base and one morning you pull down the latest Kitchen and YOU FIND A TREE IN THE MIDDLE OF THE KITCHEN!

Now, you kind of chuckle to yourself… you can understand WHY the new guy thought it might be a good idea to put a tree in the kitchen. Its got food on it, We consume the food. Two things we do in the kitchen. But there happens to be one glaringly obvious problem TREES DON’T BELONG IN THE KITCHEN! It’s obvious to us, but it may be kind of hard to explain why the tree belongs outside in the garden. The tree needs sunlinght, it needs soil. It won’t magicaly get nourishment from the theater in the basement!

You try to explain to the new guy why the Tree should probably not be in the kitchen, but in the garden. If these concepts are simply foregin to him, he will probably just blindly do what he is told because YOU obvioulsy know where all this stuff goes. Your “Home” (code base) is well structured. Your concepts are clear and understantable. The toilet is in the bathroom. The Car is in the Garage. Everything is where it is to benefit the users of the home. It’s structured after the occupants needs and behaviors.

What would a Home look like if it were organized poorly? You know… similar to some code bases you may have seen. I have seen code bases organized by architectural pieces (think MVC applications) or organized by their storage mechanisms, or communications protocols. In my oppinion even structuing the code after the “departments” in the corporation is just plain silly. You might as well organize your application by the colors of the rainbow!!

A home organized by “communications protocols” would have a room for everything delivered by the various carriers. The UPS room, the FedEx room, the USPS room. If the Dishwasher was delivered by Sears, it would be in the sears room. The toilet was delivered by DHL… so of course we should put it in the DHL room. If you were to try to “Prepare a meal” in this Home… you woul have to go to the UPS room to get the utensils, then you would have to go to the Family Vehicle room to get the food (because you went shopping for the food in the family minivan), then you head over to the Sears room to start cooking on the stove. You might get used to this arrangement… and say “We should optimize things a little better and only buy our appliances from Sears. I hate going to the RC Willy room any time I need to use the microwave” AAAAAAHHHHH! that is now how you optimize the situation. YOU HAVE BUILT YOUR HOME ON THE WRONG ABSTRACTIONS TO BEGIN WITH!!! You placed embeded “carrier” information as a first class citizen into your home. Carriers have NOTHING TO DO WITH THE ACTIVITEIS DONE IN THE HOME. The occupants of the home need things organized for their needs and behaviors. The “Carrier” organziation added sooooooo much overhead to performing tasks. so much so, that the taks itself was obfuscated. The users knew what “preparing a meal” was. But you would know know that is what it was used for from the structure of the home!

What would a home look like if it were architectural components were the driving factor?

What would a home look like if everything had be be organized by it’s “storage” mechanism? What would that even mean? Would my bed be a coffin? We would probably need BIGGER drawers!

Concepts that belong in your code should be derivied from the top down. What the users need. How the users behave. Processes the users naturally follow. Add those “concepts” into your code, and you will have a well structured “Home”