SlideShare una empresa de Scribd logo
1 de 49
Descargar para leer sin conexión
Understanding Typing,
Understanding Ruby
Justin Lin
caterpillar@openhome.cc
http://openhome.cc
What I'll cover
• What is typing?
• Pros and cons of type declarations.
• What does quacking like a dock mean?
• Static Typing vs Unit Testing.
• Making us better at Ruby.
2
Speaker?
http://www.linkedin.com/in/caterpillar
3
What is typing?
• Assigning a data type, what is called
typing, gives meaning to a sequences of
bits such as a value in memory or some
object such as a variable.
4
• Before typing … you have to figure out
– the set of related values…
– and the set of their related operation…
5
Type checking
• Checking the constraints of types.
3.14 doesn't belong to Int.
The instance doesn't have the
operation defined in Int.
6
• Checking occurs at compile-time or runtime …
– Statically-typed languages(Haskell, Scala, Java)
– Dynamically-typed languages(JavaScript, Python, Ruby)
Checked before running doubleMe
Checked when running the
method '*'
7
Today is Ruby Conf,
if my memory serves me … :p
• You all know disadvantages of statically-
typed languages, such as …
– Type declaration is verbose, inexpressive, and
annoying.
– Even I see a bird that walks like a duck and
swims like a duck and quacks like a duck, I can't
call that bird a duck.
– The static typing doesn't ensure the function is
correct. Unit test is also required.
8
Today is Ruby Conf,
if my memory serves me … :p
• You all know disadvantages of statically-
typed languages, such as …
– Type declaration is verbose, inexpressive, and
annoying.
– Even I see a bird that walks like a duck and
swims like a duck and quacks like a duck, I can't
call that bird a duck.
– The static typing doesn't ensure the function is
correct. Unit test is also required.
9
You know too much ……
Type declaration
• Verbose, inexpressive, and annoying?
• Haskell is a statically-typed language.
Type inference works here.
• For clearness, it's recommended to
declare the type.
10
• Type inference draws conclusions about the
types of variables based on how
programmers use those variables.
• It presents in more and more statically-typed
languages.
11
• The verbose Java also do type inference
somewhere, even not perfect … XD
12
List<Person>
Person
Martin Fowler
"One day I found myself trying to follow some well-
written Ruby code. I found the lack of type information
on parameters made life difficult - I kept saying to myself
'what exactly do I have here?'"
http://martinfowler.com/bliki/DynamicTyping.html
13
• Type declaration also provides type
information.
• For clearness, dynamically-typed languages
may need type information, no matter it‘s
through libraries …
14
• comments …
• or even naming conventions.
15
Function/method overloading
• Allows creating several methods with the
same name which differ from each other in
the type of the input (and the output) of the
function.
• Allows creating several methods with the
same name which have variable number of
arguments.
16
What you want is Ad-hoc polymorphism?
• A polymorphic function can denote a number
of distinct and potentially heterogeneous
implementations depending on the type of
argument(s).
• Also known as function overloading.
17
What you want is Ad-hoc polymorphism?
• A polymorphic function can denote a number
of distinct and potentially heterogeneous
implementations depending on the type of
argument(s).
• Also known as function overloading.
Subtype polymorphism
18
What you want are default values?
• Java has no syntax for setting default values
for arguments. Overloading is a way.
It's, actually, a default value.
19
Overloading in Dynamically-typed languages?
• Runtime type/property checking.
• Default values/option object.
20
What I really need is a
range-like object, not
only a Range instance.
Am I having distinct
implementations depending
on the type/properties of
argument(s)
21
Duck typing
• Quack like a duck…
• Statically-typed languages can't do that?
Structural typing
22
• Well…well…don't cheat on me … structural
typing is static duck typing …
• How about this?
Verbose, of course!!
This is Java … XD
23
Why do you need duck typing?
• Flexible … especially for start-up.
• When you take duck typing, you are using,
actually, a set of operations.
• Who does this set of operations belong to?
– Instances of some class?
– Instances of different classes?
24
Instances of some class?
• Defining a singleton method is, actually,
defining the instance method in the
anonymous singleton class.
Open the anonymous singleton
class of duck
25
• After you enjoy its convenience, refactor
the code …
26
Instances of different classes?
• In Java, we'll define an interface for the
set of operations.
27
• When you take duck typing, you are using,
actually, a set of operations.
• When instances of different classes share
the same set of method implementations,
what will you do?
28
• From JDK8, we can define default methods
in interface…
29
• In Ruby, what will you do?
Duck typing is flexible, but it
doesn't mean that you don't
need further thinking…
30
Generics
• Compilers are really noisy…
Shut up, Please!
I know what I am doing, OK?
31
• Because you tell it to shut up ….
• Generics saves the world?
ClassCastException?
It’s all your fault.
Hey, you're doing something
stupid, and you can't tell me
to shut up…YA!!
32
• Generics destroys the code!!
By Mario Fusco, http://www.slideshare.net/mariofusco/monadic-java
33
• The loading of type declaration is taken by
API developers. The API clients get
convenience.
• And the work of type checking is pushed to
compiler-time. You avoid the runtime
exception - ClassNotFoundException.
Parametric polymorphism
34
Who is responsible for type checking?
• Supporters of statically-typed languages
– Compilers perform comprehensive type
checking at compiler-time. Ensure that some
errors due to wrong types can't happen at
runtime.
• Supporters of dynamically-typed languages
– Passing appropriate types to the function
provides no guarantee that the result of the
function will be the correct value. Unit Test can
ensure types and behavior is correct.
35
Static Typing vs Unit Testing
• Compilers do type assertion cheaply or even
for free – if you have good type inference.
• The more type-error messages, the more
you have to think about types to pass
compilers.
• It's especially true when you're writing Haskell.
36
• When using dynamically-typed languages,
you ease the burden of declaring types, but
were bent with the burden of type checking.
• Do you know …
– Real requirements of type checking in your code?
– What types are in the language?
– How to do unit test for type checking?
• Would you like and have the ability to write
more unit test to ensure types and behavior
is correct?
• Are you writing unit test?
37
Type checking
Why does fill use
repond_to?, not is_a?
38
• How to unit test doubleMe?
• Double whom? Fixnum? String? Array?
Range? Ouch …it doesn't define :+ method.
• If an object doesn't define :+ method, how
to deal with? method_missing?
• Ask yourself more questions about types
when unit test.
39
Joshua Bloch
"Given how hard it is to write correct programs, we need
all the help we can get. So anything that removes
potential bugs from our plate is good. That’s why I’m very
much a believer in static typing and in static analysis"
《Coders at work》
40
• Figure out your requirements of type checking,
look for tools in Ruby eco-system like …
– python-rightarrow
– PEP-3107 -- Function Annotations
41
– IDE Support
42
• Or even something like TypeScript
– tsc compile the code to JavaScript …
– A dynamically-typed language but …
43
It's almost time to draw a conclusion
• One more question, left for you…
• Think about difference between checked
exceptions and unchecked exceptions,
an exclusive feature in Java … XD
• It'll help you think about how to deal with
exceptions in Ruby …
44
Making us better at Ruby
• Using dynamically-typed languages needs
understanding statically-typed languages.
• Knowing the advantages of statically-typed
languages furthers learning pros and cons of
dynamically-typed languages.
• Recognizing the restrictions of statically-typed
languages helps digging the responsibilities
you should take in dynamically-typed
languages.
45
• With all these great power in Ruby …
– Dynamically-typed
– Object individuation
– Open class
– Runtime introspection
– …
• What responsibility should you take?
46
"We're all consenting adults here."
NOT
just a slogan.
With great power comes
great responsibility.
Spider-Man (2002)
48
Understanding Typing, Understanding Ruby.
Thanks!!
49

