Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Understanding Typing. Understanding Ruby.

3,953 views

Published on

- 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.

你可以在以下鏈結找到中文說明:
http://www.codedata.com.tw/social-coding/understanding-typing-understanding-ruby/

Published in: Technology, Education

Understanding Typing. Understanding Ruby.

  1. 1. Understanding Typing, Understanding Ruby Justin Lin caterpillar@openhome.cc http://openhome.cc
  2. 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
  3. 3. Speaker? http://www.linkedin.com/in/caterpillar 3
  4. 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. 5. • Before typing … you have to figure out – the set of related values… – and the set of their related operation… 5
  6. 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. 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. 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. 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. 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. 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. 12. • The verbose Java also do type inference somewhere, even not perfect … XD 12 List<Person> Person
  13. 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. 14. • Type declaration also provides type information. • For clearness, dynamically-typed languages may need type information, no matter it‘s through libraries … 14
  15. 15. • comments … • or even naming conventions. 15
  16. 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. 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. 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. 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. 20. Overloading in Dynamically-typed languages? • Runtime type/property checking. • Default values/option object. 20
  21. 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. 22. Duck typing • Quack like a duck… • Statically-typed languages can't do that? Structural typing 22
  23. 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. 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. 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. 26. • After you enjoy its convenience, refactor the code … 26
  27. 27. Instances of different classes? • In Java, we'll define an interface for the set of operations. 27
  28. 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. 29. • From JDK8, we can define default methods in interface… 29
  30. 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. 31. Generics • Compilers are really noisy… Shut up, Please! I know what I am doing, OK? 31
  32. 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. 33. • Generics destroys the code!! By Mario Fusco, http://www.slideshare.net/mariofusco/monadic-java 33
  34. 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. 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. 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. 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. 38. Type checking Why does fill use repond_to?, not is_a? 38
  39. 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. 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. 41. • Figure out your requirements of type checking, look for tools in Ruby eco-system like … – python-rightarrow – PEP-3107 -- Function Annotations 41
  42. 42. – IDE Support 42
  43. 43. • Or even something like TypeScript – tsc compile the code to JavaScript … – A dynamically-typed language but … 43
  44. 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. 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. 46. • With all these great power in Ruby … – Dynamically-typed – Object individuation – Open class – Runtime introspection – … • What responsibility should you take? 46
  47. 47. "We're all consenting adults here." NOT just a slogan.
  48. 48. With great power comes great responsibility. Spider-Man (2002) 48
  49. 49. Understanding Typing, Understanding Ruby. Thanks!! 49

×