SlideShare uma empresa Scribd logo
1 de 18
Tao Showed Me the Way
I Meet the Tao ,[object Object],[object Object],[object Object]
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.
I approached, with winning smile on my face, and the code snippet on my think pad.
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(&quot;Duplicate detected:&quot; + args[i]);   }   }   System.out.println(s.size()+&quot; distinct words detected: &quot;+s);   } }
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]
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.
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(&quot;Duplicate detected:&quot; + args[i]);   }   }   System.out.println(s.size()+&quot; distinct words detected: &quot;+s);   } }
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.
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.
Early next morning, I hurried myself to Tao. Tao was not there. But He had left message for me. Design to Interface.
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.
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.
Looking Through the Glass 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.
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:
% java FindDups i came i saw i left Duplicate word detected: i Duplicate word detected: i 4 distinct words detected: [came, i, left, saw]
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.
Created by ,[object Object],[object Object]

Mais conteúdo relacionado

Destaque

De va dap an mon tieng anh
De va dap an mon tieng anhDe va dap an mon tieng anh
De va dap an mon tieng anh
Hườngg Thu
 
Propovidi_2010-11_ Final 8x11
Propovidi_2010-11_ Final 8x11Propovidi_2010-11_ Final 8x11
Propovidi_2010-11_ Final 8x11
Dr. Vasyl Dmytriv
 
Techman Pro show technologies
Techman Pro show technologiesTechman Pro show technologies
Techman Pro show technologies
shirnepes
 
Soong HOMME logo(黑底白字标)-1
Soong HOMME logo(黑底白字标)-1Soong HOMME logo(黑底白字标)-1
Soong HOMME logo(黑底白字标)-1
Cindy Song
 

Destaque (17)

Robotics Workshop Overview for School Students
Robotics Workshop Overview for School StudentsRobotics Workshop Overview for School Students
Robotics Workshop Overview for School Students
 
Blessing Equation Series 3 In 1
Blessing Equation Series 3 In 1Blessing Equation Series 3 In 1
Blessing Equation Series 3 In 1
 
Why Prefer Flat Roofs?
Why Prefer Flat Roofs? Why Prefer Flat Roofs?
Why Prefer Flat Roofs?
 
De va dap an mon tieng anh
De va dap an mon tieng anhDe va dap an mon tieng anh
De va dap an mon tieng anh
 
Online Assignment
Online AssignmentOnline Assignment
Online Assignment
 
There is a colored boy at the bottom of the water
There is a colored boy at the bottom of the waterThere is a colored boy at the bottom of the water
There is a colored boy at the bottom of the water
 
Propovidi_2010-11_ Final 8x11
Propovidi_2010-11_ Final 8x11Propovidi_2010-11_ Final 8x11
Propovidi_2010-11_ Final 8x11
 
原装进口红酒报价
原装进口红酒报价原装进口红酒报价
原装进口红酒报价
 
Three Ways to Find Your Passion and Act on Purpose
Three Ways to Find Your Passion and Act on PurposeThree Ways to Find Your Passion and Act on Purpose
Three Ways to Find Your Passion and Act on Purpose
 
Techman Pro show technologies
Techman Pro show technologiesTechman Pro show technologies
Techman Pro show technologies
 
Soong HOMME logo(黑底白字标)-1
Soong HOMME logo(黑底白字标)-1Soong HOMME logo(黑底白字标)-1
Soong HOMME logo(黑底白字标)-1
 
Acuerdo 016 de 2016
Acuerdo 016 de 2016Acuerdo 016 de 2016
Acuerdo 016 de 2016
 
XM CV 2015
XM CV 2015XM CV 2015
XM CV 2015
 
Morning at Lohika - Spring Boot Kotlin, a match made in Heaven
Morning at Lohika - Spring Boot Kotlin, a match made in HeavenMorning at Lohika - Spring Boot Kotlin, a match made in Heaven
Morning at Lohika - Spring Boot Kotlin, a match made in Heaven
 
D burmistrov
D burmistrovD burmistrov
D burmistrov
 
Ahkam Janyiz احكام الجنائز
Ahkam Janyiz احكام الجنائزAhkam Janyiz احكام الجنائز
Ahkam Janyiz احكام الجنائز
 
KGPP
KGPPKGPP
KGPP
 