Más contenido relacionado

La actualidad más candente

A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
mametter
 

La actualidad más candente (20)

TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
 
Introducing TypeScript
Introducing TypeScriptIntroducing TypeScript
Introducing TypeScript
 
Power Leveling your TypeScript
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScript
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScript
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
 
Language portfolio
Language portfolioLanguage portfolio
Language portfolio
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
 
TypeScript Modules
TypeScript ModulesTypeScript Modules
TypeScript Modules
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston Levi
 
Live Typing - California Smalltalkers
Live Typing - California SmalltalkersLive Typing - California Smalltalkers
Live Typing - California Smalltalkers
 
TypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript applicationTypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript application
 
Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2
 
Beginning Java for .NET developers
Beginning Java for .NET developersBeginning Java for .NET developers
Beginning Java for .NET developers
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
 

Similar a Understanding Typing. Understanding Ruby.

Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
Chetan Giridhar
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
TO THE NEW | Technology
 
An Overview of automated testing (1)
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)
Rodrigo Lopes
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
Iván Montes
 

Similar a Understanding Typing. Understanding Ruby. (20)

Java introduction
Java introductionJava introduction
Java introduction
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
 
Software testing (2) trainingin-mumbai
Software testing (2) trainingin-mumbaiSoftware testing (2) trainingin-mumbai
Software testing (2) trainingin-mumbai
 
