19 February 2011

Simplification and Generalization in Problem Solving

You scatter toy towers of varying heights on the floor and ask some five old kid to arrange the towers in increasing order of heights. Most likely, he is going to knock of the puzzle. His first problem (and yours) will be understanding the problem - what needs to be done. Once he understands, he will complete the puzzles with your help here and there.After sometime, you ask him to sort toy circles in increasing order to circumference. Most likely he is going to finish off this taking lesser time than his previous. 

Both the instances are specialization of sorting - a basic algorithm that is known to human being. Human beings were sorting objects in life much before sorting algorithms were put forth. All those sorting were merely specializations and they work in specific scenarios but not all. Rather than saying it won't for all scenarios it is better to say that it was not tried having "all scenarios" in mind.

It is when we think about "all scenarios" we forget the specifics and the properties that is unique to the given problem and start to think about the "commonalities". When we think about "commonalities" we abstract ideas/concepts and bring many levels of abstraction based on the "commonalities". The specifics that are known at the time of abstraction forms as a boundary condition for abstraction. So, it is also very important to think widely about a specialization so as to make the abstraction more flexible & accommodative. Moving from specialization to a broader generalization is possible only when we simplify the problem domain and think about "many scenarios".

In order to understand the problem, you have to travel towards specialization and in order to solve it after understanding it requires simplification and generalization (and for achieving this you have travel further in time, by time i mean experience).

In next post, we will see some of the ideas on generalization from neuroscience's point of view.

16 February 2011

Software Design Puzzle #7.2 - Thread Pools & Tasks

Please refer to previous two posts on the same problem - Implementing Thread Pools and Tasks. Here is the link for your convenience.
Yesterday, i added a requirement that i want to have a priority for tasks (a problem on data structures and algorithms). Today, i thought about another feature that gives a lot of flexibility (real OO design). In the above examples, we didn't talk much about the threads in thread pools. Can we try to make threads in thread pool dynamic entity - based on the need, the number of threads in thread pool should grow or shrink. Here are my next set of questions.

  1. What are the design decisions that i should take so as to make thread pool dynamic.
  2. How can i make threads, tasks, thread pool at the topmost level of abstraction and yet get many different concrete implementations.
  3. How can i ensure that the code I am going to develop after two years (due to new requirements) doesn't affect my code now (seal the code from modification for new requirements)
  4. How can i bring in hierarchy, levels of abstraction and modularity for better design?
  5. Do i have any design patterns?

15 February 2011

Software Design Puzzle #7.1 - Thread Pools & Tasks

Refer to the previous post, Software Design Puzzle #7 - Thread Pools & Tasks. This post is an addendum to the previous post.

I m going to add one more enhancement to the thread pool design. The enhancement is to come up with a priority queue for tasks. Each task will have certain priority, an integer that increases as priority increases. At any given time, the task with highest priority has to be selected and run by threads in thread pool.

Can you refactor your design?

13 February 2011

Software Design Puzzle #7 - Thread Pools & Tasks

We know what is thread. The thread is an independent execution path or entity in a process. When you want to exploit multi-core (increased processing) or do some blocking operations, the thread is the default destination (increased interactivity, heavy input/output operations). In any programming language that supports multi-threading, you run the code that can be run as independent execution path as thread with each having its own context. The infrastructure that is needed to run threads in parallel like stack, program counter, registers are tightly coupled with the code that runs. Due to this tight coupling, each time you need an independent path (let us call it as task), you create a thread (the infrastructure) which has its own overheads. Is it possible to change code of the thread?

Obviously the next step is to separate out the infrastructure and the task which is achieved using thread pools. Here is the next problem.

Write a simple thread pool in Java with following requirements

  1. Have a fixed but configurable number of threads in thread pool. At any point of time, you can run up to N threads.
  2. Have a task queue (or a suitable data structure). This can be much bigger than number of threads. The tasks wait in the queue for their turn.
  3. The thread should consume tasks from the task queue and execute it. It can be any task (but you have to ensure some fairness). The role of the thread is to run task without bothering too much on what it does and how it does.

