Enter your Email Address:

Delivered by FeedBurner

To get posts delivered to your Email

31 December 2011

2011 - Looking back & Looking forward

I never intended to write something describing what i did the past year. But a quick thought arose now. I have two things to share that i feel so happy about it. One that happened at work and another in life (i should say internet life, as each of us have two personal lives - one in internet and another ordinary/common life :-))

By the starting of 2011, i took up something at work and i feel that though it is not super hit, i realized that i m playing the game to my strengths. All the situations thought me that i should always be level headed. Sometimes, i felt the situations are custom crafted to make by temper to reach its tipping point. Unlike my usual thing, i somehow played by emotions low. Towards the end of the year, i really liked the way i handled things. I only hope that i should improve from here. It was an year of low profile and great learning (particularly the time i spent with fresh professionals just out of college).

Next, let me talk my internet life. When you want to know about something, i think usually we hit Google and most likely the first link would be LinkedIn and Facebook (if you are searching for people) and Wikipedia if you are looking for informative articles. Wikipedia is beyond Encyclopedia and it gives us authentic information. This year i contributed towards Wikipedia fund to keep it for free and i m looking forward to next year (and hopefully, i m planning to double my contributions). When you have a great place to look for information and when the people wiser than you writing/building it, it makes sense to contribute and it is wonderful feeling being part such an initiative.

For the next year, i think i should blog more (with interesting topics) and do something that makes me feel that i m constantly stretching my limits.

I constantly think about this blog and my readers who constantly make me better everyday and each time i blog.

For all the wonderful readers, wishing you a very happy new year. I m looking forward for the new year to learn more. Together, let us learn more.

30 December 2011

Now, it is Brain Rules

For the next few weeks, i m going to read two books which i think will have great impact.

I have already wrote about the book Predictably Irrational here and here. (not in an any journal or magazine, these links are below the current post :-)) I feel that i should read the book again at slightly slower pace.

I just read a chapter in another book which i found equally fascinating and has great insights. The book "Brain Rules" by John Medina. The book talks about various principles on how to keep our brain fit. I think, the book will be useful for those of us who use it. So, first, i got to use my brain in next few weeks and all the principles and then come here to write another review.

If you want to hear the author, here is the website and videos.


If you have noted, both the books are written by researchers and you could see all their points are backed by solid research.

Hope that i start using my brain from next year and then rule it :-)


29 December 2011

Predictably Irrational Again - Video

Again, it is predictably irrational. But this time it is video. Watch the following video of Dan Ariely speaking about predictably irrational.

I think, from the whole of this talk, i tend to like the portion he talks about cheating.

Even though we have thousands of mirrors in us (and conscience), we still cheat (for example, how we cheat insurance companies. I think, the insurance companies know this and again they cheat us the following years). We don't bother whether it home, street or work, we are going to fudge to an extent that will make us still have a great self image.



If you have the book, the above video could be a companion and makes the reading/understanding a lot easier. While watching this video, i could recollect whatever i read and i m going to read the book again for the insights.

This is one of the books that i thoroughly enjoyed.


 

27 December 2011

Predictably Irrational - New Perspective

How many of us make decisions everyday and how many decisions we make everyday?

Do we apply common sense before making the decisions and how often we regret for the decisions we make. How many times in our life we fool ourselves that our decisions are in fact correct. How often we justify the decisions we make and why do we want to appear intelligent to ourselves (leave alone trying to be intelligent in front of others). Is there a reason why we want to be a super man in a group and boast about our contributions or things we own.

 The book Predictably Irrational by Dan Ariely talks about the forces that shapes our decisions. The book talks about various factors that shapes one's thinking fully substantiated with a lot of research work conducted on real people. The experiments that are discussed and the conclusion drawn opens up a lot of learning on how we make certain decisions at certain conditions and how quickly we change to a different decision (a true 180 degree) when the conditions are changed only a bit.

I really like the way in which the experiments are presented and the presentation of experiments by itself is a great learning. If you read the book, i feel one will get at least couple of value adds (how to conduct experiments for research, how to present them and how to draw conclusion from the experiments) apart from knowing a bit on decision making.

