SlideShare a Scribd company logo
1 of 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]

More Related Content

Viewers also liked

Robotics Workshop Overview for School Students
Robotics Workshop Overview for School StudentsRobotics Workshop Overview for School Students
Robotics Workshop Overview for School StudentsRootStalk Academy
 
Blessing Equation Series 3 In 1
Blessing Equation Series 3 In 1Blessing Equation Series 3 In 1
Blessing Equation Series 3 In 1Simon Cooper
 
Why Prefer Flat Roofs?
Why Prefer Flat Roofs? Why Prefer Flat Roofs?
Why Prefer Flat Roofs? Michale Jazz
 
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 anhHườngg Thu
 
Online Assignment
Online AssignmentOnline Assignment
Online AssignmentArun Murali
 
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 waterJaime R. Gutiérrez
 
Propovidi_2010-11_ Final 8x11
Propovidi_2010-11_ Final 8x11Propovidi_2010-11_ Final 8x11
Propovidi_2010-11_ Final 8x11Dr. Vasyl Dmytriv
 
原装进口红酒报价
原装进口红酒报价原装进口红酒报价
原装进口红酒报价brookedeng
 
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 PurposeKarl Bimshas Consulting
 
Techman Pro show technologies
Techman Pro show technologiesTechman Pro show technologies
Techman Pro show technologiesshirnepes
 
Soong HOMME logo(黑底白字标)-1
Soong HOMME logo(黑底白字标)-1Soong HOMME logo(黑底白字标)-1
Soong HOMME logo(黑底白字标)-1Cindy Song
 
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 HeavenNicolas Fränkel
 
Ahkam Janyiz احكام الجنائز
Ahkam Janyiz احكام الجنائزAhkam Janyiz احكام الجنائز
Ahkam Janyiz احكام الجنائزMohamed Irshad
 

Viewers also liked (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
 

Similar to 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.docxsmile790243
 
Professional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsProfessional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsMike Wilcox
 
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.PVS-Studio
 
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.pdfrd1742
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeNaresh Jain
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate JavaPhilip 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 EngineeringSource Conference
 
Java interview-questions-and-answers
Java interview-questions-and-answersJava interview-questions-and-answers
Java interview-questions-and-answersbestonlinetrainers
 
SharePoint Saturday St. Louis - SharePoint & jQuery
SharePoint Saturday St. Louis - SharePoint & jQuerySharePoint Saturday St. Louis - SharePoint & jQuery
SharePoint Saturday St. Louis - SharePoint & jQueryMark Rackley
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4sotlsoc
 
Questioning the status quo
Questioning the status quoQuestioning the status quo
Questioning the status quoIvano Pagano
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answersKrishnaov
 
Software Design
Software DesignSoftware Design
Software DesignSpy Seat
 
C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questionsnicolbiden
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescriptDavid Furber
 
#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
 
The java server pages
The java server pagesThe java server pages
The java server pagesAtul Saurabh
 
Data weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsData weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsManjuKumara GH
 

Similar to 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
 

Recently uploaded

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

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.