Search This Blog

Monday, November 07, 2011

Coupling and Cohesion in Software and Business

One of the important topics in Object Oriented Programming that is often overlooked or not even considered is that of Coupling and Cohesion. A sound understanding of these concepts can make a huge difference in how you package functionality within your program and how flexible your program can become for enhancements.

When performing source code review, keep coupling and cohesion in mind and ensure that the classes within a package all perform same or closely related activities (high cohesion) and that classes within different packages don't refer each other too much or at least in a well-defined manner (low coupling).

Definitions


Coupling is how inter-dependent two functional components are within your program. High coupling is bad, because if you change one function, you might end up affecting all the dependent functions as well.

Cohesion is how closely the parts within a function work together to make the function as one single, well-defined unit. High cohesion is good, since you can treat the whole function as a black box, thereby abstracting your system for better clarity.

Coupling Types

Type (worst to best) Description
Content/Pathological Coupling When a module uses/alters data in another
Control Coupling 2 modules communicating with a control flag (first tells second what to do via flag)
Common/Global-data Coupling 2 modules communicating via global data
Stamp/Data-structure Coupling Communicating via a data structure passed as a parameter. The data structure holds more information than the recipient needs.
Data Coupling Communicating via parameter passing. The parameters passed are only those that the recipient needs. No data coupling : independent modules.

Cohesion Types

Type (worst to best) Description
Coincidental Cohesion Module elements are unrelated
Logical Cohesion Elements perform similar activities as selected from outside module, i.e. by a flag that selects operation to perform. That is, body of function is one huge if-else/ switch on operation flag
Temporal Cohesion Operations related only by general time performed
Procedural Cohesion Elements involved in different but sequential activities, each on different data (usually could be trivially split into multiple modules along linear sequence boundaries)
Communicational Cohesion Unrelated operations except need same data or input
Sequential Cohesion Operations on same data in significant order; output from one function is input to next (pipeline)
Informational Cohesion A module performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data structure. Essentially an implementation of an abstract data type
Functional Cohesion All elements contribute to a single, well-defined task, i.e. a function that performs exactly one operation

Sunday, October 23, 2011

Goodbye World!

I am glad to have seen him in person even if in passing - he was a great inspiration to a C of developers.

#include <stdio.h> 

int main()
{
   /** Author: Dennis Ritchie */ 
   /** You are expected to understand this */
   printf("Goodbye World\n");
   return 0;
}
RIP Dennis Ritchie!


Wednesday, October 05, 2011

Rest In Peace Steve Jobs!

Almost everything--all external expectations, all pride, all fear of embarrassment or failure--these things just fall away in the face of death, leaving only what is truly important. Remembering that you are going to die is the best way I know to avoid the trap of thinking you have something to lose. You are already naked. There is no reason not to follow your heart. 
Steve Jobs


It's a sad day for American innovation and the Tech World - an icon has passed away - an icon who was a phoenix in the Tech world, who rose out of the ashes to become more powerful than what he was before.

Much as I am not an iFan, I admire the creative genius and charisma that is Steve Jobs. It is rare to see charisma and genius combined into one package, and Steve Jobs was one of them. He was the man with the Midas touch over the last couple of decades with both Pixar and Apple - creating products and films that were blockbusters every single time.

We salute you, Steve!

Monday, September 19, 2011

Curse of Knowledge

It's been a while since the last post - unfortunately blog updates is the first victim in a busy schedule.

A few years back, I read the book Made to Stick by Chip and Dan Heath. It's a very interesting book and the authors talk about how to make ideas stick in people's minds. The crux of the book is that in order for an idea to stick, it must have 6 elements - Simplicity, Unexpectedness, Concreteness, Credibility, Emotions, and Stories. More on that at a later date. In the course of the book, the authors also make a reference to something called the "Curse of Knowledge". While the idea has been around a while, this name stuck in my head.



You might wonder - why is knowledge a curse? Isn't knowledge what every person and every organization trying to achieve? Wouldn't having more knowledge make one wiser and more blessed? Why would it be a curse then? Does it even make sense in a knowledge-oriented world?

In this case, the reason why knowledge is a curse because once you learn something, it is difficult and in many cases, impossible to unlearn it, thereby making it harder for a knowledgeable person to explain how he/she gained the knowledge to a layman.

If you think about it, this is a very profound issue and happens everyday in our lives. Once you know how to do something it's harder to explain it to others who have not yet been enlightened by the knowledge.

Imagine trying to explain how a particular piece of code you've developed works to another developer, or how you came up with a specific design to a manager in non-technical terms. Remember the crappy documentation you've complained about time and again. Some cane be traced to laziness, but others can indeed be traced to the curse. It's very difficult for us to let go of the knowledge gain and revert back to a previous position - there is no "undo" button in life.

