Advertisement
ShotgunDebugging

Slide transcript of André Peng's Foundations of Objective-C

Nov 24th, 2012
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. Foundations of Objective-C
  2. André Peng
  3.  
  4. --
  5.  
  6. Get out a piece of paper
  7. Write a factorial function. Use whatever language you like
  8.  
  9. Don't cheat
  10.  
  11. int fact(int n)
  12. {
  13. /* Write your code here */
  14. }
  15.  
  16. fact(3) = 3 * 2 * 1 == 6
  17. fact(4) = 4 * 3 * 2 * 1 == 24
  18. fact(5) = 5 * 4 * 3 * 2 * 1 == 128
  19.  
  20. fact(0) = 1
  21.  
  22. --
  23. What is an object?
  24.  
  25. int i;
  26. i = 0xdeadbeef
  27. [de][ad][be][ef]
  28.  
  29. --
  30.  
  31. typedef struct _IntContainer {
  32. int i;
  33. } IntContainer
  34.  
  35. IntContainer ic;
  36.  
  37. ic.i=0xdeadbeef;
  38.  
  39. [de][ad][be][ef]
  40.  
  41. --
  42.  
  43. What is an object?
  44.  
  45. typedef struct _NSPoint {
  46. CGFloat x;
  47. CGFloat y;
  48. } NSPoint;
  49.  
  50. NSPoint p;
  51.  
  52. p.x = 1.0; p.y = 2.0;
  53.  
  54. [3f][80][00][00][00][40][00][00][00]
  55. x y
  56.  
  57.  
  58. --
  59.  
  60. int *pi;
  61. *pi = 0xdeadbeef;
  62.  
  63. pi [08][03][91][70]
  64. 0x08039170 [de][ad][be][ef]
  65.  
  66. --
  67.  
  68. struct NSObject {
  69. Class isa;
  70. }
  71.  
  72. --
  73.  
  74. struct NSObject {
  75. struct objc_class* isa;
  76. }
  77.  
  78.  
  79. struct objc_class {
  80. Class isa;
  81. Class super_class;
  82. const char* name;
  83. long version;
  84. long info;
  85. long instance_size;
  86. struct objc_ivar_list *ivars;
  87. struct objc_method_list **methodLists;
  88. struct objc_cache *cache;
  89. struct objc_protocol_list *protocols;
  90. }
  91.  
  92. (gdb) p NSApp
  93. $2 = (struct objc_object *)
  94. 0x8039170
  95.  
  96. (gdb) p *NSApp
  97. $3 = {
  98. isa = 0x15f8e0
  99. }
  100.  
  101. (gdb) p NSApp->isa
  102. $4 = (struct objc_class *)
  103. 0x15f8e0
  104. (gbd) p *NSApp->isa
  105. $5 = {
  106. isa = 0x160de0,
  107. super_class = 0x22d3a0,
  108. name = 0x1322de "RWApplication",
  109. version = 0,
  110. info = 12206145,
  111. instance_size = 100,
  112. ivars = 0x169720,
  113. methodLists = 80391e0,
  114. cache = 809d710,
  115. protocols = 15b064
  116. }
  117.  
  118. --
  119.  
  120. @interface NSObject {
  121. class isa;
  122. }
  123.  
  124. @interface MySubClass : NSObject {
  125. int i;
  126. }
  127.  
  128. @interface MySubSubClass : MySubClass {
  129. float f;
  130. }
  131.  
  132. struct NSObject {
  133. Class isa;
  134. }
  135.  
  136. struct MySubClass {
  137. Class isa;
  138. int i;
  139. }
  140.  
  141. struct MySubSubClass {
  142. Class isa;
  143. int i;
  144. float f;
  145. }
  146.  
  147. --
  148.  
  149. Messaging
  150.  
  151. --
  152.  
  153. @implementation NSMutableString
  154. (void) appendString:(NSString*)aString
  155. {
  156. ...;
  157. }
  158.  
  159. @end
  160.  
  161. =>
  162.  
  163. void | NSMutableString appendString:|(id self, SEL _cmd, NSString* aString)
  164. {
  165. ...;
  166. }
  167.  
  168. --
  169.  
  170. % nm /usr/lib/libSystem.dylib | grep strcmp
  171. 06093b0 T _strcmp
  172.  
  173. nm Foundation.framework/Foundation | grep NSString compare
  174. 0C02bbf0 t -|NSString compare:|
  175. 0C06c200 t -|NSString compare:options|
  176. 0C00d480 t -|NSString compare:options:range|
  177. 0C00d4e0 t -|NSString compare:options:range:locale:|
  178.  
  179. --
  180.  
  181. I'm sorry that I long ago coined the term "objects" for this topic because it gets many people to focus on the lesser idea.
  182.  
  183. The big idea is "messaging".
  184. - Alan Kay
  185.  
  186. --
  187.  
  188. http://www.paulgraham.com/power.html
  189.  
  190. --
  191.  
  192. UNIFORMITY IS POWER
  193.  
  194. --
  195.  
  196. for(int i = 0; i<=10; i++) {
  197. NSLog(@"%d", i);
  198. }
  199.  
  200. 1 to:10 do:[:i|
  201. Transcript show: (i asString);
  202. ].
  203.  
  204. --
  205.  
  206. Everything is an object, including code.
  207. Messaging is the exclusive means of object communication.
  208.  
  209. --
  210.  
  211. if (x < y) {
  212. foo;
  213. } else {
  214. bar;
  215. }
  216.  
  217. (x < y) ifTrue: [
  218. foo
  219. ] ifFalse: [
  220. bar
  221. ]
  222.  
  223. --
  224.  
  225. Simpler to reason about
  226. Simpler to implement
  227. Re-think the problem
  228.  
  229. --
  230.  
  231. NSArray* array = ...;
  232. |array do:^(id obj){ NSLog("%@", obj) }];
  233. |NSThread performOnMainThread:^{ ... }]
  234. |array pdo:^(id obj){ NSLog("%@", obj) }];
  235.  
  236. --
  237.  
  238. Alonzo &
  239. Alan &
  240. Stephen &
  241. Kurt.
  242.  
  243. --
  244.  
  245. f(x) = x * 2
  246. f(3) == 6
  247.  
  248. λ x. x * 2
  249. λ (x. x * 2) 3 == 6
  250.  
  251. --
  252.  
  253. x
  254. λ...
  255. (λ...) y
  256.  
  257. --
  258.  
  259. 0 := λ f x. x
  260. 1 := λ f x. f x
  261. 2 := λ f x. f (f x)
  262. 3 := λ f x. f (f (f x))
  263. PLUS := λ m n f x. n f (m f x)
  264. MULT := λ m n f. n (n f)
  265. TRUE := λ x y. x
  266. FALSE := λ x y. y
  267. AND := λ p q. p q p
  268. OR := λ p q. p q p
  269. NOT := λ p a b. p b a
  270. IFTHENELSE := λ p a b. p a b
  271. PAIR := λ x y f. f x y
  272. FIRST := λ p. p TRUE
  273. SECOND := λ p. p FALSE
  274. NIL := λ x. TRUE
  275. NOTNIL := λ p. p (λx y. FALSE)
  276.  
  277. --
  278.  
  279. Get out a piece of paper
  280. Write a factorial function. Use whatever language you like
  281.  
  282. Don't cheat
  283.  
  284. int fact(int n)
  285. {
  286. /* Write your code here */
  287. }
  288.  
  289. fact(3) = 3 * 2 * 1 == 6
  290. fact(4) = 4 * 3 * 2 * 1 == 24
  291. fact(5) = 5 * 4 * 3 * 2 * 1 == 128
  292.  
  293. fact(0) = 1
  294.  
  295. --
  296.  
  297. int fact(int n)
  298. {
  299. int result = 1;
  300. int i;
  301. for(int i=0; i<=n; i++)
  302. {
  303. result *=i;
  304. }
  305.  
  306. return result;
  307. }
  308.  
  309. int fact(int n)
  310. {
  311. if (n == 0) return 1;
  312. else return n * fact(n-1);
  313. }
  314.  
  315. --
  316.  
  317. Though OOP came from many motivations, two were central... the small scale one was to find a more flexible version of assignment, and then try to eliminate it altogether.
  318. - Alan Kay
  319.  
  320. Assignment statements - even abstract ones - express very low-level goals, and moreof them will be needed to get anything done. Generally, we don't want the programmer to be messing around with state, whether simulated or not.
  321. - Alan Kay
  322.  
  323. --
  324.  
  325. Pure functions
  326. Give the same output when given the same inputs
  327. Have no observable side-effects
  328.  
  329. --
  330.  
  331. f(x) = x * 2
  332. f(string) = newString
  333.  
  334. --
  335.  
  336. Pure functions
  337. Are far, far easier to reason about (i.e. maintain!)
  338. Are far more composable than objects+methods
  339. They are more uniform
  340. They are smaller
  341. Naturally concurrent
  342.  
  343. --
  344.  
  345. Lessons for Objective C
  346. Minimal state, in both methods and objects
  347. Every |var you add is a burden
  348. Design your data so that invalid state is impossible
  349. Use const liberally in methods
  350. Prefer immutable objects over mutable
  351.  
  352. --
  353.  
  354. Lessons for Objective C
  355. If you need a comment to explain something, move the code into a new function
  356. Search for uniformity, in data design and method calls
  357.  
  358. --
  359.  
  360. Thanks!
  361. André Peng
  362. W http://algorithm.com.au
  363. E ozone@algorithm.com.au
  364. T http://twitter.com/andrepang
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement