SlideShare a Scribd company logo
ORTHOGONALITY
A strategy for reusable code
@rsebbe
Orthogonality
• Why this talk?
• I saw some frameworks with unmanageable
complexity. “We do complex stuff, so complexity
is OK”
• Most of the time, it’s not!
• Hoping to share some techniques that will help.
Orthogonality
• What is it?
• Make <features> that don’t(*) depend on each
other.
• (*) minimally depend
• Where <feature> is:
• Code - Method - Class - Library - Project - App - etc.
• Self-discipline
Orthogonality
• Why?
• Easier maintenance. Changes to some part of
the code shouldn’t affect some other part.
• Easier reading. Because less (told or untold)
dependencies.
• Easier reuse.
• Future-proof, adapts more easily.
Orthogonality
• How?
• Some prefer to “think before”. Does not really
work for me. (could work for you)
• I start from real-world needs & use-cases, not
trying to predict them *all* beforehand.
• Then I continuously challenge the design, and try
to improve it with new real-world usage, until it
settles/converges.
Levels
App
Library
Class
Method
Code
App
Library
Class
Method
• One purpose (avoid Swiss knives)
• Expose what’s strictly required
• Less state (fewer properties) & immutability
• One purpose
• Clear naming
• Option dictionaries instead of signature variants
• One purpose, keep them thematic
• Don’t expose internals
• Limit external dependencies (vertical slicing)
• One purpose
• Factor them out, make reusable libs
(horizontal slicing)
Code • One purpose code blocks, think of those as
a “flow”, avoid local hacks
• Use local variables
Orthogonality
Techniques
• Think Lego® Bricks (standard, reusable blocks), avoid custom
solutions.
• Keep as much internal things as possible. (Swift)
• Make abstractions, handle complexity internally (~ class clusters)
• Take a step back when designing APIs
• Use of static libs / frameworks / resource bundles
• Use categories
• Minimize state, favor the immutable.
API
• A meeting point.
• Insulates inside from outside.
• Keep complexity inside, there’s really no point in
exposing it.
• An API separates a feature from its implementation
choices.
Anatomy of a Method
• Method signature, required args, optional ones, return
values, completion handlers.
• Be aware of the flow & dependencies
• No internal returns. Single entry, single exit.
Global Variables
• Constants that absolutely need to be known
outside: OK
• Avoid otherwise, they break orthogonality
Local variables…
… insulate your code from the outside (think threads)
• Interactions with self have a meaning. Don’t abuse those.
Consider them as input / output for your code.
• Code with lots of reference to self or ivar is suspect.
Less State
• Each property has to be maintained over the lifetime of
the object.
• It’s like planting a tree. It’s cool & nice, until you’re
mowing the lawn around it.
• Very often, it’s easy and convenient to use a property.
But you’ll pay for it over time.
• Avoid properties if you can. Even private / internal
ones. Compute values from other state or from inputs
instead. [Exception: caching]
Immutability
• Has been there from day one in Cocoa.
• Once you get an object, it won’t change behind
your back. It stops all form of change propagation.
• Very interesting in a parallel world (GCD).
• Makes code robust.
• Trendy: Swift’s let & struct vs. class, reactive/
functional programming, etc.
Property Check List
• Do you need a property? Why?
• Internal or public?
• Read-only or read-write?
• Public read-only & internal read-write?
• Read-only & mutable or read-write & immutable?
• Public & read-only or full read-write? read-only + set
through specific method?
Method Check List
• Private or public? Internal?
• Inputs as arguments or options dictionary?
• Flow: do you really need those internal return’s?
• Are you insulating against self ?
• Isn’t it too long, woud factoring it out make more
sense?
Class/API Check List
• Do you need that class?
• Internal or public? Class cluster?
• Do you need each of those methods, public/
private?
• Do you need to include that additional framework in
the .h? Isn’t a .m inclusion enough?
• Do you compartmentalize deps with categories?
Fruit Ninja
Large App
Framework Framework
Framework
Framework
App
Framework
Horizontal Slicing
Large Framework,
w/ all kinds of dependencies
Vertical Slicing
Framework Ext A Ext B
Keep cool, man!
• Good framework, method, API design is clear for everyone. There’s
not that much room for ego or personal preferences.
• Junior could come up with a better design. That’s OK, you should
welcome it & even encourage it! Be thankful for learning new stuff.
• Search for design examples. It helps. For API design, Apple
*typically* makes effort about this. (OK, they screw up sometimes)
• When you reach good design, you typically feel you’ve achieved
something. It fits nicely together, hacks are not needed, it is easily
reusable… But don’t stop there!
• Simplicity of code often converges to the same design, even from
people with different backgrounds.
Case Studies
CeedBase
CeedVision
CeedGL
CeedMath
CeedAnalytics
CeedShare
CeedImage
CeedCamera
CeedDiagnosis
CeedOCR
CeedTranslate
CeedJPEG
CeedPageDetection
CeedUI
CeedSSL
CeedReceipt
Case Study 1: CeedOCR
• Reusable OCR library
• Possibly different OCR engines
• Mac / iOS
DEMO: Prizmo
Case Study 1: CeedOCR
• Simple API: 1 class, 1 method. OCR engines are
strings.
• Similar to class-cluster design (variety of internal
classes)
• No internal API is exposed (engine types, classes,
etc.)
• Dynamically looked up (not linked -> Class is Nil)
• Extensible through delegation (binarization)
Case Study 2: CeedAnalytics
• Reusable Analytics library. Goal: track which app
features are used, which are not.
• Possibly different backends (Google, Countly…)
• Similar solution as CeedOCR
DEMO: Prizmo
Case Study 3: CeedVision
• Has a class to perform Accelerate-based image
processing
• Variety of image source options (CoreVideo,
CoreImage, OpenCV, CoreGraphics, vImage…)
DEMO: Prizmo
Case Study 3: CeedVision
• Core features part of the class
• Input options as categories (Swift: extensions)
Case Study 4: CeedImage
• GPU Image Processing lib, similar to Core Image
(tiled rendering) but with more control (color vs.
gray, memory cache…).
• Optional features (external JPEG lib)
• We don’t want app have JPEG part if app don’t
need it (external lib dep, maintenance…)
DEMO: Hydra
Fruit Ninja
CeedImage CeedImage
Framework
Vertical Slicing
+JPEG
Case Study 4: CeedImage
• Use extension/category for the optional feature
(JPEG)
• Put it in its own library: CeedImage.JPEGExt.a
• App link against that lib if it needs the feature.
Case Study 5: CeedGL
• Obj-C Wrapper for OpenGL
• Optional features: handling of Core Video buffers
• We don’t want app have Core Video dependency if
they don’t need it
• We use a separate library: CeedGL.CoreVideoExt.a
• It’s open source -> have a look!
DEMO: Hydra
Tips & Tricks
Misc.
• Cocoapods
Frameworks or static libs?
• Frameworks are available on iOS 8+ (any OS X)
• We use frameworks to share code between app
and extensions, static libs otherwise.
• Libraries can have their resource bundle
• Frameworks can be built from libraries
Frameworks or static libs?
Libs.a
PrizmoKit
(Framework)
Prizmo
PrizmoKit
(Framework)
Cleanup Extension
PrizmoKit (Framework)
Lib A.a Lib B.a Lib C.a
Tips & Tricks
• OK, but libs that depend on libs are a mess in
Xcode.
• How do you solve that? Use scheme to force lib
build order? No, cumbersome.
• There’s a better way.
Orthogonality: A Strategy for Reusable Code
Workspace
Project A
Lib A
Project B
Lib B (depends on Lib A)
Project C
App (depends on Lib A, Lib B)
Tips & Tricks
Workspace
Project A
Lib A
Project B
Lib B (depends on Lib A)
Project C
App (depends on Lib A, Lib B)
Tips & Tricks
Workspace
Project A
Lib A
Project B
Lib B (depends on Lib A)
Project C
App (depends on Lib A, Lib B)
Stub A
• Using (empty) stub libraries, clean schemes!
implicit dependency explicit dependency
Orthogonality: A Strategy for Reusable Code
THANK YOU