If you find the book in your library, I would recommend you to give it a glance.

While many of us thinking of starting the new year with new resolution and trying to be better, i feel so happy about reading this book and the way i m bidding farewell to 2011. I think, this book is a good investment i made in 2011.

Wish you a happy new year

.

16 November 2011

Puzzle - Mega Substring

Scenario #1
Given two strings - string1 and string2,  write a program to find whether string2 is a part of string1 (substring of string1). The above puzzle may seem to be very simple as it is. If this sound silly, read on.

Scenario #2
Assume that you have to write above substring program to compare the contents of two files whose sizes put together is more than the size of your RAM + SWAP partition (let us say 10GB of contents). If you feel like you can solve this with bit of try, move on to the next scenario.

Scenario #3:
What if I want to exploit the number of cores? Will it be better if I have few threads to do the job. If i choose to go with multi-threaded way, what are the special conditions that i should take care of?

Watch out this place for answer (probably this weekend - 20/Nov/2011)

05 November 2011

Time Server - Network Programming

Happen to flip first few pages of Unix Network Programming by Richard Stevens. Wrote a simple time server and client.

The server listens to specified port for incoming connections and sends the current time in seconds since Epoch (midnight of 1-1-1970 UTC).

The client is written to connect to the server, gets the time and displays it.

Here is the screenshot of the output (server is started as background process, netstat shows the server is listening in port 12000. The client connects to server and gets the current time).

The source files (with comments, so that one can follow) are uploaded to my Dropbox account. Sharing it here, just in case if you want to look/run the piece of code.

Server Source
Client Source

Compile the above programs (gcc client.c -o client & gcc server.c -o server) and run it as shown in the pic.

02 November 2011

Palindrome in Haskell

This is probably a week after i started to read Haskell (a functional programming language). Having tried few examples from the book and writing few on my own, i feel like it is a different world. If i m successful enough to keep my interest with Haskell and continue to practice, i think it is going to improve my thinking at a very least. I m not so sure of using this language at work (but who knows). More on Haskell later.

Here is the code that says a given String (list) is palindrome. (see, how expressive Haskell (FP) is). The first two line is to reverse the list and the last line is the one that decides whether or not the original and reversed string same (you need to write probably 10 lines in imperative language)

rev [] = [] 
rev (x:xs) = reverse xs ++ [x] 
palindrome x = (x == rev x)
 Here is the sample output (from WinGHCi)


*Main> palindrome "test"
False
*Main> palindrome "madam"
True
*Main> palindrome "123321"
True
*Main> palindrome [1..100]
False
*Main> palindrome "a great good haskell"
False
*Main> 

You can find my Haskell notebook (and my daily progress) here. (thanks to Dropbox)

29 October 2011

Scala - The Scalable Language

I stumbled upon the programming language Scala few months back and haven't got a chance yet to work with Scala (just download Scala last night and installed Eclipse plugin in my laptop this morning). I happen to watch a video on Scala by Martin Odersky. It is a great presentation - both the language and the presenter.

If you are looking for some alternatives for Java, check out Scala. Companies like Twitter and LinkedIn are using Scala in their products and seems like the list is growing day by day.

Scala is both object oriented and functional language and the code compiles into bytecodes which runs on a Java Virtual Machine. The main advantage of writing Scala program is that you will end up writing lesser amount of code as Scala is more expressive than Java. The adoption of Scala is in early stage and i hope (should say, rather want) it replaces Java.

You can find more stuff on Scala from here.

Overview on Scala, might help you to embrace Scala - http://www.youtube.com/watch?v=zqFryHC018k
Geeky Stuff - http://www.scala-lang.org/node/198
Scala Home - http://www.scala-lang.org/

Needless to say, i m so confused on whether i should blog about Java or Scala :-)

28 October 2011

Measuring Time to Minutest Precision With Guava

Other than profiling, have you ever encountered scenarios where you wanted to measure elapsed time from a predetermined past. A simple way of doing is to log the time (milliseconds or nanoseconds) into a long and then find the difference between two points. Here is a simple code.

