SlideShare a Scribd company logo
Object Calisthenics 
by Taki Bacalso
Before we start... 
We are all quite aware what core concepts make up good 
OO code… 
cohesion, loose coupling, no redundancy, encapsulation, 
testability, etc. 
However, are we really implementing these concepts 
properly in code?
Coding is like Drawing... 
Drawing process... 
Expectation Reality
Not exactly what you had in mind... 
Source: http://www.funnyjunk.com/funny_pictures/4574195/Drawing+expectations+vs+reality
“It’s one thing to understand that encapsulation 
means hiding data, implementation, type, design, or 
construction. It’s another thing altogether to design 
code that implements encapsulation well.” 
- Jeff Bay
Object Calisthenics 
“physical exercises that are done without special equipment” 
source: Merriam-Webster 
coined by Jeff Bay in : 
“The Thoughtworks Anthology” 
Is a group of guidelines that promotes 
good object-oriented design.
The Challenge... 
so pick the ones that work for you 9 
Far stricter coding standards than 
you’ve ever used in your life. 
Not universally applicable to all 
programming languages. 
Leggo!
Use only one level of indentation 
per method 
1st Rule of thumb
Basically... 
● Do one thing per method 
○ one control structure 
○ one block of statements 
● Avoid Nested Control Structures 
○ it means you’re doing more than one thing
√ Do one thing per method 
√ Reusable code
Benefits 
● Level of reuse will start to rise 
exponentially 
● Easier to debug these ‘code snippets’
Don’t Use the else keyword 
2nd Rule of thumb
Basically... 
● Nested conditionals and long case statements are 
hard to follow 
● Easy to add branch in existing conditional 
○ ...rather than refactoring to a better solution 
● Frequent source of Code Duplication
How to Fix... 
● return Early 
● Use Polymorphism 
● See Refactoring Methods: 
○ Replace Type Code with State/Strategy 
○ Replace Conditional with Polymorphism 
○ Introduce Null Object
Wrap all primitives and strings 
3rd Rule of thumb
Basically... 
● Bad: Method names do all the work of expressing the 
intent 
● Primitive variables 
○ Don’t help write semantically correct programs 
● Apply if objects need behavior or for a group of 
primitives
Benefits... 
● More maintainable code 
● Type Hinting 
○ benefits both compiler and programmer 
● Context of value is more obvious 
● Avoids Primitive Obsession Code Smell
Use only one dot per line 
4th Rule of thumb
Basically... 
● Multiple dots (Nested Calls) 
○ indicate misplaced responsibilities 
○ violate encapsulation 
● Ask an object to do something for you 
○ Not poke around its insides! 
● Law of Demeter 
○ “Talk only to your friends”
Don’t Abbreviate 
5th Rule of thumb
Basically... 
● Confusing - tend to hide larger problems 
● Keep names to one-two words 
○ class names 
○ method names 
○ entities 
● Avoid names that duplicate context 
○ bad: [order shipOrder] ✘ 
○ good: [order ship] ✔
Keep all entities small 
6th Rule of thumb
Basically... 
● No class more than 50 lines 
● No package more than ten files 
● Why 50 lines? 
○ Visible on screen without scrolling 
○ easier to grasp quickly
Don’t use any classes with more 
than two instance variables 
7th Rule of thumb
Basically... 
● Forms 2 Kinds of classes, those that: 
○ maintain the state of a single instance variable 
○ coordinate two separate variables 
● Don’t mix these 2 responsibilities!
Use First-Class Collections 
8th Rule of thumb
Basically... 
● Any class that contains a collection should contain no 
other member variables 
● Class will contain related behaviors (i.e filters) 
● Use when collection has behavior 
● Implemented by NSArrays, NSSets, etc.
Don’t use any 
getters/setters/properties 
9th Rule of thumb
Basically... 
● Stated as, “Tell, don’t ask” 
● Don’t expose private variables 
● Please do use these to handle 
retains/releases
In Conclusion, they aim 
● to visualize and implement the encapsulation of data 
● to use polymorphism appropriately 
● to encourage concise and straightforward naming 
standards 
● to craft code that has no duplication in code or idea
The Real Challenge
Try It! 
Write a 1000 line program that follow 100% 
of these rules
You will find yourself 
● Breaking old habits 
● Changing rules that you may have lived with for 
your whole programming life 
● Creating something completely different from 
what you expected
Overall 
“ The goal is code that concisely expresses simple 
and elegant abstractions for the incidental 
complexity we deal with all day long.” 
- Jeff Bay
Try It and see what 
happens :) 
Thank you for listening! :D
References 
“ThoughtWorks Anthology” by ThoughtWorks 
“Object Calisthenics Applied to PHP” by Guilherme Blanco 
“Your code sucks! let’s fix it” by Rafael Dohms 
“Object Calisthenics: write better object-oriented code” by Fran Diéguez 
http://www.bennadel.com/resources/uploads/2012/ObjectCalisthenics.pdf