Apart from making it difficult to communicate to others, the curse of knowledge has a number of other side effects as well. It fosters preconceived notions and sometimes makes it harder to see the facts as they are. As Sherlock Holmes said, "It is a capital mistake to theorize before one has facts. Inevitably, one tends to twist facts to suit theories instead of theories to suit facts" and sometimes having too much knowledge can make one do just that.

Every curse typically has a remedy in stories. So, is there a remedy to break this curse? Not really because of the above mentioned lack of "undo" button! However, I feel there are a few ways in which the curse can be relaxed a bit - enough to carry on with our lives. Here's my quick list.

Put yourself in a child's shoes
One of my favorite comic strips is Calvin and Hobbes - a true American classic. While each piece is a gem, my favorite is when Calvin's dad explains science to him. Before you create a presentation or a document especially to a manager or a non-technical person, imagine how it would read from a child's perspective. In most cases, out will go all the acronyms, jargon, and assumptions you've made. This can be a great way to help simplify the message. This works even better on simpler things like email as well.

Imagine you are making a move out of it
When directing a movie, you have to make sure it reaches all forms of audience - both elite and the non-elite (if you want to make big money, at least). That means your story has to be simple, crisp, and easy to digest. It can have a complex story as long as you are able to explain in a way the viewer can comprehend. Imagining that you are directing a movie on the message you want to tell can help you simplify your message and remove a lot of fluff that you'd have otherwise added because you just don't want to miss out on any detail and want to say anything and everything.

Develop an analogy from a completely different domain
I have found this to be helpful in many places - try equating what you are trying to convey in a completely different field like say, music, or arts, or something seemingly unrelated. This is something that can come closest to the reversal of the curse. By trying to translate your known knowledge into an unknown domain, you are essentially putting yourself in the layman's shoe - in essence, reliving your lack of knowledge, which can be a great way to understand how the other person might perceive your information.

One very pronounced area where the curse of knowledge is prevalent is in the blogosphere. After all, blogs are about sharing knowledge one has gained - be it a tip on a software, a book you've read, or a issue you faced. Providing enough context before conveying the message and easing the viewer into the message can go a long way in them empathizing with your view.

PS: A personal appeal
I have seen this especially pronounced in blogs on Sanskrit or Hinduism, where in many cases, it's not a curse of knowledge but rather a 'brag' of knowledge. Eager viewers are turned off by generously sprinkled Sanskrit with no explanation whatsoever, breaking the flow of the message and distracting readers. It only takes a few more minutes to add an explanation - even in a footnote - on what the phrases mean to demystify the message. If you go through all the trouble to write a post, why not go just that extra mile to add some additional detail? After all, these messages are intended to last beyond the time when it's written - might as well make it stand-alone! I hope they break or at least relax the clutches of the curse and shed some more light.

Thursday, February 24, 2011

Illustrated stories and an illustrious writer

No sooner than three days since I wrote my last post about the start of the 'death march' of my emotional relatives, the news came today of the demise of another illustrious member of that group - Mr. Anant Pai.
Source: http://en.wikipedia.org/wiki/Anant_Pai
Most outside India (and sadly most inside either) may not have heard of Mr. Pai but he has been a driving force in the life of children in India over the last few decades. You see, Pai was the creator of a number of comic books in India, the most popular series of them being the Amar Chitra Katha and Tinkle. The books were aimed at the 8 to 14 year old group, the key age where children start understanding the world and start shaping their moral character.

The story of how Mr. Pai started his career in this field itself is fairly interesting. The story goes that the idea behind starting a comic book series devoted to Indian culture and history came to Pai from a quiz contest aired on Doordarshan in February 1967, in which participants could easily answer questions pertaining to Greek mythology, but were unable to reply to the question "In the Ramayana, who was Rama's mother?" Pai actually did something about this. He dipped into this huge treasure trove of the folklore both in India and elsewhere and converted the gems into pictorial stories in a way that could be easily digested by kids. His significant work in this field is probably the single biggest contribution in converting the traditional oral tradition of India into the more modern written format.

I have spent countless hours during my childhood days devouring many of these stories. I was happy to see that after a lull these comics are being reprinted in India and are selling like hotcakes. It is not easy to convert a story into a pictorial form without losing its integrity. Pai was a master in doing this and his creations have been cherished over the years and am sure will remain in readers' memories for years to come.
Interestingly, I am seeing the trend of visualization crossing the boundaries of 'kid magazines' and into the business world. Recent books like Back of the Napkin and Visual Meetings are bringing this concept into the business world. It takes a lot of effort to simplify a complex concept and explain it elegantly. Being visual beings, we understand and process pictures much better than words, as we are naturally born to do so. One of my hopes is to master my visual craft further and do something in this space. If you have enjoyed reading comics - be it by Pai or others - and love the simplicity and expressive nature of the medium, I encourage you to read Scott McCloud's Making Comics, where he describes the art and science of this beautiful medium.

In the meanwhile, the death march continues... My prayers for Mr. Pai and his family - for boldly going where no one dared to go and for preserving the treasures of the past for generations to come.