public static void timer() throws Exception { 
long startMillis = System.currentTimeMillis(); 
for(int i = 0; i < 10; ++i) { 
Thread.sleep(1000);//equivalent to some work/logic 
long endMillis = System.currentTimeMillis(); 
System.out.println("Elapsed Time = " + (endMillis - startMillis));
// if i need to reset the start time, i need to do the following 
// startMillis = System.currentTimeMillis();
}  
}

If you are looking for only time difference between two points, you don't need to actually log the current time. It is enough if you start a clock (your own clock may be) at time t1 and stop it at time t2. If you want to measure time, you end up writing boiler plates. Guava has a little cute API for this.

Stopwatch is a simple API that mimics the functionality of a stopwatch. Here is the reincarnation of above code example.


public static void niceLittleAPI() throws Exception { 
Stopwatch stopWatch = new Stopwatch();  
stopWatch.start();  
for(int i = 0; i < 10; ++i) { 
Thread.sleep(1000);//equivalent to some work/logic  
System.out.println("Elapsed Time = " + stopWatch.elapsedMillis()); 
// if i need to reset the timer/clock, i need to do the following 
//stopWatch.reset(); 
// and my timer resets to zero
                                 // and if you want to stop the Stopwatch, just call
                                // stopWatch.stop();
}
What are the other advantages of using Stopwatch. I think it is an advantage of Stopwatch that it hides completely the mechanism it uses to keep time and in fact the user of the API is free to give his/her own Ticker.

Another nice addon is that you can get the elapsed time in days, hours, minutes, seconds, milliseconds, microseconds and nanoseconds which becomes very handy at times. A word of caution, Stopwatch is not thread safe and you have to figure it out how to make it thread-safe :-).

In order to understand the full power of Stopwatch, you may want to read Javadocs of Stopwatch, Timer and TimeUnit.

In my next post, we will see yet another Guava class and its use.

You may also want to check my previous post on Guava's Postconditions.

27 October 2011

Preconditions - (Java) Guava Collections Library

Fail early. It is applicable in life and programming.

The methods (or functions) are inevitable part of your code. The methods operate on the objects (or state of the system) and has a logic to do. Sometimes, the logic cannot (or rather must not be applied) due to non-availability of data (objects) or data being in an inconsistent state.

Let us take simple IPv4 address validator. Here is the code.

public static boolean isValidIPV4Address(String ipAddress)
{  
boolean isIp = false;
StringTokenizer tokens = new StringTokenizer(ipAddress, "."); if(tokens.countTokens() == 4)
{
// logic to validate ip address
// set isIp to true or false;
}
return isIp;
}

The above code is potentially dangerous if null is passed into the method. It crashes and brings everything to grinding halt. We can fix this by just adding a null check (and we have been often told to do that but we hardly follow it). The code will look something like this.


public static boolean isValidIPV4Address(String ipAddress)
{
                if(ipAddress == null)
                    throw new NullPointerException();
boolean isIp = false;
StringTokenizer tokens = new StringTokenizer(ipAddress, "."); if(tokens.countTokens() == 4)
{
// logic to validate ip address
// set isIp to true or false;
}
return isIp;
}