More Related Content

What's hot

Tdd in practice
Tdd in practiceTdd in practice
Tdd in practice
Andrew Meredith
 
CPP14 - Encapsulation
CPP14 - EncapsulationCPP14 - Encapsulation
CPP14 - Encapsulation
Michael Heron
 
Mca2030 object oriented programming – c++
Mca2030  object oriented programming – c++Mca2030  object oriented programming – c++
Mca2030 object oriented programming – c++
smumbahelp
 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - Encapsulation
Michael Heron
 
Think Different: Objective-C for the .NET developer
Think Different: Objective-C for the .NET developerThink Different: Objective-C for the .NET developer
Think Different: Objective-C for the .NET developer
Shawn Price
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation java
JayasankarPR2
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
FALLEE31188
 
Encapsulation
EncapsulationEncapsulation

What's hot (8)

Tdd in practice
Tdd in practiceTdd in practice
Tdd in practice
 
CPP14 - Encapsulation
CPP14 - EncapsulationCPP14 - Encapsulation
CPP14 - Encapsulation
 
Mca2030 object oriented programming – c++
Mca2030  object oriented programming – c++Mca2030  object oriented programming – c++
Mca2030 object oriented programming – c++
 
2CPP09 - Encapsulation
2CPP09 - Encapsulation2CPP09 - Encapsulation
2CPP09 - Encapsulation
 
Think Different: Objective-C for the .NET developer
Think Different: Objective-C for the .NET developerThink Different: Objective-C for the .NET developer
Think Different: Objective-C for the .NET developer
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation java
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 

Similar to Object Calisthenics in Objective-C

Clean Code
Clean CodeClean Code
Clean Code
NeeleEilers
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patterns
Steven Smith
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
Eyob Lube
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
yannick grenzinger
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
Alfred Jett Grandeza
 
30% faster coder on-boarding when you have a code cookbook
30% faster coder on-boarding when you have a code cookbook30% faster coder on-boarding when you have a code cookbook
30% faster coder on-boarding when you have a code cookbook
Gabriel Paunescu 🤖
 
The View object orientated programming in Lotuscript
The View object orientated programming in LotuscriptThe View object orientated programming in Lotuscript
The View object orientated programming in Lotuscript
Bill Buchan
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
Knoldus Inc.
 
Clean_Code
Clean_CodeClean_Code
Clean_Code
Mohamed Essam
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
Advanced java script essentials v1
Advanced java script essentials v1Advanced java script essentials v1
Advanced java script essentials v1
ASHUTOSHPATKAR1
 
Basic software engineering principles - Session 1
Basic software engineering principles - Session 1Basic software engineering principles - Session 1
Basic software engineering principles - Session 1
LahiruWijewardana1
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
Metin Ogurlu
 
How object oriented are you feeling today?
How object oriented are you feeling today?How object oriented are you feeling today?
How object oriented are you feeling today?
Krzysztof Jelski
 
Clean Code
Clean CodeClean Code
Clean Code
Chris Farrell
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Code
eddiehaber
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
BADR
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
Ny Fanilo Andrianjafy, B.Eng.
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
Bhavin Gandhi
 

Similar to Object Calisthenics in Objective-C (20)

Clean Code
Clean CodeClean Code
Clean Code
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patterns
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
 
30% faster coder on-boarding when you have a code cookbook
30% faster coder on-boarding when you have a code cookbook30% faster coder on-boarding when you have a code cookbook
30% faster coder on-boarding when you have a code cookbook
 
The View object orientated programming in Lotuscript
The View object orientated programming in LotuscriptThe View object orientated programming in Lotuscript
The View object orientated programming in Lotuscript
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
 
Clean_Code
Clean_CodeClean_Code
Clean_Code
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
 
