Understanding Typing. Understanding Ruby.

Justin Lin
Justin LinTechnology / Community Evangelist at Free lancer
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
1 of 49

Recommended

Learning typescript by
Learning typescriptLearning typescript
Learning typescriptAlexandre Marreiros
3.5K views37 slides
TypeScript by
TypeScriptTypeScript
TypeScriptUdaiappa Ramachandran
2.1K views24 slides
TypeScript Best Practices by
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practicesfelixbillon
3K views57 slides
Typescript Basics by
Typescript BasicsTypescript Basics
Typescript BasicsManikandan [M M K]
135 views44 slides
Typescript Fundamentals by
Typescript FundamentalsTypescript Fundamentals
Typescript FundamentalsSunny Sharma
2K views30 slides
TypeScript 101 by
TypeScript 101TypeScript 101
TypeScript 101rachelterman
1.8K views13 slides

More Related Content

What's hot

TypeScript . the JavaScript developer best friend! by
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!Alessandro Giorgetti
981 views32 slides
Power Leveling your TypeScript by
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScriptOffirmo
712 views50 slides
TypeScript - An Introduction by
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An IntroductionNexThoughts Technologies
5.2K views52 slides
Getting Started with TypeScript by
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScriptGil Fink
709 views29 slides
TypeScript intro by
TypeScript introTypeScript intro
TypeScript introAts Uiboupin
1.6K views36 slides

What's hot(20)

Power Leveling your TypeScript by Offirmo
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScript
Offirmo712 views
Getting Started with TypeScript by Gil Fink
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScript
Gil Fink709 views
Typescript ppt by akhilsreyas
Typescript pptTypescript ppt
Typescript ppt
akhilsreyas13.8K views
TypeScript Modules by Noam Kfir
TypeScript ModulesTypeScript Modules
TypeScript Modules
Noam Kfir773 views
Introduction to TypeScript by Winston Levi by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston Levi
Winston Levi492 views
Live Typing - California Smalltalkers by Hernan Wilkinson
Live Typing - California SmalltalkersLive Typing - California Smalltalkers
Live Typing - California Smalltalkers
Hernan Wilkinson558 views
TypeScript introduction to scalable javascript application by Andrea Paciolla
TypeScript introduction to scalable javascript applicationTypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript application
Andrea Paciolla403 views
Getting started with typescript and angular 2 by Knoldus Inc.
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2
Knoldus Inc.4.4K views
Beginning Java for .NET developers by Andrei Rinea
Beginning Java for .NET developersBeginning Java for .NET developers
Beginning Java for .NET developers
Andrei Rinea18.4K views
Typescript in 30mins by Udaya Kumar
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
Udaya Kumar876 views
Grooming with Groovy by Dhaval Dalal
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
Dhaval Dalal4.1K views
A Static Type Analyzer of Untyped Ruby Code for Ruby 3 by mametter
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
mametter758 views

Similar to Understanding Typing. Understanding Ruby.

Java introduction by
Java introductionJava introduction
Java introductionGaneshKumarKanthiah
56 views36 slides
web programming UNIT VIII python by Bhavsingh Maloth by
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh MalothBhavsingh Maloth
863 views62 slides
Tutorial on-python-programming by
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programmingChetan Giridhar
1.4K views62 slides
Software testing (2) trainingin-mumbai by
Software testing (2) trainingin-mumbaiSoftware testing (2) trainingin-mumbai
Software testing (2) trainingin-mumbaivibrantuser
70 views21 slides
Software testing (2) trainingin-mumbai... by
Software testing (2) trainingin-mumbai...Software testing (2) trainingin-mumbai...
Software testing (2) trainingin-mumbai...vibrantuser
31 views22 slides
Grails Spock Testing by
Grails Spock TestingGrails Spock Testing
Grails Spock TestingTO THE NEW | Technology
6.4K views44 slides

Similar to Understanding Typing. Understanding Ruby.(20)