More Related Content

What's hot (11)

PDF
RubyConf China 2015 - Rails off assets pipeline
Florian Dutey
 
PDF
Short and fast introduction to Scala
Sergi González Pérez
 
PPTX
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
Gerke Max Preussner
 
PDF
Ruby and Rails short motivation
jistr
 
PPTX
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
Gerke Max Preussner
 
PPTX
Polyglot
Rory Preddy
 
PDF
Software Engineering Thailand: Programming with Scala
Brian Topping
 
PPTX
Melbourne Cocoa Heads CoreML Presentation
Hon Weng Chong
 
PDF
Making CLI app in ruby
Huy Do
 
PDF
Metaprogramming Go
Weng Wei
 
PDF
Scala: An OO Surprise
Apostolos Syropoulos
 
RubyConf China 2015 - Rails off assets pipeline
Florian Dutey
 
Short and fast introduction to Scala
Sergi González Pérez
 
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
Gerke Max Preussner
 
Ruby and Rails short motivation
jistr
 
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
Gerke Max Preussner
 
Polyglot
Rory Preddy
 
Software Engineering Thailand: Programming with Scala
Brian Topping
 
Melbourne Cocoa Heads CoreML Presentation
Hon Weng Chong
 
Making CLI app in ruby
Huy Do
 