Advanced java script essentials v1
Advanced java script essentials v1Advanced java script essentials v1
Advanced java script essentials v1
 
Basic software engineering principles - Session 1
Basic software engineering principles - Session 1Basic software engineering principles - Session 1
Basic software engineering principles - Session 1
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
 
How object oriented are you feeling today?
How object oriented are you feeling today?How object oriented are you feeling today?
How object oriented are you feeling today?
 
Clean Code
Clean CodeClean Code
Clean Code
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Code
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
 

More from KLabCyscorpions-TechBlog

Auto Layout on Xcode 5
Auto Layout on Xcode 5Auto Layout on Xcode 5
Auto Layout on Xcode 5
KLabCyscorpions-TechBlog
 
Code Review for iOS
Code Review for iOSCode Review for iOS
Code Review for iOS
KLabCyscorpions-TechBlog
 
Object Calisthenics
Object CalisthenicsObject Calisthenics
Object Calisthenics
KLabCyscorpions-TechBlog
 
Why You're A Bad PHP Programmer
Why You're A Bad PHP ProgrammerWhy You're A Bad PHP Programmer
Why You're A Bad PHP Programmer
KLabCyscorpions-TechBlog
 
Redis Set Go
Redis Set GoRedis Set Go
Redis Beyond
Redis BeyondRedis Beyond
X-Debug in Php Storm
X-Debug in Php StormX-Debug in Php Storm
X-Debug in Php Storm
KLabCyscorpions-TechBlog
 
Software Testing
Software TestingSoftware Testing
Software Testing
KLabCyscorpions-TechBlog
 
Php + MySql Optimization
Php + MySql OptimizationPhp + MySql Optimization
Php + MySql Optimization
KLabCyscorpions-TechBlog
 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
KLabCyscorpions-TechBlog
 
MVC Web Application
MVC Web ApplicationMVC Web Application
MVC Web Application
KLabCyscorpions-TechBlog
 
AfNetworking vs. Native + Caching
AfNetworking vs. Native + CachingAfNetworking vs. Native + Caching
AfNetworking vs. Native + Caching
KLabCyscorpions-TechBlog
 
Bash
BashBash

More from KLabCyscorpions-TechBlog (13)

Auto Layout on Xcode 5
Auto Layout on Xcode 5Auto Layout on Xcode 5
Auto Layout on Xcode 5
 
Code Review for iOS
Code Review for iOSCode Review for iOS
Code Review for iOS
 
Object Calisthenics
Object CalisthenicsObject Calisthenics
Object Calisthenics
 
Why You're A Bad PHP Programmer
Why You're A Bad PHP ProgrammerWhy You're A Bad PHP Programmer
Why You're A Bad PHP Programmer
 
Redis Set Go
Redis Set GoRedis Set Go
Redis Set Go
 
Redis Beyond
Redis BeyondRedis Beyond
Redis Beyond
 
X-Debug in Php Storm
X-Debug in Php StormX-Debug in Php Storm
X-Debug in Php Storm
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Php + MySql Optimization
Php + MySql OptimizationPhp + MySql Optimization
Php + MySql Optimization
 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
 
MVC Web Application
MVC Web ApplicationMVC Web Application
MVC Web Application
 
AfNetworking vs. Native + Caching
AfNetworking vs. Native + CachingAfNetworking vs. Native + Caching
AfNetworking vs. Native + Caching
 
Bash
BashBash
Bash
 

Recently uploaded

skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
blueshagoo1
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
Prof. Dr. K. Adisesha
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
Celine George
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 

Recently uploaded (20)

skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 