And thats how you fail early. You validate the inputs (irrespective of the number of arguments) and flag/fail early without changing the state if the arguments (inputs) sound weird. If you still operate on the data that is passed and change the state of the system, you add more pain by pushing the system to an inconsistent state. [Read Effective Java by Joshua Bloch if you haven't read it yet and if you want to be told by a Java expert]

If you think above is a good way, hold on. You have better way.

Guava (Google collection library) has a better way to handle this by using Preconditions. Replace the null check with one line statement and bingo Guava helps you to throw the exception.


public static boolean isValidIPV4Address(String ipAddress)
{  
Preconditions.checkNotNull(ipAddress); 
boolean isIp = false;
StringTokenizer tokens = new StringTokenizer(ipAddress, "."); if(tokens.countTokens() == 4)
{
// logic to validate ip address
// set isIp to true or false;
}
return isIp;
}

If "null" is passed, Preconditions.checkNotNull throws NullPointerException. You can also check for the validity of expression using Preconditions.checkArgument (check out Javadoc). Apart from improving the readability of code (to reduce boiler plates from code), it helps you to fail faster without causing any trouble to the state of the system.

Check out Preconditions. Here is the Javadoc of Preconditions. Don't you think that Preconditions is worth adding in our source code :-)

In the next post, we will see how to measure time with Guava's Stopwatch.


13 August 2011

Effort towards Perfection

Is it possible for a fresher or a newly formed company (any new entrant) to stand out and highly regarded among professionals (even among professionals with few years of experience)? I think, standing out in the crowd is no way related to how old are you. There isn't any age factor involved.

Standing out is the result of an attempt (even a failed attempt) towards perfection. In order to move towards perfection or excellence, you need a thought that perfection/excellence is a necessity (and not a value-add) and once you feel/absorb that the value is inherent in you. Perfection/excellence is not a stretch goal but an action, may be a base thing which you are supposed to do and a thing that you should naturally do.

I can't wait more to write this post after watching this video.


23 July 2011

First Program in Haskell

Recently, I stumbled upon a new programming paradigm - functional programming. Rather than focusing on variables, we define functions that does something. Without much of theory, let us quickly see an example

Write a program that finds triplets (a, b c) such that a, b and c are sides of a right angle triangle with none of the sides is greater than 20. I tried to write programs in Java and in Haskell as well. Here is the code snippet
Java
public class RightAngleTuples {
public static void main(String[] args) {
printTuples();
}
public static void printTuples() {
for(int c = 1; c <= 10; c++) {
for(int a = 1; a <= c; a++) {
for(int b = 1; b <= a; ++b ) {
if(a*a + b*b == c*c && a+b+c == 24) {
System.out.println(a + ", " + b + ", " + c);
}
}
}
}
}
}
Haskell
 Prelude> let triples = [(a,b,c) | c <- [1..10], a <- [1..c], b <- [1..a], a^2 + b^2 == c^2, a+b+c == 24 ]
Prelude> triples
[(8,6,10)]
Initially it may appear that both the programs are different syntactically. But if you really see, while programming in an imperative languages, we need to think a lot of logic and variables (or memory location) that maintains the state of the program. It is programmers responsibility to keep the state sane. But in the case of functional programming, there is no concept of state. Everything is functions (obviously everything is logic).

If you are really interested to learn a new programming paradigm, i recommend Haskell. With my few hours of encounter with Haskell, i feel it changes one's thinking :-) and Learn you a Haskell for Great Good by Miran Lipovaca can be a great resource. (BTW, i copied the above example from the book and i promise that it is in fact last example that i will steal from the book). An heads-up: reading Haskell doesn't increase your value in job market :-), you cannot ask for 40% more hike but just improves your geeky quotient :-)

More stuffs on Haskell in coming weeks/months. Sharing never hurts :-) 

19 June 2011

After a while things go real hard

It appears to me that i stopped blogging forever. I literally can't think what to write. It is over a month i tried to post something here. The past two months have been so engaging and lot of events. I felt that i lost touch and became irrelevant. I needed so much courage to write this post because i honestly think that every post should add value to me and at least to my eyes i should feel that my thinking is slightly getting better. This self imposed exile of keeping away from my blog was not so easy. I m back and i think i should be consistent.

Here comes the real learning. The manager typically delegates work sacrificing hands-on. Though the delegation helps and often needed when one moves up, being hands-on is equally important. I m not here to say that you need to strike balance as i think that such balance never exist in reality. All i m saying is "dont lose touch". Why?

Hands-on experience helps in better abstraction. Abstraction is more meaningful only if one is consistently gets his/her hands dirt. Without hands-on, the abstraction deviates from its course and it turns out to be a poor abstraction. The poor abstraction gives us meaningless decisions and from there the things go real hard. From empowerment, people move towards irresponsibility and abandonment without their own knowledge simply because they are far from reality and most of their decisions/thinking will become outdated.

05 April 2011

Multiple Dimensions

What is 9460528400000000?

It is the distance traveled  by light in one year in meters. If the theory of big bang is true and if this universe started off from a tiny dot and still expanding till the moment we read this, what will be the size of universe. In which direction does it expand, may be in all possible direction. If you say the size of universe is infinite, how infinite is it? We tried to find the size of universe and we failed. And then we started to say "the size of observable universe"