Metaprogramming Go
Weng Wei
 
Scala: An OO Surprise
Apostolos Syropoulos
 

Viewers also liked (9)

PDF
Advanced Imaging on iOS
rsebbe
 
PDF
iOS 7 URL Session & Motion FX APIs
rsebbe
 
PDF
Introduction of Xcode
Dhaval Kaneria
 
PDF
Introduction to xcode
Sunny Shaikh
 
PDF
iOS 101 - Xcode, Objective-C, iOS APIs
Subhransu Behera
 
PDF
Xcode, Basics and Beyond
rsebbe
 
KEY
キーボードで完結!ハイスピード Xcodeコーディング
cocopon
 
PPTX
Apple iOS
Chetan Gowda
 
Advanced Imaging on iOS
rsebbe
 
iOS 7 URL Session & Motion FX APIs
rsebbe
 
Introduction of Xcode
Dhaval Kaneria
 
Introduction to xcode
Sunny Shaikh
 
iOS 101 - Xcode, Objective-C, iOS APIs
Subhransu Behera
 
Xcode, Basics and Beyond
rsebbe
 
キーボードで完結!ハイスピード Xcodeコーディング
cocopon
 
Apple iOS
Chetan Gowda
 
Ad

Similar to Orthogonality: A Strategy for Reusable Code (20)

PPTX
iOS Development at Scale @Chegg
GalOrlanczyk
 
PDF
iOS development best practices
Michal Juhas
 
PPTX
Best practices iOS meetup - pmd
Suyash Gupta
 
KEY
OOP in JS
Eugene Lazutkin
 
PDF
Architecting iOS Project
Massimo Oliviero
 
PDF
FI MUNI 2012 - iOS Basics
Petr Dvorak
 
PDF
MFF UK - Introduction to iOS
Petr Dvorak
 
PDF
Cocoa Heads Tricity - Design Patterns
Maciej Burda
 
PPTX
Guide to Destroying Codebases The Demise of Clever Code
Gabor Varadi
 
PDF
Objective-C Is Not Java
Chris Adamson
 
PDF
Object Oriented Programming in Swift Ch0 - Encapsulation
Chihyang Li
 
PPTX
Solid OOPS
Toshish Jawale
 
PDF
[CocoaHeads Tricity] Do not reinvent the wheel
Mateusz Klimczak
 
PDF
Design Pattern Cheatsheet
Rachanee Saengkrajai
 
KEY
TxJS 2011
Brian LeRoux
 
PDF
Introduction to Swift 2
Joris Timmerman
 
PPTX
OOP, API Design and MVP
Harshith Keni
 
PDF
What Makes Objective C Dynamic?
Kyle Oba
 
PPTX
Creativity vs Best Practices
Supun Dissanayake
 
PDF
Tricks for Isolated Architecture in Ruby - Sergey Kukunin
Ruby Meditation
 
iOS Development at Scale @Chegg
GalOrlanczyk
 
iOS development best practices
Michal Juhas
 
Best practices iOS meetup - pmd
Suyash Gupta
 
OOP in JS
Eugene Lazutkin
 
Architecting iOS Project
Massimo Oliviero
 
FI MUNI 2012 - iOS Basics
Petr Dvorak
 
MFF UK - Introduction to iOS
Petr Dvorak
 
Cocoa Heads Tricity - Design Patterns
Maciej Burda
 
Guide to Destroying Codebases The Demise of Clever Code
Gabor Varadi
 
Objective-C Is Not Java
Chris Adamson
 
Object Oriented Programming in Swift Ch0 - Encapsulation
Chihyang Li
 
Solid OOPS
Toshish Jawale
 
[CocoaHeads Tricity] Do not reinvent the wheel
Mateusz Klimczak
 
Design Pattern Cheatsheet
Rachanee Saengkrajai
 
TxJS 2011
Brian LeRoux
 
Introduction to Swift 2
Joris Timmerman
 