06 February 2011

Puzzle - Help the Painter

Your friend is a painter and he paints houses for quite sometime. He is highly professional and highly sought after as he known for his quality work and fair painting charges. Throughout his life he has painted lot of houses that are regular sizes - rectangles, squares and circles. He charges his customer based on the area he paints. (you see calculating area for regular shapes is cake for him)

Last week, a new potential customer called him up for a huge order that will keep him and his team busy for next five to seven months. The catch here is that the customer is not willing to pay a penny more and even your friend being a professional painter wants to quote a fair pricing strategy to his new potential customer to win this order. BTW, did i tell you that the area to be painted are irregular shapes.

The painting area is

  • Irregularly shaped like amoeba
  • Each surface is uniquely shaped (bare minimum he needs to paint 1000 such surface different in 1000 ways)


What is the pricing strategy that your friend can offer to his customer so that both your friend and his customer is happy.

03 February 2011

Organizational Entropy

Entropy (thermodynamics) is the energy that is not available for any useful work. Entropy in software measures the degree to which it is unordered, degree to which it can't be maintained. Entropy in organization measures the degree to which the workforce is unfocused & unordered. Though the field of application is different, by the very definition we understand that entropy is not a thing to boast about. The system continue to go in favor of entropy unless it is taken out in the form of heat.

Entropy in organization implies that energy that is available and that will eventually be spent on matters that is not so useful to the organization either be it taking too much of tea for a prolonged period in cafe or putting up a strategy in a conference with tea (or beer). All these are efforts which won't necessarily be converted to results. The next question that may come you mind - "is entropy same as that of being unproductive?".

Being unproductive is the result of having an entropy at higher side. Productivity is a metric that is measured after the event has happened. You have to wait for a week, a month, a year or until you have next meeting. But entropy measures how much of energy you have and what portion of it will be spent on things that will have little to no impact.

I feel like measure on entropy, and trying to reduce it, is a proactive approach than doing with measuring productivity alone. You better remove entropy periodically. Keeping a check on entropy increases the awareness and anticipation which in turn increases the probability of producing productive work/results.

Either in software or organizations, the entropy has to be worked against.

Related Links

31 January 2011

Can't Find a Better Leader, Be one

Often we complain that we are not able to find a perfect leader. The word perfect is the word that denotes excellence and such a leader cannot be found. Even found, the perfect leader never accepts that she is perfect as the excellence is not a destination. Let us come to the complain part. We generally tend to have a lower expectations and want the benchmarks to be lowered, tweaked until we get in. Once we get on board, we generally argue in favor of excellence and raise the bars. We clearly eliminate our roadblocks in the name of eliminating inefficiencies [that creates double standards]. Pretty weak way of looking either life or work.

How many times we thought that we have to be a better leader? We stop our thinking just with wanting to have a perfect leader. If you can't find a perfect leader, it doesn't mean that it can't be made, it just means that nobody has guts or conviction to be a perfect leader. Because, trying to be a perfect leaders hurts a lot and pains a lot as we need to work a lot and sacrifice a lot. The building cannot be raised without foundations and the leadership cannot be built without sacrifices. Sacrifice and see what happens. That is why people say, "the leadership starts with self".

If you haven't found any perfect leader in front you, go ahead and try to make yourself a perfect leader. The leadership is nothing to do with power/positions/designations and it is more to do with your virtue of thinking & action.

If Mahatma Gandhiji said, "Be the change you want to see", there has to be some reasons. :-)

30 January 2011

Java Threads - Why you can't start a thread twice?

Assume the following code
class Mythread extends Thread {
          }


          Mythead thread = new Mythread(); 
          thread.start(); thread.start();

Why you cannot start the thread (i.e calling thread.start()) twice? Why JVM panics when you do that?

26 January 2011

Why Loose Coupling is Strong Coupling?

Often, senior programmers insist us to have a loose coupling. In order to appreciate those words - "go for loose coupling", we need to understand what does it mean by loose coupling.