Assume that we haven't invented numbers yet. How will you describe the size of universe? Having no number system is a dimension and having a number system is a dimension. You need both to depict or guess the size of universe. You need "no number system" dimension to say that the universe is massive and you need "number system" dimension to say or to give a feel of how massive is this universe. Each dimension has its own job of conveying something unique of the subject. And we can travel only certain level in each dimension. Assume that instead of sending light to measure distance, you decide to walk and count the number of kilometers. Beyond a point, even sending light and measuring the distance doesn't do well (as waiting time is more. If you send light, only your grandson can receive the light reflected from the subject) and you have to measure the light that is originated from the distant galaxy. There is clearly inherent limitation in each dimension and we have switch over to another dimension to visualize things (and to overcome the limitations of a dimension)

Unless the subject is caught in few dimensions, our understanding of the subject will be vague and limited. Multiple dimensions really help not only in finding the size of universe  (science) but also in solving general problems in life. :-)

04 April 2011

Design Patterns - How to Learn

Most often we encounter people whom they call themselves as expert in design patterns. They start off with the discussion saying that they know Singleton pattern - knowing little bit of what is singleton and completely ignorant of why Singleton is needed. While design pattern helped experts to improve their productivity and the quality of software they write, it hasn't done anything to novice designers like us. Right away, let me say that it is not the problem with the design patterns. It shows that the way we learn design patterns isn't right.

Learning design patterns starts with getting hands dirt with abstraction, hierarchy, encapsulation and loose coupling. All these attributes do not come easy and they evolve over a period of time with imagination. We have to visualize how a solution is better than the other and if we can solve by writing code, it really helps us to understand deeper.

Keeping that mind, i started to put together series of puzzles on software design to improve my design knowledge - particularly on object oriented software design for quite sometime. I would say that this method is changing the way i think. If you also want to get your hands dirt, you can find puzzles tagged as "Design for Fun".


Let me tell you, more puzzles are on the way.

27 March 2011

Software Design Puzzle #8 - Manufacturing Soaps

Assume that you are an expert in manufacturing many varieties of soaps - bath soaps, detergent soaps and all soaps in the earth. You have a magic recipes of soap making right from procuring raw materials till manufacturing soaps and shipping to stores. There are many processes involved in soap making and there are various flavors for each process. And some of the processes are optional and specific types of soaps. For example, you know that you to have add a lot of scents for bath/toilet soap and lot of whitening material for detergent soaps. 

The puzzle is to design the soap manufacturing with lot of processes/steps and all these processes/steps are sequential meaning that one process cannot start until the previous processes are complete. For instance, the process of shipping cannot happen until you make soap and you cannot make soaps until you form soap base and you cannot start soap base without raw materials.

Can you design soap manufacturing process where one step depends on so many previous steps. To induce your thinking, let us assume that you have the following processes/steps

  • Buy raw materials (can be different based on the soaps)
  • Mix raw materials
  • Form soap base
  • Make soaps
  • Package soaps
  • Ship It
You are free to add more steps but the key is to come up with a process that can be changed dynamically based on soap types that are going to be invented in future :-)

19 March 2011

Interesting Tools on Bytecode Manipulation

The last week, i got a chance to experiment on couple of bytecode manipulation APIs/tools and thought i can share my learning here.

Javassist is one of bytecode manipulation APIs that helps you to manipulate bytecode and it is simpler to use. I wrote a simple method profiler that takes class files as input and instruments the bytecodes in each method. Added, method start time, complete time and measured the time spent in the method. One good thing about Javassist is that you can directly add code written in Java that compiles on the fly while instrumentation. Seems like bytecode instrumentation is bit easier in Javassist. With Javassist, you can also do on the fly instrumentation during classloading phase.

Retroweaver is another bytecode manipulation tool that back ports the code compiled by Java 5 to Java 1.4. If you have written classes (or a third party classes/libraries) in Java 5 and if your production system still runs with Java 1.4/1.3/1.2, you may want to check out this tool. It is really cool to use latest APIs/libraries in old JVM. Retrotranslator is a similar tool.

Bytecode engineering seems to be very interesting.

07 March 2011

