Objective-C: Good and Bad
Good
LighweightSimply preprocessor and small runtimeSyntax sugar over C, easy to learn
RuntimeCompletely dynamic message sending. Dont need inheritance to call a method with different objectsTrue introspection...
Categories and ProxiesAllows addition of methods to another classEven a class you dont have source forAlso allows overridi...
Key Value Coding/ObservingBind string names with object invariants at  run-timeCall back when the value of some invariant ...
Self-contained objectsObjects tend to be handful and smart enough to be used standalone
#importNo include guards or “#pragma once” anymore
[NSBundle classNamed]Create a class by name, which could be used later to instantiate objects
BlocksClosures are awesomeAllows to define a block of code with  surrounding context and pass it to other  functionFacilit...
Bad
SyntaxLots of square braces. Compare:  [[[MyClass alloc] init:[foo bar]] autorelease]  MyClass.alloc.init(foo.bar).autorel...
LighweightThe same code has to be repeated oftenLittle language support for anything other  than message sendingFolks coul...
Memory ManagementHalf-manual, half-automaticRaw pointers are evilNo garbage collectorReference counters overheadMigration ...
Allocation and InitializationWhich are two separated operations MyClass *object = [MyClass alloc] init];Multiple construct...
No Stack ObjectsDealloc wont be called automatically
Messaging nilDoes nothing and hides real bugsWouldnt be so bad if it were easy to make messaging nil screamCocoa messages ...
Overriding and OverloadingCategories are broken: overriding a method more than once leads to undefined behaviorNo method o...
No NamespacesNot a major gripe, but Its real handy
Id should be id *Not clear: NSString *foo = @”foo”; id bar = foo;Better: NSString *foo = @”foo”; id *bar = foo;
So So
FrameworksCould be a problem, if Cocoa wasnt the best one publicly available
Type SafetyCant catch lots of errors during the compile timeBut allows its awesome runtime features
Questions?
Upcoming SlideShare
Loading in …5
×

Objective-C: Good and Bad

1,771 views

Published on

Published in: Technology
  • Be the first to comment

Objective-C: Good and Bad

  1. 1. Objective-C: Good and Bad
  2. 2. Good
  3. 3. LighweightSimply preprocessor and small runtimeSyntax sugar over C, easy to learn
  4. 4. RuntimeCompletely dynamic message sending. Dont need inheritance to call a method with different objectsTrue introspection. Allows to watch class hierarchy, check if an object has a particular invariant or implements some method, call a method by name
  5. 5. Categories and ProxiesAllows addition of methods to another classEven a class you dont have source forAlso allows overriding of existing methods
  6. 6. Key Value Coding/ObservingBind string names with object invariants at run-timeCall back when the value of some invariant is going to be / was changed
  7. 7. Self-contained objectsObjects tend to be handful and smart enough to be used standalone
  8. 8. #importNo include guards or “#pragma once” anymore
  9. 9. [NSBundle classNamed]Create a class by name, which could be used later to instantiate objects
  10. 10. BlocksClosures are awesomeAllows to define a block of code with surrounding context and pass it to other functionFacilitates concurrency
  11. 11. Bad
  12. 12. SyntaxLots of square braces. Compare: [[[MyClass alloc] init:[foo bar]] autorelease] MyClass.alloc.init(foo.bar).autoreleaseKeyed parameters: difficult to reorder them during refactoring
  13. 13. LighweightThe same code has to be repeated oftenLittle language support for anything other than message sendingFolks could say “because theres no magic”. But more code means more bugsWithout Cocoa this “no magic” would be absolutely awful
  14. 14. Memory ManagementHalf-manual, half-automaticRaw pointers are evilNo garbage collectorReference counters overheadMigration to automatic reference counting takes time
  15. 15. Allocation and InitializationWhich are two separated operations MyClass *object = [MyClass alloc] init];Multiple constructors work via designated initializers without any language supportself = [super init]; //assignment to self :( if (self) { //init invariants } return self;
  16. 16. No Stack ObjectsDealloc wont be called automatically
  17. 17. Messaging nilDoes nothing and hides real bugsWouldnt be so bad if it were easy to make messaging nil screamCocoa messages nil as a matter of course
  18. 18. Overriding and OverloadingCategories are broken: overriding a method more than once leads to undefined behaviorNo method overload. Need to mangle names manually
  19. 19. No NamespacesNot a major gripe, but Its real handy
  20. 20. Id should be id *Not clear: NSString *foo = @”foo”; id bar = foo;Better: NSString *foo = @”foo”; id *bar = foo;
  21. 21. So So
  22. 22. FrameworksCould be a problem, if Cocoa wasnt the best one publicly available
  23. 23. Type SafetyCant catch lots of errors during the compile timeBut allows its awesome runtime features
  24. 24. Questions?

×