SlideShare a Scribd company logo
1 of 42
Download to read offline
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.
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
THANK YOU

More Related Content

What's hot

RubyConf China 2015 - Rails off assets pipeline
RubyConf China 2015 - Rails off assets pipelineRubyConf China 2015 - Rails off assets pipeline
RubyConf China 2015 - Rails off assets pipelineFlorian Dutey
 
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersEast Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersGerke Max Preussner
 
Ruby and Rails short motivation
Ruby and Rails short motivationRuby and Rails short motivation
Ruby and Rails short motivationjistr
 
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)Gerke Max Preussner
 
Software Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaSoftware Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaBrian Topping
 
Melbourne Cocoa Heads CoreML Presentation
Melbourne Cocoa Heads CoreML PresentationMelbourne Cocoa Heads CoreML Presentation
Melbourne Cocoa Heads CoreML PresentationHon Weng Chong
 
Making CLI app in ruby
Making CLI app in rubyMaking CLI app in ruby
Making CLI app in rubyHuy Do
 
Metaprogramming Go
Metaprogramming GoMetaprogramming Go
Metaprogramming GoWeng Wei
 

What's hot (11)

RubyConf China 2015 - Rails off assets pipeline
RubyConf China 2015 - Rails off assets pipelineRubyConf China 2015 - Rails off assets pipeline
RubyConf China 2015 - Rails off assets pipeline
 
Short and fast introduction to Scala
Short and fast introduction to ScalaShort and fast introduction to Scala
Short and fast introduction to Scala
 
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersEast Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
 
Ruby and Rails short motivation
Ruby and Rails short motivationRuby and Rails short motivation
Ruby and Rails short motivation
 
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
 
Polyglot
PolyglotPolyglot
Polyglot
 
Software Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaSoftware Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with Scala
 
Melbourne Cocoa Heads CoreML Presentation
Melbourne Cocoa Heads CoreML PresentationMelbourne Cocoa Heads CoreML Presentation
Melbourne Cocoa Heads CoreML Presentation
 
Making CLI app in ruby
Making CLI app in rubyMaking CLI app in ruby
Making CLI app in ruby
 
Metaprogramming Go
Metaprogramming GoMetaprogramming Go
Metaprogramming Go
 
Scala: An OO Surprise
Scala: An OO SurpriseScala: An OO Surprise
Scala: An OO Surprise
 

Viewers also liked

Advanced Imaging on iOS
Advanced Imaging on iOSAdvanced Imaging on iOS
Advanced Imaging on iOSrsebbe
 
iOS 7 URL Session & Motion FX APIs
iOS 7 URL Session & Motion FX APIsiOS 7 URL Session & Motion FX APIs
iOS 7 URL Session & Motion FX APIsrsebbe
 
Introduction to xcode
Introduction to xcodeIntroduction to xcode
Introduction to xcodeSunny Shaikh
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsSubhransu Behera
 
Xcode, Basics and Beyond
Xcode, Basics and BeyondXcode, Basics and Beyond
Xcode, Basics and Beyondrsebbe
 
キーボードで完結!ハイスピード Xcodeコーディング
キーボードで完結!ハイスピード Xcodeコーディングキーボードで完結!ハイスピード Xcodeコーディング
キーボードで完結!ハイスピード Xcodeコーディングcocopon
 

Viewers also liked (9)

Advanced Imaging on iOS
Advanced Imaging on iOSAdvanced Imaging on iOS
Advanced Imaging on iOS
 
iOS 7 URL Session & Motion FX APIs
iOS 7 URL Session & Motion FX APIsiOS 7 URL Session & Motion FX APIs
iOS 7 URL Session & Motion FX APIs
 
Introduction of Xcode
Introduction of XcodeIntroduction of Xcode
Introduction of Xcode
 
Introduction to xcode
Introduction to xcodeIntroduction to xcode
Introduction to xcode
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIs
 