There is another good practice encouraged by senior developers which is modularity. Often, they want us to write function or methods which is just a screen's length (by the way for 14 inch monitor :-)). How can loose coupling be possible when you have modularity? When your application is highly modularized, you heavily depends on many modules to accomplish things.

The concept of modularity in fact leads to loose coupling. By modularity, we make a part of the code to do one critical thing. We don't want any part (or technically a unit) of the code to do two things. We want to restrict to a critical thing and by modularity we also want to hide the gory details underneath. When modularity is practiced in a structural programming, we tend to get loose coupling in a structural language/programming and when we follow right abstraction in object oriented paradigm, we get right loose coupling in object orientation. The modularity mean do one thing that is appropriate to the level of abstraction.

The loose coupling is not restricted to a specific programming types but rather has to be seen as a concept. The application of concept can be different in different programming language types like modularity, abstraction (having hierarchies of abstraction, each level of abstraction marries to a same level of abstraction to realize the functionality).

So, loose coupling helps us to realize strong coupling between objects but avoids tight coupling (which is a code smell).

What do you think?

21 January 2011

Paradigm Shifts - Function of Potential, Action and Time

Who is responsible for transforming an organization? Is it God, the organization itself, the CEO, the strategy that is penned by business leaders or who else? I don't think neither the God nor the CEO can bring about the change. The God or the CEO can only give you direction and can minimize intrusion. 

Before reading further, i would like you to watch the following video by Prof. C K Prahalad on leadership and my frequently viewed video.


What will you think when you see an egg? Will you think that there is a potential hen which again has the potential of creating billions of hens? Or will you just think about having the breakfast? Assume that a fresher stands before you. What will you think? Will you ever think that he is a potential CEO of your company and create so many transformation? Will you think that the guy who stands before you is manifestation of many paradigm shifts?

Paradigm shift is gradual, it takes time. It is zillions of consistent tiny improvements. All such tiny improvements make a transformation. Transformation is a function (product) of potential, action and time. When any of these is zero, the total transformation will be zero. There won't be any paradigm shifts without potential or action and there is no instant transformation (without time factor).

The guys who undergo transformation should keep an eye/heart on "potential and action" part of transformation and the guys who facilitate the transformation should keep an eye/heart on the "time" factor.



18 January 2011

On God and Religion

The belief of God has to be a personal thing - you follow one or don't believe it. For those who believe that there exists a God - you never saw Him, you never heard Him and you never follow what you believe He said you to follow. You will continue to preach irrespective whether or not God appears before you. God has to prove that he is God even if he appears before them. Quite materialistic. They just believe what they believe but not believe enough. If they learn to simply recognize Him, they will simply recognize God in them, you, me and anyone who stand before them.

Another view is the view of believers world. Believers believe that their portion of elephant is real elephant and they didn't explore other areas of elephant. The religion they portray is their perception. They are atheist with respect to other religions. The man has transformed this world a mess, even if God appears he would think to destroy this world rather than brining in order. The entropy is so huge now and tomorrow it will little more than today.

The state of non-believers is more pathetic. They never saw God and they tend to believe there isn't a God. They have a stigma that they are rational and they are so much glued to it like the believers. They may be rational but not rational enough. The rational thinking doesn't help to reduce the entropy rather adds to its growth. My three old nephew is so intelligent to believe that there exists a country called Canada and he believes that one day he will travel (just for sight seeing) to see places and in order to do that he has to study well and conduct well. It is the same belief that astronauts have on an alternate earthlike planet. If a belief gives you a good feeling, put you on the ethical orbit, makes you to find an inner piece, make you progressive and make you to handle uncertainty, you got to believe that it exists even if your cognition says the other way. The thing can be God or love or something else. Belief is superior than the fact. 

The belief shouldn't be a blind one and rational shouldn't too rational. The believers should learn rational thinking from rational thinkers and the atheists should learn the importance of belief.

The very argument whether or not God exists is insane and inappropriate.