OOP, API Design and MVP
Harshith Keni
 
What Makes Objective C Dynamic?
Kyle Oba
 
Creativity vs Best Practices
Supun Dissanayake
 
Tricks for Isolated Architecture in Ruby - Sergey Kukunin
Ruby Meditation
 
Ad

Recently uploaded (20)

PDF
Dealing with JSON in the relational world
Andres Almiray
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
GridView,Recycler view, API, SQLITE& NetworkRequest.pdf
Nabin Dhakal
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
WYSIWYG Web Builder Crack 2025 – Free Download Full Version with License Key
HyperPc soft
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
PPT
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
PDF
Transform Retail with Smart Technology: Power Your Growth with Ginesys
Ginesys
 
PPTX
PowerISO Crack 2025 – Free Download Full Version with Serial Key [Latest](1)....
HyperPc soft
 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
PDF
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
 
Dealing with JSON in the relational world
Andres Almiray
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
GridView,Recycler view, API, SQLITE& NetworkRequest.pdf
Nabin Dhakal
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Human Resources Information System (HRIS)
Amity University, Patna
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
WYSIWYG Web Builder Crack 2025 – Free Download Full Version with License Key
HyperPc soft
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
Transform Retail with Smart Technology: Power Your Growth with Ginesys
Ginesys
 
PowerISO Crack 2025 – Free Download Full Version with Serial Key [Latest](1)....
HyperPc soft
 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
 

