Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Nullability: между добром и злом

Nullability: между добром и злом

Tagir Valeev

May 24, 2018
Tweet

More Decks by Tagir Valeev

Other Decks in Technology

Transcript

  1. 2

  2. 4

  3. 5

  4. 6

  5. 8

  6. 9

  7. 10

  8. 11

  9. 12

  10. 13

  11. 16

  12. 17

  13. 18

  14. 19

  15. 20

  16. 21

  17. 22

  18. 23

  19. 24

  20. 25

  21. §6.5.3.3 The unary * operator denotes indirection. If the operand

    points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object. If the operand has type ‘‘pointer to type’’, the result has type ‘‘type’’. If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined. 84) 84) ... Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime. ISO/IEC 9899 International Standard – Programming Languages – C 27
  22. §6.5.3.3 The unary * operator denotes indirection. If the operand

    points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object. If the operand has type ‘‘pointer to type’’, the result has type ‘‘type’’. If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined. 84) 84) ... Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime. ISO/IEC 9899 International Standard – Programming Languages – C 28
  23. 31

  24. I call it my billion-dollar mistake. It was the invention

    of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. Tony Hoare, 2009 32
  25. 33

  26. 34

  27. 35

  28. 36

  29. 38

  30. 39

  31. 40

  32. 41

  33. 42

  34. 43

  35. 44

  36. 45

  37. 46

  38. 47

  39. 48

  40. 49

  41. 50

  42. 51

  43. 52

  44. 53

  45. 54

  46. 55

  47. public class Person { private String name; private String surname;

    public Person(Company company) :name(""), surname("") { company.chief = this; } ... } 56
  48. 58

  49. 59

  50. 60

  51. 61

  52. 62

  53. 63

  54. 64

  55. 65

  56. 66

  57. 67

  58. 68

  59. 69

  60. 70

  61. 71

  62. 72

  63. 73

  64. 74

  65. 75

  66. 76

  67. 77

  68. 78

  69. 79

  70. 80

  71. 81

  72. 82

  73. 83

  74. 84

  75. 85

  76. 86

  77. 87

  78. 88

  79. 89

  80. 90

  81. 91

  82. 92

  83. 93

  84. 94

  85. 95

  86. 96

  87. 97

  88. 98

  89. 99

  90. 100

  91. 101

  92. 102

  93. GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help Prelude> :l ParentChild.hs

    [1 of 1] Compiling ParentChild (ParentChild.hs, interpreted ) Ok, modules loaded: ParentChild. *ParentChild> (p, c) = create "Курица" "Яйцо" *ParentChild> p Parent named Курица *ParentChild> c Child named Яйцо *ParentChild> child p Child named Яйцо *ParentChild> parent c Parent named Курица *ParentChild> child (parent (child p)) Child named Яйцо 103
  94. Выводы • Используйте статическую типизацию • Прорабатывайте систему типов •

    Используйте статический анализ • Верьте спецификации, а не реализации • Учитесь пользоваться и не пользоваться новыми API • Изучайте языки программирования • Расширяйте кругозор 104