Why Contemporary Leadership?

In the ancient times, when locomotive motor was not invented, people who could afford, traveled using bullock carts and the people who weren't able to afford a pair of horses and cart traveled by legs. After the invention of motor and after having undergone industrial revolution, people travel by cars or public transportation. The means of transport has changed but the travel hasn't (moving from one point to another point).

Today, many of the organizations are finding ways for transportation of their organizations from point a (current state) to point b (the transformation state) and they are ready to do anything that ought to be done. They are not ready to travel using bullock carts. I think criticisms and cranky jokes of obsessed Gen Xs on Gen Y are fading away and by now people who will be getting old in near future have to anyway deal with Gen Ys by their sheer numbers. If they continue to criticize, i think it is not going to help the organization at large. Any organization needs contemporary ways of brining in more effectiveness.

These days, the people who join workplace (predominantly often called as Gen Ys) are value conscious and by their virtue of exposure, they tend to make decisions based on how things affect them. We can't question and debate on their way of thinking because this thinking is not unique to Gen Ys. It is part of human evolution. The evolution says, the size of human brain shrinks but its processing power increase every day and the way a 22 year old youth thought 20 years back and the way in which modern day youth thinks varies (greatly?).

While we talk about culture, workplace ethics, work ethics or professionalism, we got to understand that those are things that were once practiced and if we get too much carried away (or adamant) with yesteryears, it will be living in the past or reliving the past and who knows we may fail utterly when we relive. Our thoughts, approach and the way we achieve the vision has to be contemporary. This has to be freshness.

While leading, we ought to be contemporary and forward looking taking good things of the past which just gives enough direction and propulsion needed for the escape velocity and to puts us into the orbit. 

Freshness, productive, creativity and innovation applies even for leadership.

05 March 2011

Democratization of Organization

There are two ways of getting things done - in your own way, dictatorially and in the way others like. With the machines, you can be dictatorial meaning that you decide when the machine has to produce products. Switch on the power, boot up the machine, make sure that all parts work in unison and the product is produced. It is a continuous system where the output is predictable. But in the place where human beings are involved and the factor of motivation/passion more appropriate than fear, democratization or knowing what people have in their mind becomes absolute need. Human beings are discrete systems and they become quite unpredictable as mind and emotions are involved. 

There are two reasons why you democratize anything. First is due to the fact that you cannot make decisions or you don't feel responsible for something that is going to happen and the second reason is that you want to democratize to align your people towards the vision - may it be vision of a company or a country or the mankind. The first reason actually paves way to collective irresponsibility (like the climate change where everyone talks but no one does anything) and the second approach involves a lot hard work (likes of Mahatma Gandhi, Martin  Luther King). In order for effective democratization to become reality, first we have to accept that we can achieve it after a lot of failures. Initially, people tend to misunderstand the process of democratizing before understanding it completely which at the initial stage results to a lot of failure. After the initial failures and if you rally around your people during the failures, the democratization takes shape and becomes effective.

What are the signs of effective democratization? If you have at least few souls who give back far more than what they take back and if the ratio of number of souls that give back to the number of souls take back increases as the function of time, then you are staging yourself for better democratization.

22 February 2011

Puzzle - Who is Knight and Who is Knave

This is a puzzle that i read in internet sometime last week and thought of sharing it here.

There is an island that has people who always speak truth (knight) or people who always lie (knaves). Unfortunately, you need find the way to the capital of the island. You are standing at the fork and there are two roads - left and right. There are three people, let us call them A, B and C, who are native of the island standing near the fork. You approach them, they tell you the following statements

Two of us are knaves, one of us is knight and the road in the left goes to the capital.

Will you be able to find which road goes to the capital - is it left or right? 

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.

16 January 2011

Emergent Behavior & its Role in Big Picture

A football team wins FIFA world cup, a society that is progressive, a complex software system that fails in field utterly, a strategic group fail to deliver a good strategy, a promising youth later turns out be a pathetic failure at later stage of his life due to his tiny compromises, a kid by continuous practice turns out to be a distinguished performer in arts by being passionate everyday. 

Let us see a software perspective.