Xcode, Basics and Beyond
Xcode, Basics and BeyondXcode, Basics and Beyond
Xcode, Basics and Beyond
 
キーボードで完結!ハイスピード Xcodeコーディング
キーボードで完結!ハイスピード Xcodeコーディングキーボードで完結!ハイスピード Xcodeコーディング
キーボードで完結!ハイスピード Xcodeコーディング
 
IELTS ORIENTATION
IELTS ORIENTATIONIELTS ORIENTATION
IELTS ORIENTATION
 
Apple iOS
Apple iOSApple iOS
Apple iOS
 

Similar to Orthogonality: A Strategy for Reusable Code

Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®Hannes Lowette
 
Devconf 2011 - PHP - How Yii framework is developed
Devconf 2011 - PHP - How Yii framework is developedDevconf 2011 - PHP - How Yii framework is developed
Devconf 2011 - PHP - How Yii framework is developedAlexander Makarov
 
Code reviews
Code reviewsCode reviews
Code reviewsRoger Xia
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentationBhavin Gandhi
 
Introduction to Aspect Oriented Programming by Donald Belcham
Introduction to Aspect Oriented Programming by Donald BelchamIntroduction to Aspect Oriented Programming by Donald Belcham
Introduction to Aspect Oriented Programming by Donald Belcham.NET Conf UY
 
Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®Hannes Lowette
 
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Molieremfrancis
 
Segue to design patterns
Segue to design patternsSegue to design patterns
Segue to design patternsRahul Singh
 
Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...SQALab
 
Don't get blamed for your choices - Techorama 2019
Don't get blamed for your choices - Techorama 2019Don't get blamed for your choices - Techorama 2019
Don't get blamed for your choices - Techorama 2019Hannes Lowette
 
Coding Standard And Code Review
Coding Standard And Code ReviewCoding Standard And Code Review
Coding Standard And Code ReviewMilan Vukoje
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesDr. Syed Hassan Amin
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Andrei KUCHARAVY
 

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

Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
 
Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®
 
Eurosport's Kodakademi #2
Eurosport's Kodakademi #2Eurosport's Kodakademi #2
Eurosport's Kodakademi #2
 
Devconf 2011 - PHP - How Yii framework is developed
Devconf 2011 - PHP - How Yii framework is developedDevconf 2011 - PHP - How Yii framework is developed
Devconf 2011 - PHP - How Yii framework is developed
 
Code reviews
Code reviewsCode reviews
Code reviews
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Introduction to Aspect Oriented Programming by Donald Belcham
Introduction to Aspect Oriented Programming by Donald BelchamIntroduction to Aspect Oriented Programming by Donald Belcham
Introduction to Aspect Oriented Programming by Donald Belcham
 
Introduction To AOP
Introduction To AOPIntroduction To AOP
Introduction To AOP
 
The Architect Way
The Architect WayThe Architect Way
The Architect Way
 
Intro To AOP
Intro To AOPIntro To AOP
Intro To AOP
 
Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®Build software like a bag of marbles, not a castle of LEGO®
Build software like a bag of marbles, not a castle of LEGO®
 
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
10 clues showing that you are doing OSGi in the wrong manner - Jerome Moliere
 
Segue to design patterns
Segue to design patternsSegue to design patterns
Segue to design patterns
 
Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...
 
Don't get blamed for your choices - Techorama 2019
Don't get blamed for your choices - Techorama 2019Don't get blamed for your choices - Techorama 2019
Don't get blamed for your choices - Techorama 2019
 
Coding Standard And Code Review
Coding Standard And Code ReviewCoding Standard And Code Review
Coding Standard And Code Review
 
2CPP19 - Summation
2CPP19 - Summation2CPP19 - Summation
2CPP19 - Summation
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design Principles
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1
 

Recently uploaded

A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineeringssuserb3a23b
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 

Recently uploaded (20)

A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineering
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Odoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting ServiceOdoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting Service
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 

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.
  • 37.
  • 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
  • 41.