Objective-C Sins

Objective-C Sins

I have a confession to make. I've written some terrible Objective-C code. I guess you can say I've sinned. In this talk, I discuss common anti-patterns and how to avoid them.

This talk was originally presented to students of the iOS class at The Flatiron School. Without the actual talking to go with it, these slides are probably missing a lot of context. If you're confused about something, send me a message on twitter (@mb).

23ad4e437c3ece1b7a04696321036c30?s=128

Matthew Bischoff

October 29, 2013
Tweet

Transcript

  1. None
  2. Objective-C Sins

  3. sin 1 |sin| noun an immoral act considered to be

    a transgression against divine law: a sin in the eyes of God | the human capacity for sin. ! • an act regarded as a serious or regrettable fault, offense, or omission: he committed the unforgivable sin of refusing to give interviews | humorous : with air like this, it's a sin not to go out.
  4. sin 1 |sin| noun an immoral act considered to be

    a transgression against divine law: a sin in the eyes of God | the human capacity for sin. ! • an act regarded as a serious or regrettable fault, offense, or omission: he committed the unforgivable sin of refusing to give interviews | humorous : with air like this, it's a sin not to go out.
  5. @mb

  6. matthew bischoff

  7. matt

  8. None
  9. Velocity Quotebook

  10. None
  11. None
  12. Sins

  13. [object retainCount];

  14. None
  15. Why not?

  16. Object retainCount? Actual retainCount [NSNumber numberWithInt:1] 1 2 @"Matt" 1

    1152921504606846975 [NSString stringWithString:@"Matt"] 1 1152921504606846975
  17. Key-Value Observing

  18. Key-Value Observing has the worst API in all of Cocoa.

    It’s awkward, verbose, and confusing. Mattt Thompson
  19. “Stringly Typed”

  20. Refactoring is scary.

  21. Throws Exceptions

  22. ARC == garbage collection

  23. You still have to pay attention.

  24. . Syntax [ ] Syntax

  25. What’s the obvious thing to do, the easiest way to

    agree? Just do what the headers say. If it’s a property, treat it as such. If it’s not, don’t. ! And when in doubt, look it up. Brent Simmons
  26. Dot syntax is for properties.

  27. Bracket syntax is for methods.

  28. UIColor.clearColor; array.count; object.hash;

  29. More than 1 Class per file

  30. None
  31. performSelector: afterDelay:

  32. It’s a crash waiting to happen.

  33. Missing or improper reuse

  34. Always reuse cells.

  35. Don’t keep track of cells.

  36. Architecture-dependent primitive types

  37. int float double

  38. NSInteger / NSUInteger CGFloat NSTimeInterval

  39. Violating MVC

  40. Models that do networking

  41. Views that know about models

  42. Controllers that do layout

  43. NSDictionary

  44. Not a replacement for a model

  45. ✨"

  46. Magic numbers

  47. Repeating yourself Repeating yourself Repeating yourself Repeating yourself Repeating yourself

    Repeating yourself
  48. Every piece of knowledge must have a single, unambiguous, authoritative

    representation within a system. Andy Hunt and Dave Thomas
  49. if (taylorSwift) { taylorSwift.age = 22; }

  50. id

  51. Singletons

  52. Blocking the main queue

  53. Low level APIs

  54. CoreFoundation CoreText CoreGraphics CoreAnimation

  55. Foundation TextKit UIKit UIView Animations

  56. @property (nonatomic, strong, readwrite) NSString *title;

  57. @property (nonatomic) NSString *title;

  58. @property NSString *title;

  59. Warnings

  60. Unused Variable ‘sin’ !

  61. Unused Variable ‘sin’ !

  62. Public and readwrite

  63. Missing #pragma marks

  64. #pragma mark - NSObject ! - (void)dealloc {} ! #pragma

    mark - UIViewController ! - (void)viewDidLoad {} ! #pragma mark - LCKDocumentsTableViewController ! - (void)setupRefreshControl {}
  65. Bad names

  66. UIImage *img; UIButton *btn; NSString *str; NSArray *arr; NSDictionary *dict;

    NSInteger i;
  67. UIImage *avatar; UIButton *signupButton; NSString *name; NSArray *people; NSDictionary *userInfo;

    NSInteger count;
  68. Two-letter prefixes

  69. NS UI CG CA AC ST XC SK MF AD

  70. Two-letter prefixes…are reserved by Apple for use in framework classes.

    ! Your own classes should use three letter prefixes.
  71. Inconsistent style

  72. None
  73. Not overriding the designated initializer

  74. - (instancetype)initWithPerson:(LCKPerson *)person { self = [super initWithStyle:UITableViewStylePlain]; if (self)

    { _person = person; _dataStore = [NSMutableDictionary dictionary]; } return self; }
  75. - (id)initWithStyle:(UITableViewStyle)style { return [self initWithPerson:nil]; } ! - (instancetype)initWithPerson:(LCKPerson

    *)person { self = [super initWithStyle:UITableViewStylePlain]; if (self) { _person = person; _dataStore = [NSMutableDictionary dictionary]; } return self; }
  76. UITapGestureRecognizer

  77. Just use a UIButton.

  78. UISwipeGestureRecognizer

  79. Just use a UIPanGestureRecognizer.

  80. Inaccessibility

  81. None
  82. Property Description Example accessibilityLabel Identifies the accessibility element, but does

    not include the type of the control or view “Share” accessibilityValue The value is a localized string that contains the current value of an element “35 %” accessibilityHint Description of the result of performing an action on the element. “Selects the message”
  83. Missing Localization

  84. None
  85. // Unnecessary comments

  86. Comments explain why. Not what or how.

  87. true && false

  88. YES and and NO

  89. Profiling at the end

  90. None
  91. [[[[[too much] code] on] one] line]

  92. #import <objc/runtime.h>

  93. #define

  94. retain cycles

  95. Object Object strong

  96. [self.queue addOperationWithBlock:^{ [self download]; }];

  97. __weak typeof(self) weakSelf = self; [self.queue addOperationWithBlock:^{ [weakSelf download]; }];

  98. (assign) delegates

  99. self.webView.delegate = nil;

  100. Blended layers

  101. None
  102. None
  103. Mixing up nothing

  104. nil, Nil, NULL, NSNull

  105. Symbol Value Meaning NULL (void *)0 literal null value for

    C pointers nil (id)0 literal null value for Objective-C objects Nil (Class)0 literal null value for Objective-C classes NSNull [NSNull null] singleton object used to represent null
  106. Ignoring crash logs

  107. None
  108. Special Thanks •Ash Furrow •Brent Simmons •Mattt Thompson •Brian Capps

  109. Thank you.

  110. Questions?