Semelhante a Tao Showed Me The Way

Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docxLab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
smile790243
 
Preexisting code, please useMain.javapublic class Main { p.pdf
Preexisting code, please useMain.javapublic class Main {    p.pdfPreexisting code, please useMain.javapublic class Main {    p.pdf
Preexisting code, please useMain.javapublic class Main { p.pdf
rd1742
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate Java
Philip Johnson
 
Ben Agre - Adding Another Level of Hell to Reverse Engineering
Ben Agre - Adding Another Level of Hell to Reverse EngineeringBen Agre - Adding Another Level of Hell to Reverse Engineering
Ben Agre - Adding Another Level of Hell to Reverse Engineering
Source Conference
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4
sotlsoc
 
#SPSEMEA SharePoint & jQuery - What I wish I would have known a year ago..
#SPSEMEA SharePoint & jQuery - What I wish I would have known a year ago..#SPSEMEA SharePoint & jQuery - What I wish I would have known a year ago..
#SPSEMEA SharePoint & jQuery - What I wish I would have known a year ago..
Mark Rackley
 

Semelhante a Tao Showed Me The Way (20)

Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docxLab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
 
Professional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsProfessional JavaScript: AntiPatterns
Professional JavaScript: AntiPatterns
 
Wade not in unknown waters. Part two.
Wade not in unknown waters. Part two.Wade not in unknown waters. Part two.
Wade not in unknown waters. Part two.
 
Preexisting code, please useMain.javapublic class Main { p.pdf
Preexisting code, please useMain.javapublic class Main {    p.pdfPreexisting code, please useMain.javapublic class Main {    p.pdf
Preexisting code, please useMain.javapublic class Main { p.pdf
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate Java
 
Ben Agre - Adding Another Level of Hell to Reverse Engineering
Ben Agre - Adding Another Level of Hell to Reverse EngineeringBen Agre - Adding Another Level of Hell to Reverse Engineering
Ben Agre - Adding Another Level of Hell to Reverse Engineering
 
Java interview-questions-and-answers
Java interview-questions-and-answersJava interview-questions-and-answers
Java interview-questions-and-answers
 
Spsemea j query
Spsemea   j querySpsemea   j query
Spsemea j query
 
SharePoint Saturday St. Louis - SharePoint & jQuery
SharePoint Saturday St. Louis - SharePoint & jQuerySharePoint Saturday St. Louis - SharePoint & jQuery
SharePoint Saturday St. Louis - SharePoint & jQuery
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4
 
Questioning the status quo
Questioning the status quoQuestioning the status quo
Questioning the status quo
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
 
Software Design
Software DesignSoftware Design
Software Design
 
C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questions
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
PHP Reviewer
PHP ReviewerPHP Reviewer
PHP Reviewer
 
#SPSEMEA SharePoint & jQuery - What I wish I would have known a year ago..
#SPSEMEA SharePoint & jQuery - What I wish I would have known a year ago..#SPSEMEA SharePoint & jQuery - What I wish I would have known a year ago..
#SPSEMEA SharePoint & jQuery - What I wish I would have known a year ago..
 
The java server pages
The java server pagesThe java server pages
The java server pages
 
Data weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsData weave 2.0 language fundamentals
Data weave 2.0 language fundamentals
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 

Tao Showed Me The Way

  • 1. Tao Showed Me the Way
  • 2.
  • 3. 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.
  • 4. I approached, with winning smile on my face, and the code snippet on my think pad.
  • 5. 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(&quot;Duplicate detected:&quot; + args[i]); } } System.out.println(s.size()+&quot; distinct words detected: &quot;+s); } }
  • 6. 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]
  • 7. 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.
  • 8. 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(&quot;Duplicate detected:&quot; + args[i]); } } System.out.println(s.size()+&quot; distinct words detected: &quot;+s); } }
  • 9. 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.
  • 10. 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.
  • 11. Early next morning, I hurried myself to Tao. Tao was not there. But He had left message for me. Design to Interface.
  • 12. 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.
  • 13. 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.
  • 14. Looking Through the Glass 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.
  • 15. 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:
  • 16. % java FindDups i came i saw i left Duplicate word detected: i Duplicate word detected: i 4 distinct words detected: [came, i, left, saw]
  • 17. 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.
  • 18.