Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tao Showed Me the Way

Tao Showed Me the Way

Anecdote to highlight importance of programming paradigm - Interface Based Programming.

Avatar for Sachin Raverkar

Sachin Raverkar

March 12, 2013

More Decks by Sachin Raverkar

Other Decks in Programming

Transcript

  1. I Meet the Tao Tao is as usual sitting under

    the tree of wisdom. L a s t w e e k w h e n t h e programming lesson on java collection classes was over, he threw a coding challenge at us. He asked to write a small piece of code to iterate over the collection.
  2. The Problem Write a program that takes the words in

    its argument list and prints out any duplicate words, the number of distinct words, and a list of the words with duplicates eliminated.
  3. import java.util.*; public class FindDups { public static void main(String

    args[]) { new FindDups(args); } FindDups(String[] input){ HashSet s = new HashSet(); for (int i=0; i<args.length; i++){ if (!s.add(args[i])){ System.out.println("Duplicate detected:" + args[i]); } } System.out.println(s.size()+" distinct words detected: "+s); } }
  4. Out of the program: % java FindDups i came i

    saw i left Duplicate detected: i Duplicate detected: i 4 distinct words detected: [came, left, saw, i]
  5. Do Small Change, to Make Big Difference. Tao had a

    glance at my great piece of code. There was charismatic smile on his face. I was startled. I knew, I must have done some grave mistake. However, I was eager to learn that fine point of Java programming. I look forward to Tao to enlighten me. Then, to my surprise, Tao made a small correction to my code. I could not see much improvement though.
  6. import java.util.*; public class FindDups { public static void main(String

    args[]) { new FindDups(args); } FindDups(String[] input){ Set s = new HashSet(); for (int i=0; i<args.length; i++){ if (!s.add(args[i])){ System.out.println("Duplicate detected:" + args[i]); } } System.out.println(s.size()+" distinct words detected: "+s); } }
  7. I could not hide my quizzical expressions on my face.

    Tao continued to don the same charismatic smile. Then he asked me to ponder over the “small change” and decipher the underlying design principle. Then he left me. I stood there wondering about the design principle.
  8. I struggled through out the day to pin point the

    design principle. But no gain. Then I wrote down the lines of difference, in hope that I could read between the lines. HashSet s = new HashSet(); …………… My code Set s = new HashSet(); …………… His code It was beautiful full moon night. I kept staring, at my lap top screen. I do not remember when did I fall a sleep.
  9. Early next morning, I hurried myself to Tao. Tao was

    not there. But He had left message for me. Design to Interface.
  10. I revisited the lines of difference. HashSet s = new

    HashSet(); …………… My code Set s = new HashSet(); …………… His code . Now I could see the design principle I was exploring. I got the Aha! Feeling. But I was craving for the Aha! Insight. What is the rationale behind this design mantra. And how it is making the big difference to my Java code. I surrendered myself to Tao. Tao showed mercy on me. I know. When student is ready, Tao appears.
  11. Looking Through the Glass Note that the code should always

    refer to the collection by its interface type (Set), rather than by its implementation type (HashSet). This is a strongly recommended programming practice, as it gives you the flexibility to change implementations merely by changing the constructor. If the variables used to store a collection, or the parameters used to pass it around, are declared to be of the collection's implementation type rather than its interface type, then all such variables and parameters must be changed to change the collection's implementation type.
  12. Furthermore, there's no guarantee that the resulting program will work;

    if the program uses any non-standard operations that are present in the original implementation type but not the new one, the program will fail. Referring to collections only by their interface keeps you honest, in the sense that it prevents you from using any non-standard operations. Looking Through the Glass
  13. Small Change in Requirement Should Not Result in Big Change

    in Code The implementation type of the Set in the example above is HashSet, which makes no guarantees as to the order of the elements in the Set. If you want the program to print the word list in alphabetical order, all you have to do is to change the set's implementation type from HashSet to TreeSet. Making this trivial one-line change causes the command line in the previous example to generate the following output:
  14. % java FindDups i came i saw i left Duplicate

    word detected: i Duplicate word detected: i 4 distinct words detected: [came, i, left, saw]
  15. When I emerged from the fundamentals and looked up, Tao

    had left me long back. Tao was not there. But He had left message for me. Design to Interface.