Object Calisthenics in Objective-C

  • 1. Object Calisthenics by Taki Bacalso
  • 2. Before we start... We are all quite aware what core concepts make up good OO code… cohesion, loose coupling, no redundancy, encapsulation, testability, etc. However, are we really implementing these concepts properly in code?
  • 3. Coding is like Drawing... Drawing process... Expectation Reality
  • 4. Not exactly what you had in mind... Source: http://www.funnyjunk.com/funny_pictures/4574195/Drawing+expectations+vs+reality
  • 5. “It’s one thing to understand that encapsulation means hiding data, implementation, type, design, or construction. It’s another thing altogether to design code that implements encapsulation well.” - Jeff Bay
  • 6. Object Calisthenics “physical exercises that are done without special equipment” source: Merriam-Webster coined by Jeff Bay in : “The Thoughtworks Anthology” Is a group of guidelines that promotes good object-oriented design.
  • 7. The Challenge... so pick the ones that work for you 9 Far stricter coding standards than you’ve ever used in your life. Not universally applicable to all programming languages. Leggo!
  • 8. Use only one level of indentation per method 1st Rule of thumb
  • 9.
  • 10. Basically... ● Do one thing per method ○ one control structure ○ one block of statements ● Avoid Nested Control Structures ○ it means you’re doing more than one thing
  • 11. √ Do one thing per method √ Reusable code
  • 12. Benefits ● Level of reuse will start to rise exponentially ● Easier to debug these ‘code snippets’
  • 13. Don’t Use the else keyword 2nd Rule of thumb
  • 14.
  • 15. Basically... ● Nested conditionals and long case statements are hard to follow ● Easy to add branch in existing conditional ○ ...rather than refactoring to a better solution ● Frequent source of Code Duplication
  • 16. How to Fix... ● return Early ● Use Polymorphism ● See Refactoring Methods: ○ Replace Type Code with State/Strategy ○ Replace Conditional with Polymorphism ○ Introduce Null Object
  • 17.
  • 18.
  • 19. Wrap all primitives and strings 3rd Rule of thumb
  • 20.
  • 21. Basically... ● Bad: Method names do all the work of expressing the intent ● Primitive variables ○ Don’t help write semantically correct programs ● Apply if objects need behavior or for a group of primitives
  • 22.
  • 23. Benefits... ● More maintainable code ● Type Hinting ○ benefits both compiler and programmer ● Context of value is more obvious ● Avoids Primitive Obsession Code Smell
  • 24. Use only one dot per line 4th Rule of thumb
  • 25.
  • 26. Basically... ● Multiple dots (Nested Calls) ○ indicate misplaced responsibilities ○ violate encapsulation ● Ask an object to do something for you ○ Not poke around its insides! ● Law of Demeter ○ “Talk only to your friends”
  • 27. Don’t Abbreviate 5th Rule of thumb
  • 28. Basically... ● Confusing - tend to hide larger problems ● Keep names to one-two words ○ class names ○ method names ○ entities ● Avoid names that duplicate context ○ bad: [order shipOrder] ✘ ○ good: [order ship] ✔
  • 29. Keep all entities small 6th Rule of thumb
  • 30. Basically... ● No class more than 50 lines ● No package more than ten files ● Why 50 lines? ○ Visible on screen without scrolling ○ easier to grasp quickly
  • 31. Don’t use any classes with more than two instance variables 7th Rule of thumb
  • 32.
  • 33. Basically... ● Forms 2 Kinds of classes, those that: ○ maintain the state of a single instance variable ○ coordinate two separate variables ● Don’t mix these 2 responsibilities!
  • 34.
  • 35. Use First-Class Collections 8th Rule of thumb
  • 36. Basically... ● Any class that contains a collection should contain no other member variables ● Class will contain related behaviors (i.e filters) ● Use when collection has behavior ● Implemented by NSArrays, NSSets, etc.
  • 37. Don’t use any getters/setters/properties 9th Rule of thumb
  • 38. Basically... ● Stated as, “Tell, don’t ask” ● Don’t expose private variables ● Please do use these to handle retains/releases
  • 39. In Conclusion, they aim ● to visualize and implement the encapsulation of data ● to use polymorphism appropriately ● to encourage concise and straightforward naming standards ● to craft code that has no duplication in code or idea
  • 41. Try It! Write a 1000 line program that follow 100% of these rules
  • 42. You will find yourself ● Breaking old habits ● Changing rules that you may have lived with for your whole programming life ● Creating something completely different from what you expected
  • 43. Overall “ The goal is code that concisely expresses simple and elegant abstractions for the incidental complexity we deal with all day long.” - Jeff Bay
  • 44. Try It and see what happens :) Thank you for listening! :D
  • 45. References “ThoughtWorks Anthology” by ThoughtWorks “Object Calisthenics Applied to PHP” by Guilherme Blanco “Your code sucks! let’s fix it” by Rafael Dohms “Object Calisthenics: write better object-oriented code” by Fran Diéguez http://www.bennadel.com/resources/uploads/2012/ObjectCalisthenics.pdf