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

iOS Worst Practices (And How To Avoid Them)

iOS Worst Practices (And How To Avoid Them)

Learn everything you need to know about writing bad code... and how to fix and prevent it in your own project. This talk will identify the common pitfalls, anti-patterns, and red flags that commonly plague an Objective-C codebase, ranging from cosmetic nitpicks and shibboleths to more serious, egregious offenses. Is your code base above reproach? Come and find out for yourself!

D29bb4d2d2f2ba2c2fb5a329e1e4651f?s=128

Mattt Thompson

April 20, 2013
Tweet

Transcript

  1. Objective-C Worst Practices VXXXXXXXXXXZ And How To Avoid Them

  2. • Aesthetics • Shibboleths • Verbosity • Responsibility • Privilege

    • Logistics
  3. Aesthetics

  4. Formatting • Consistency • Consistency • Consistency

  5. • Tabs vs. Spaces • 2 / 4 Space Tabs

    • Space after -, (, or ) • Newline after { or }
  6. Does Not Matter

  7. GitHub Objective-C Style Guide https://github.com/github/ objective-c-conventions

  8. #Pragma #pragma mark - UIViewController

  9. #pragma Sections • initializers • overridden subclass methods • protocols

  10. typedef-ing Block Parameters

  11. - (void)performBlock:(XXBlock)block

  12. - (void)performBlock: (void (^)(NSError *error))block

  13. Shibboleths

  14. Gileadites Ephraimites ʃɪbəlεθ sɪbəlεθ

  15. Deutsch English ɪx iʃ

  16. Ruby Ruby Objective-C underscore_variable_name camelCaseVariableName def short(options = {}) -

    (void)longMethodWithParameter: andAnotherParameter: CAPITALIZED_CONSTANT kCamelCaseConstant Module::Class MNClass
  17. Java Java AbstractLinkedHashMapFactorySingletonBean Objective-C NSAbstractLinkedHashMapFactorySingletonBean

  18. Java Java AbstractLinkedHashMapFactorySingletonBean Objective-C NSDictionary

  19. Using Strings Instead of Enumerated Constants

  20. Ruby Ruby Objective-C :symbol kNSConstant

  21. Bonus Points NS_ENUM NS_OPTIONS

  22. Using Literal Strings Instead of Constants

  23. @{@"error": @"Foo Bar Baz"}

  24. NSString * const kErrorKey = @"error"; @{kErrorKey: @"Foo Bar Baz"}

  25. Using Literal Strings Instead of Localized Strings

  26. NSLocalizedString()

  27. Not Using Foundation Built-in Types

  28. • NSSet • NSCache • NSLocale • NSSortDescriptor • NSPredicate

    • NSURL • NSURLRequest • NSURLResponse • NSURLCredential Foundation Favorites
  29. ( N S < ❤

  30. Responsibility

  31. T M I oo uch nfo

  32. X X X X Censored

  33. Private Category @interface FooViewController () ... @end

  34. Private Category @interface FVC () <NSProtocol>

  35. • NSOperationQueue • NSURLProtocol • NSPersistentStoreCoordinator

  36. Mutable Properties X X X X Censored

  37. Proxying Complex Internal Objects

  38. cell.text = @"Guten Tag!";

  39. cell.text = @"Guten Tag!"; cell.textColor = [...]; cell.font = [...];

  40. UILabel "I got this"

  41. cell.textLabel.text = @"Guten Tag!";

  42. Privilege

  43. No Classes should require insider knowledge to use

  44. Delegates

  45. Block Callbacks

  46. Notifications

  47. Logistics

  48. None
  49. platform :ios, “5.0” pod “AFNetworking” pod “AFIncrementalStore”

  50. MAINTAIN SANITY AND USE COCOAPODS

  51. Open-Source Projects

  52. Not Having README Screenshots Sample Project or and

  53. ?

  54. Please, please, stop making Swiss Army Knife category Libraries

  55. • NSArray -firstObject • NSString -URL • UIView -setLeft:/-setSize:/etc. •

    UIImage -scaledImage...
  56. tl;dr

  57. • Make your code sparkle • Follow Apple's conventions •

    Delegate responsibilities • Use CocoaPods