Orthogonality: A Strategy for Reusable Code

  • 1. ORTHOGONALITY A strategy for reusable code @rsebbe
  • 2. Orthogonality • Why this talk? • I saw some frameworks with unmanageable complexity. “We do complex stuff, so complexity is OK” • Most of the time, it’s not! • Hoping to share some techniques that will help.
  • 3. Orthogonality • What is it? • Make <features> that don’t(*) depend on each other. • (*) minimally depend • Where <feature> is: • Code - Method - Class - Library - Project - App - etc. • Self-discipline
  • 4. Orthogonality • Why? • Easier maintenance. Changes to some part of the code shouldn’t affect some other part. • Easier reading. Because less (told or untold) dependencies. • Easier reuse. • Future-proof, adapts more easily.
  • 5. Orthogonality • How? • Some prefer to “think before”. Does not really work for me. (could work for you) • I start from real-world needs & use-cases, not trying to predict them *all* beforehand. • Then I continuously challenge the design, and try to improve it with new real-world usage, until it settles/converges.
  • 7. App Library Class Method • One purpose (avoid Swiss knives) • Expose what’s strictly required • Less state (fewer properties) & immutability • One purpose • Clear naming • Option dictionaries instead of signature variants • One purpose, keep them thematic • Don’t expose internals • Limit external dependencies (vertical slicing) • One purpose • Factor them out, make reusable libs (horizontal slicing) Code • One purpose code blocks, think of those as a “flow”, avoid local hacks • Use local variables Orthogonality
  • 8. Techniques • Think Lego® Bricks (standard, reusable blocks), avoid custom solutions. • Keep as much internal things as possible. (Swift) • Make abstractions, handle complexity internally (~ class clusters) • Take a step back when designing APIs • Use of static libs / frameworks / resource bundles • Use categories • Minimize state, favor the immutable.
  • 9. API • A meeting point. • Insulates inside from outside. • Keep complexity inside, there’s really no point in exposing it. • An API separates a feature from its implementation choices.
  • 10. Anatomy of a Method • Method signature, required args, optional ones, return values, completion handlers. • Be aware of the flow & dependencies • No internal returns. Single entry, single exit.
  • 11. Global Variables • Constants that absolutely need to be known outside: OK • Avoid otherwise, they break orthogonality
  • 12. Local variables… … insulate your code from the outside (think threads) • Interactions with self have a meaning. Don’t abuse those. Consider them as input / output for your code. • Code with lots of reference to self or ivar is suspect.
  • 13. Less State • Each property has to be maintained over the lifetime of the object. • It’s like planting a tree. It’s cool & nice, until you’re mowing the lawn around it. • Very often, it’s easy and convenient to use a property. But you’ll pay for it over time. • Avoid properties if you can. Even private / internal ones. Compute values from other state or from inputs instead. [Exception: caching]
  • 14. Immutability • Has been there from day one in Cocoa. • Once you get an object, it won’t change behind your back. It stops all form of change propagation. • Very interesting in a parallel world (GCD). • Makes code robust. • Trendy: Swift’s let & struct vs. class, reactive/ functional programming, etc.
  • 15. Property Check List • Do you need a property? Why? • Internal or public? • Read-only or read-write? • Public read-only & internal read-write? • Read-only & mutable or read-write & immutable? • Public & read-only or full read-write? read-only + set through specific method?
  • 16. Method Check List • Private or public? Internal? • Inputs as arguments or options dictionary? • Flow: do you really need those internal return’s? • Are you insulating against self ? • Isn’t it too long, woud factoring it out make more sense?
  • 17. Class/API Check List • Do you need that class? • Internal or public? Class cluster? • Do you need each of those methods, public/ private? • Do you need to include that additional framework in the .h? Isn’t a .m inclusion enough? • Do you compartmentalize deps with categories?
  • 18. Fruit Ninja Large App Framework Framework Framework Framework App Framework Horizontal Slicing
  • 19. Large Framework, w/ all kinds of dependencies Vertical Slicing Framework Ext A Ext B
  • 20. Keep cool, man! • Good framework, method, API design is clear for everyone. There’s not that much room for ego or personal preferences. • Junior could come up with a better design. That’s OK, you should welcome it & even encourage it! Be thankful for learning new stuff. • Search for design examples. It helps. For API design, Apple *typically* makes effort about this. (OK, they screw up sometimes) • When you reach good design, you typically feel you’ve achieved something. It fits nicely together, hacks are not needed, it is easily reusable… But don’t stop there! • Simplicity of code often converges to the same design, even from people with different backgrounds.
  • 23. Case Study 1: CeedOCR • Reusable OCR library • Possibly different OCR engines • Mac / iOS DEMO: Prizmo
  • 24. Case Study 1: CeedOCR • Simple API: 1 class, 1 method. OCR engines are strings. • Similar to class-cluster design (variety of internal classes) • No internal API is exposed (engine types, classes, etc.) • Dynamically looked up (not linked -> Class is Nil) • Extensible through delegation (binarization)
  • 25. Case Study 2: CeedAnalytics • Reusable Analytics library. Goal: track which app features are used, which are not. • Possibly different backends (Google, Countly…) • Similar solution as CeedOCR DEMO: Prizmo
  • 26. Case Study 3: CeedVision • Has a class to perform Accelerate-based image processing • Variety of image source options (CoreVideo, CoreImage, OpenCV, CoreGraphics, vImage…) DEMO: Prizmo
  • 27. Case Study 3: CeedVision • Core features part of the class • Input options as categories (Swift: extensions)
  • 28. Case Study 4: CeedImage • GPU Image Processing lib, similar to Core Image (tiled rendering) but with more control (color vs. gray, memory cache…). • Optional features (external JPEG lib) • We don’t want app have JPEG part if app don’t need it (external lib dep, maintenance…) DEMO: Hydra
  • 30. Case Study 4: CeedImage • Use extension/category for the optional feature (JPEG) • Put it in its own library: CeedImage.JPEGExt.a • App link against that lib if it needs the feature.
  • 31. Case Study 5: CeedGL • Obj-C Wrapper for OpenGL • Optional features: handling of Core Video buffers • We don’t want app have Core Video dependency if they don’t need it • We use a separate library: CeedGL.CoreVideoExt.a • It’s open source -> have a look! DEMO: Hydra
  • 34. Frameworks or static libs? • Frameworks are available on iOS 8+ (any OS X) • We use frameworks to share code between app and extensions, static libs otherwise. • Libraries can have their resource bundle • Frameworks can be built from libraries
  • 35. Frameworks or static libs? Libs.a PrizmoKit (Framework) Prizmo PrizmoKit (Framework) Cleanup Extension PrizmoKit (Framework) Lib A.a Lib B.a Lib C.a
  • 36. Tips & Tricks • OK, but libs that depend on libs are a mess in Xcode. • How do you solve that? Use scheme to force lib build order? No, cumbersome. • There’s a better way.
  • 38. Workspace Project A Lib A Project B Lib B (depends on Lib A) Project C App (depends on Lib A, Lib B) Tips & Tricks
  • 39. Workspace Project A Lib A Project B Lib B (depends on Lib A) Project C App (depends on Lib A, Lib B) Tips & Tricks
  • 40. Workspace Project A Lib A Project B Lib B (depends on Lib A) Project C App (depends on Lib A, Lib B) Stub A • Using (empty) stub libraries, clean schemes! implicit dependency explicit dependency