Few years back, having a personal computer with 2GB of RAM is very rare and today it is common thing and so is parallel processing (particularly after multi-cores becoming a common thing). The sheer performance of the hardware has increased at least few folds. However, Microsoft Windows froze few years back and it continues to freeze even now. BTW, the freezing is little to do with MS Windows (but it plays a role). Most of the damage is done by the application developers.

Few years back, the developers used to think that we have 512 MB RAM and the part of it is ours (may be half of it). Today, we tend to think that 2 GB RAM is available in the system (as we give minimum requirements in our user guide or release note) and half of it ours. Our thinking doesn't change over a period of time. When we develop applications, we think that there will be few applications that will coexists with ours.

Let us assume that for a 2 GB RAM and 2 GB of swap, we think that at least four applications can run with each  consuming 1 GB of RAM. The math works perfectly well on paper and for the first few days until the processes grows up to 2GB (put together). Beyond 2 GB, the operating system has to swap some portion of RAM to disk. Once few pages are swapped, a method call or access to heap may lead to page miss and the operating system has to fetch the page from swap - an interesting complexity gets added. Due to this complexity, the time to execute an intended operation takes little longer which again may increase the time and memory footprint (as delayed execution delays reclaim of memory which will become eligible for reclaim if the program runs as per plan). This complexity grows exponentially until few process crashes or the system crawls and you decide to give a rebirth by rebooting the system.

But mathematically, we think that there is no problem with the parts (each process) but the entire issue is with the whole (the operating system and the processes put together). This is called emergent behavior. The whole exhibits a behavior that cannot be explicitly attributed to the parts and each part thinks that none of its behavior is responsible for the whole behavior. There is inherent complexity due to movement of parts, the environment and the processing of events that are external to the parts (and no way related to any of the parts).

This emergent behavior affects the operating systems, make climate change a failure or reason for human stupidity. Emergent Behavior proves that the software can be made with the same process that is used to make this universe and the same process can be used to attain spiritual enlightenment.

Think about how an image is formed. The image are formed using pixels. Does removing a pixel lead to disfigured image? No. The disfigure is almost negligible. So, the whole has a power than its parts due to the emergent behavior. People who talk about "the bigger picture" should focus more on studying "emergent behavior". Generalizing things from individual events can be done well by understanding this emergent behavior (and it actually helps not to generalize the exceptions).

15 January 2011

Importance of Expressiveness in Programming Languages

It is a while i had a post here and in fact this is my first post in 2011. I happy to have my first post of the year on the topic that is of huge interest to me these days.

Why aren't we still programming in assembly languages? The threads, inter-process communication mechanisms can be done in assembly language but not easily though. With assembly language, there is only level of abstraction - ability to see the requirements as sequence of bits that are comprehensible by the machine. When you want make your program to run in another target platform, you may need to change both the abstraction and implementation. The assembly language is not as expressive as procedural language. In order to increment a number, you have locate the address, move it to accumulator, add it by one and store back to the same memory location. There is no clear division of responsibility. You have more number of steps to do in assembly but that is very primitive in procedural language.

Why aren't we writing program with procedural language alone? Procedural language is less expressive than object oriented program. The amount of code that a developer should write to accomplish a logic is more in the case of procedural language. While the level of abstraction is moved a level up from the bare machine, still the abstraction is not efficient to solve a problem in application domain. As far as the application is concerned (and in imperative languages), a task is accomplished with side effects. You call a method and it changes the state of few objects. The data and operation have to cohesive. So, in application development it is highly essential that we glue the data together with operations because these operations are specialized over the data.

There are other types of programming languages like functional programming inspired by lambda calculus and one can write their own language for a particular domain. There is one basic commonality in all types of programming languages - they try to be more expressive to make the life of developers easier. For example, today many developers are thinking about writing concurrent programs not only due to multi-cores being prevalent but also due to modern languages that made multi-threading highly expressive. It is easier to write multi-threaded in Java than in C or assembly.

As we move on, we will see many more expressive language, expressive than Java world and it is part of evolution of programming languages.

The article was written after reading Grady Booch's "OOAD with Application" and first chapter of "Programming Paradigms". Hoping to bring few posts on the subject this year. Watch out this space.