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.

Monday, February 21, 2011

The 'Emotional' Death March

No - this has nothing to do with holocaust or any such historic event. Rather, this is something a lot more personal and something that I am sure each of us will experience at some point in time - typically coinciding with our mid-life crisis (give or take a few years).

While what I am about to share with you is very topical, but I am sure you can relate with your own equivalent memories. With that in mind, read on.

You probably have heard of Buddha's life story in one form or the other. The gist of the story is that he was a royal prince with all the riches in the world and shielded from the miseries of life. One day he steps out of his palace and sees people at varied stages in their life and comfort - the poor, the ill, and the dead. At this transformational moment, he leaves his worldly attachments in search for the meaning of life and eventually attains englightenment.

While everyone is not necessarily a Buddha, I feel that we end up going through at least the first part of the story in our own way - the only difference being the Buddha and the rest is that we don't follow through - much like an amateur golfer and Phil Mickelson!

Many of us start our lives in a fairly well shielded environment. We experience illness and death here and there, but most are distant enough that we are not too emotionally affected by those events. As we grow up, we build our own memories during school and college and eventually culminating at our work lives. However, it is during our mid-life (well mid to late thirties anyways) that we come face to face with transformational moments - and in most cases, they are not related to 'blood' relatives but rather 'emotional' relatives - ones that we grew up with emotionally.

A classic case of such an emotional relative is John Lennon for those who grew up in the sixties and seventies listening to the Beatles and the impact that his death had on them.

My transformational moment happened a few months back on hearing the news of the death of 'Sujatha' (aka Rangarajan) a well-known and well-respected author / script writer in India (especially Tamilnadu).
Source: http://en.wikipedia.org/wiki/Sujatha_Rangarajan
Sujatha, an engineer by work and author by hobby was an amazing influence on those who grew up during the eighties and nineties in Madras (now Chennai) in India. He had an amazing knack of making science fun and in writing O.Henry-esque stories with ease. He was extremely versatile in writing and had an amazing range of interests which he willingly and eagerly shared with his readers.

He was one of the first 'emotional' relative that I lost and it was hard. I felt the inexplicable sinking, knot-in-the-stomach feeling when I read the news. It's when the reality of death struck me fairly strongly. I feel that these are the times when we come face to face with our own temporal existence and take a hard look at ourselves and what we have accomplished so far - probably the onset of mid-life crisis!

Recently, there was another such incident. While this 'emotional' relative - Malaysia Vasudevan, a popular singer in Tamil films in the eighties - was not as impactful as Sujatha, it did prompt me to write this article. It was not really due to my attachment with his songs but rather more due to one of the songs he sang and how it intersected with my life. You see, one of the more popular songs he sang (shown below), was shot in the auditorium of the school where I was studying during my junior high school days.


My class was adjacent to the auditorium so much so that the auditorium air-vent-window was on one of the walls. As any kid would, we crowded around the window and peeped into the auditorium and watched the set being constructed and shot being taken with curiosity and awe!

I am sure this is just the beginning and there are many more 'emotional' relatives who are going to go beyond the horizon over the next few decades. The question is whether I will go in search of the truth like Buddha or let the events numb me and go back to getting immersed in TV - what do you think?!

If you have a moment, share your first 'emotional' relative whose passing affected you personally via a comment. If you don't have one yet, stop reading this blog and go on collecting more 'emotional' relatives - you still have time!

Update: After I published the post, I have been corrected rightly that the singer for the song above is K J Yesudas and not Malaysia Vasudevan. Interestingly, I ended up viewing the song as a 'related song' after listening to one of Malaysia Vasudevan's songs shared by my friend Bala - I guess my mind generalized the singers by then and just kept the emotional content :)

Sunday, February 13, 2011

The Power of Abstraction

Abstraction is a fairly powerful concept and one that is most used and abused pretty much everywhere, especially within the IT industry. It is difficult, if not impossible to sit in a meeting without hearing the word being used at least ones, mostly incorrectly. The usage is normally more incorrect as the level of the meeting attendees goes up ('pointy-haired boss' anyone?!)

Surprisingly, abstraction is something our brains seem to be happy to handle. In fact, many a research has shown that the brain actually prefers abstraction a lot and uses it as a tool to reduce the amount of information that it needs to hold. One classic example is the Gestaldt diagram you might have seen elsewhere.

Source: http://www.freshbrainz.com/2007_06_01_archive.html
Sometimes it does not have to be complicated. Just a couple of strokes should be sufficient to recognize something, however abstract it may be.

Source: Wiki Commons
Like any concept, abstraction can be used for the good or the bad, or even something in between.

Source: http://www.premisemarketing.com/blog/post/got-to-be-real1/
I feel abstraction is quite helpful - it helps simplify things and help me understand the larger picture without having to know everything.

Abstraction has also been used for a long time. Ancient Hindu scriptures used a form of literary abstraction named sutras (or aphorisms) that condensed the meaning of a complex philosophical concept into a few words. Most likely such a mechanism was utilized due to the oral tradition that existed back then - necessitating a condensed format that was easy to remember and pass on to future generations, with the explanation left to the teacher. Such abstractions are similar to some architecture diagrams that I see on a daily basis, generic, somewhat useful, but does not make much sense without someone being available to explain it properly (on the flip side, this has been conveniently been used by business analysts and architects to reuse generic frameworks or concepts pretty much everywhere without much thought to whether it makes sense or not). Proverbs and fables are other forms of literary abstractions that you might have heard in your childhood from your grandmother.

However, abstractions cannot live alone. A good abstraction typically requires a supporting framework - a generic structure that can hold specifics. A classic example of a framework is the periodic table. Having a framework such as the table helped Mendelev find elements that had not even been discovered yet. A good framework - be it an architecture diagram or a process flow or a mind map can help identify gaps and help us move towards the 'right' state.

However, increasingly of late I have been seeing the darker side of abstraction - abstraction used as a means for confusing people or even simplifying things to an extent where the abstraction does not even make sense.

One of the greatest and most recent examples of abstraction being used in the most evil manner is the near collapse of the US financial market. The financial elements that were involved in the financial collapse were highly abstract in nature - most involving three or four levels of abstraction, so much so that the investor had no clue what he was investing in.

Other similar examples can be found in politics elsewhere, such as the Spectrum Scandal in India where the amount of money involved is so huge and so far removed from the common man that people are numb to the extent of the scam or the seriousness of it all.

On the other side, non-abstraction can also be equally bad. A great example is the US news media (which of late, is sadly being copied in emerging nations as well). This situation, perpetrated by the 24x7 news media, is where a simple concept that can be explained in 2 sentences is stretched out across the entire day or multiple days, providing so much redundant detail that the core issue is lost on the user. In most cases, even those who report the news get so bored that you hear them repeating the same questions over and over again, sometimes without even expecting an answer ("How do you feel about this?" "I am sure this is traumatic for you." "Did you feel bad?").

Hope this post was not too abstract for you to grab the idea - "Abstraction can be good or evil - use it for good."