Software testing (2) trainingin-mumbai...
Software testing (2) trainingin-mumbai...Software testing (2) trainingin-mumbai...
Software testing (2) trainingin-mumbai...
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
Static-Analysis-in-Industry.pptx
Static-Analysis-in-Industry.pptxStatic-Analysis-in-Industry.pptx
Static-Analysis-in-Industry.pptx
 
Python Tutorial for Beginner
Python Tutorial for BeginnerPython Tutorial for Beginner
Python Tutorial for Beginner
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
 
Software Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaSoftware Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with Scala
 
F# for startups
F# for startupsF# for startups
F# for startups
 
An Overview of automated testing (1)
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
Jvm2
Jvm2Jvm2
Jvm2
 
Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1
 
Kevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScriptKevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScript
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
 

Más de Justin Lin

Más de Justin Lin (20)

Ch14 簡介 Spring Boot
Ch14 簡介 Spring BootCh14 簡介 Spring Boot
Ch14 簡介 Spring Boot
 
Ch13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityCh13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/Security
 
Ch12 Spring 起步走
Ch12 Spring 起步走Ch12 Spring 起步走
Ch12 Spring 起步走
 
Ch11 簡介 JavaMail
Ch11 簡介 JavaMailCh11 簡介 JavaMail
Ch11 簡介 JavaMail
 
Ch10 Web 容器安全管理
Ch10 Web 容器安全管理Ch10 Web 容器安全管理
Ch10 Web 容器安全管理
 
Ch09 整合資料庫
Ch09 整合資料庫Ch09 整合資料庫
Ch09 整合資料庫
 
Ch08 自訂標籤
Ch08 自訂標籤Ch08 自訂標籤
Ch08 自訂標籤
 
Ch07 使用 JSTL
Ch07 使用 JSTLCh07 使用 JSTL
Ch07 使用 JSTL
 
Ch06 使用 JSP
Ch06 使用 JSPCh06 使用 JSP
Ch06 使用 JSP
 
Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器
 
Ch04 會話管理
Ch04 會話管理Ch04 會話管理
Ch04 會話管理
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應
 
Ch02 撰寫與設定 Servlet
Ch02 撰寫與設定 ServletCh02 撰寫與設定 Servlet
Ch02 撰寫與設定 Servlet
 
CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式
 
14. 進階主題
14. 進階主題14. 進階主題
14. 進階主題
 
13.並行、平行與非同步
13.並行、平行與非同步13.並行、平行與非同步
13.並行、平行與非同步
 
12. 除錯、測試與效能
12. 除錯、測試與效能12. 除錯、測試與效能
12. 除錯、測試與效能
 