web programming UNIT VIII python by Bhavsingh Maloth 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
Bhavsingh Maloth863 views
Tutorial on-python-programming by Chetan Giridhar
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
Chetan Giridhar1.4K views
Software testing (2) trainingin-mumbai by vibrantuser
Software testing (2) trainingin-mumbaiSoftware testing (2) trainingin-mumbai
Software testing (2) trainingin-mumbai
vibrantuser70 views
Software testing (2) trainingin-mumbai... by vibrantuser
Software testing (2) trainingin-mumbai...Software testing (2) trainingin-mumbai...
Software testing (2) trainingin-mumbai...
vibrantuser31 views
Software Engineering Thailand: Programming with Scala by Brian Topping
Software Engineering Thailand: Programming with ScalaSoftware Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with Scala
Brian Topping929 views
F# for startups by joelgrus
F# for startupsF# for startups
F# for startups
joelgrus2.2K views
An Overview of automated testing (1) by Rodrigo Lopes
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)
Rodrigo Lopes222 views
Java Closures by Ben Evans
Java ClosuresJava Closures
Java Closures
Ben Evans1.8K views
Polyglot and Functional Programming (OSCON 2012) by Martijn Verburg
Polyglot and Functional Programming (OSCON 2012)Polyglot and Functional Programming (OSCON 2012)
Polyglot and Functional Programming (OSCON 2012)
Martijn Verburg2.7K views
Introduction to the intermediate Python - v1.1 by Andrei KUCHARAVY
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1
Andrei KUCHARAVY847 views
Programming Languages #devcon2013 by Iván Montes
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
Iván Montes1.2K views

More from Justin Lin

Ch14 簡介 Spring Boot by
Ch14 簡介 Spring BootCh14 簡介 Spring Boot
Ch14 簡介 Spring BootJustin Lin
872 views22 slides
Ch13 整合 Spring MVC/Security by
Ch13 整合 Spring MVC/SecurityCh13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityJustin Lin
280 views58 slides
Ch12 Spring 起步走 by
Ch12 Spring 起步走Ch12 Spring 起步走
Ch12 Spring 起步走Justin Lin
274 views31 slides
Ch11 簡介 JavaMail by
Ch11 簡介 JavaMailCh11 簡介 JavaMail
Ch11 簡介 JavaMailJustin Lin
157 views8 slides
Ch10 Web 容器安全管理 by
Ch10 Web 容器安全管理Ch10 Web 容器安全管理
Ch10 Web 容器安全管理Justin Lin
153 views30 slides
Ch09 整合資料庫 by
Ch09 整合資料庫Ch09 整合資料庫
Ch09 整合資料庫Justin Lin
233 views92 slides

More from Justin Lin(20)

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

Recently uploaded

CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TShapeBlue
38 views34 slides
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti... by
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...ShapeBlue
26 views29 slides
State of the Union - Rohit Yadav - Apache CloudStack by
State of the Union - Rohit Yadav - Apache CloudStackState of the Union - Rohit Yadav - Apache CloudStack
State of the Union - Rohit Yadav - Apache CloudStackShapeBlue
106 views53 slides
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITShapeBlue
66 views8 slides
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue by
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlueMigrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlueShapeBlue
71 views20 slides
NTGapps NTG LowCode Platform by
NTGapps NTG LowCode Platform NTGapps NTG LowCode Platform
NTGapps NTG LowCode Platform Mustafa Kuğu
28 views30 slides

Recently uploaded(20)

CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by ShapeBlue
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
ShapeBlue38 views
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti... by ShapeBlue
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
ShapeBlue26 views
State of the Union - Rohit Yadav - Apache CloudStack by ShapeBlue
State of the Union - Rohit Yadav - Apache CloudStackState of the Union - Rohit Yadav - Apache CloudStack
State of the Union - Rohit Yadav - Apache CloudStack
ShapeBlue106 views
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by ShapeBlue
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
ShapeBlue66 views
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue by ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlueMigrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
ShapeBlue71 views
NTGapps NTG LowCode Platform by Mustafa Kuğu
NTGapps NTG LowCode Platform NTGapps NTG LowCode Platform
NTGapps NTG LowCode Platform
Mustafa Kuğu28 views
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc72 views
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ... by ShapeBlue
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...
ShapeBlue61 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10345 views
Why and How CloudStack at weSystems - Stephan Bienek - weSystems by ShapeBlue
Why and How CloudStack at weSystems - Stephan Bienek - weSystemsWhy and How CloudStack at weSystems - Stephan Bienek - weSystems
Why and How CloudStack at weSystems - Stephan Bienek - weSystems
ShapeBlue81 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker48 views
DRBD Deep Dive - Philipp Reisner - LINBIT by ShapeBlue
DRBD Deep Dive - Philipp Reisner - LINBITDRBD Deep Dive - Philipp Reisner - LINBIT
DRBD Deep Dive - Philipp Reisner - LINBIT
ShapeBlue44 views
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
ShapeBlue70 views
Data Integrity for Banking and Financial Services by Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely29 views
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava... by ShapeBlue
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
ShapeBlue28 views
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De... by Moses Kemibaro
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Moses Kemibaro27 views
Business Analyst Series 2023 - Week 4 Session 7 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray1042 views
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn26 views
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue by ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
ShapeBlue62 views

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