11. 常用內建模組
11. 常用內建模組11. 常用內建模組
11. 常用內建模組
 
10. 資料永續與交換
10. 資料永續與交換10. 資料永續與交換
10. 資料永續與交換
 
9. 資料結構
9. 資料結構9. 資料結構
9. 資料結構
 

Último

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Último (20)

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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

Understanding Typing. Understanding Ruby.

  • 1. Understanding Typing, Understanding Ruby Justin Lin caterpillar@openhome.cc http://openhome.cc
  • 2. What I'll cover • What is typing? • Pros and cons of type declarations. • What does quacking like a dock mean? • Static Typing vs Unit Testing. • Making us better at Ruby. 2
  • 4. What is typing? • Assigning a data type, what is called typing, gives meaning to a sequences of bits such as a value in memory or some object such as a variable. 4
  • 5. • Before typing … you have to figure out – the set of related values… – and the set of their related operation… 5
  • 6. Type checking • Checking the constraints of types. 3.14 doesn't belong to Int. The instance doesn't have the operation defined in Int. 6
  • 7. • Checking occurs at compile-time or runtime … – Statically-typed languages(Haskell, Scala, Java) – Dynamically-typed languages(JavaScript, Python, Ruby) Checked before running doubleMe Checked when running the method '*' 7
  • 8. Today is Ruby Conf, if my memory serves me … :p • You all know disadvantages of statically- typed languages, such as … – Type declaration is verbose, inexpressive, and annoying. – Even I see a bird that walks like a duck and swims like a duck and quacks like a duck, I can't call that bird a duck. – The static typing doesn't ensure the function is correct. Unit test is also required. 8
  • 9. Today is Ruby Conf, if my memory serves me … :p • You all know disadvantages of statically- typed languages, such as … – Type declaration is verbose, inexpressive, and annoying. – Even I see a bird that walks like a duck and swims like a duck and quacks like a duck, I can't call that bird a duck. – The static typing doesn't ensure the function is correct. Unit test is also required. 9 You know too much ……
  • 10. Type declaration • Verbose, inexpressive, and annoying? • Haskell is a statically-typed language. Type inference works here. • For clearness, it's recommended to declare the type. 10
  • 11. • Type inference draws conclusions about the types of variables based on how programmers use those variables. • It presents in more and more statically-typed languages. 11
  • 12. • The verbose Java also do type inference somewhere, even not perfect … XD 12 List<Person> Person
  • 13. Martin Fowler "One day I found myself trying to follow some well- written Ruby code. I found the lack of type information on parameters made life difficult - I kept saying to myself 'what exactly do I have here?'" http://martinfowler.com/bliki/DynamicTyping.html 13
  • 14. • Type declaration also provides type information. • For clearness, dynamically-typed languages may need type information, no matter it‘s through libraries … 14
  • 15. • comments … • or even naming conventions. 15
  • 16. Function/method overloading • Allows creating several methods with the same name which differ from each other in the type of the input (and the output) of the function. • Allows creating several methods with the same name which have variable number of arguments. 16
  • 17. What you want is Ad-hoc polymorphism? • A polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s). • Also known as function overloading. 17
  • 18. What you want is Ad-hoc polymorphism? • A polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s). • Also known as function overloading. Subtype polymorphism 18
  • 19. What you want are default values? • Java has no syntax for setting default values for arguments. Overloading is a way. It's, actually, a default value. 19
  • 20. Overloading in Dynamically-typed languages? • Runtime type/property checking. • Default values/option object. 20
  • 21. What I really need is a range-like object, not only a Range instance. Am I having distinct implementations depending on the type/properties of argument(s) 21
  • 22. Duck typing • Quack like a duck… • Statically-typed languages can't do that? Structural typing 22
  • 23. • Well…well…don't cheat on me … structural typing is static duck typing … • How about this? Verbose, of course!! This is Java … XD 23
  • 24. Why do you need duck typing? • Flexible … especially for start-up. • When you take duck typing, you are using, actually, a set of operations. • Who does this set of operations belong to? – Instances of some class? – Instances of different classes? 24
  • 25. Instances of some class? • Defining a singleton method is, actually, defining the instance method in the anonymous singleton class. Open the anonymous singleton class of duck 25
  • 26. • After you enjoy its convenience, refactor the code … 26
  • 27. Instances of different classes? • In Java, we'll define an interface for the set of operations. 27
  • 28. • When you take duck typing, you are using, actually, a set of operations. • When instances of different classes share the same set of method implementations, what will you do? 28
  • 29. • From JDK8, we can define default methods in interface… 29
  • 30. • In Ruby, what will you do? Duck typing is flexible, but it doesn't mean that you don't need further thinking… 30
  • 31. Generics • Compilers are really noisy… Shut up, Please! I know what I am doing, OK? 31
  • 32. • Because you tell it to shut up …. • Generics saves the world? ClassCastException? It’s all your fault. Hey, you're doing something stupid, and you can't tell me to shut up…YA!! 32
  • 33. • Generics destroys the code!! By Mario Fusco, http://www.slideshare.net/mariofusco/monadic-java 33
  • 34. • The loading of type declaration is taken by API developers. The API clients get convenience. • And the work of type checking is pushed to compiler-time. You avoid the runtime exception - ClassNotFoundException. Parametric polymorphism 34
  • 35. Who is responsible for type checking? • Supporters of statically-typed languages – Compilers perform comprehensive type checking at compiler-time. Ensure that some errors due to wrong types can't happen at runtime. • Supporters of dynamically-typed languages – Passing appropriate types to the function provides no guarantee that the result of the function will be the correct value. Unit Test can ensure types and behavior is correct. 35
  • 36. Static Typing vs Unit Testing • Compilers do type assertion cheaply or even for free – if you have good type inference. • The more type-error messages, the more you have to think about types to pass compilers. • It's especially true when you're writing Haskell. 36
  • 37. • When using dynamically-typed languages, you ease the burden of declaring types, but were bent with the burden of type checking. • Do you know … – Real requirements of type checking in your code? – What types are in the language? – How to do unit test for type checking? • Would you like and have the ability to write more unit test to ensure types and behavior is correct? • Are you writing unit test? 37
  • 38. Type checking Why does fill use repond_to?, not is_a? 38
  • 39. • How to unit test doubleMe? • Double whom? Fixnum? String? Array? Range? Ouch …it doesn't define :+ method. • If an object doesn't define :+ method, how to deal with? method_missing? • Ask yourself more questions about types when unit test. 39
  • 40. Joshua Bloch "Given how hard it is to write correct programs, we need all the help we can get. So anything that removes potential bugs from our plate is good. That’s why I’m very much a believer in static typing and in static analysis" 《Coders at work》 40
  • 41. • Figure out your requirements of type checking, look for tools in Ruby eco-system like … – python-rightarrow – PEP-3107 -- Function Annotations 41
  • 43. • Or even something like TypeScript – tsc compile the code to JavaScript … – A dynamically-typed language but … 43
  • 44. It's almost time to draw a conclusion • One more question, left for you… • Think about difference between checked exceptions and unchecked exceptions, an exclusive feature in Java … XD • It'll help you think about how to deal with exceptions in Ruby … 44
  • 45. Making us better at Ruby • Using dynamically-typed languages needs understanding statically-typed languages. • Knowing the advantages of statically-typed languages furthers learning pros and cons of dynamically-typed languages. • Recognizing the restrictions of statically-typed languages helps digging the responsibilities you should take in dynamically-typed languages. 45
  • 46. • With all these great power in Ruby … – Dynamically-typed – Object individuation – Open class – Runtime introspection – … • What responsibility should you take? 46
  • 47. "We're all consenting adults here." NOT just a slogan.
  • 48. With great power comes great responsibility. Spider-Man (2002) 48