45
Object Calisthenics by Taki Bacalso

Object Calisthenics in Objective-C

Embed Size (px)

DESCRIPTION

OOP is obviously nothing new and has been around for a long time. However, even if we learn and understand these concepts, do you really think we're implementing these properly in code?

Citation preview

Page 1: Object Calisthenics in Objective-C

Object Calisthenics

by Taki Bacalso

Page 2: Object Calisthenics in Objective-C

Before we start...

We are all quite aware what core concepts make up good OO code…

However, are we really implementing these concepts properly in code?

cohesion, loose coupling, no redundancy, encapsulation, testability, etc.

Page 3: Object Calisthenics in Objective-C

Coding is like Drawing...

Expectation Reality

Drawing process...

Page 4: Object Calisthenics in Objective-C

Not exactly what you had in mind...

Source: http://www.funnyjunk.com/funny_pictures/4574195/Drawing+expectations+vs+reality

Page 5: Object Calisthenics in Objective-C

“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

Page 6: Object Calisthenics in Objective-C

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.

Page 7: Object Calisthenics in Objective-C

The Challenge...

Far stricter coding standards than you’ve ever used in your life.

Not universally applicable to all programming languages.

so pick the ones that work for you

9Leggo!

Page 8: Object Calisthenics in Objective-C

1st Rule of thumb

Use only one level of indentation

per method

Page 9: Object Calisthenics in Objective-C
Page 10: Object Calisthenics in Objective-C

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

Page 11: Object Calisthenics in Objective-C

√ Do one thing per method√ Reusable code

Page 12: Object Calisthenics in Objective-C

Benefits

● Level of reuse will start to rise exponentially

● Easier to debug these ‘code snippets’

Page 13: Object Calisthenics in Objective-C

2nd Rule of thumb

Don’t Use the else keyword

Page 14: Object Calisthenics in Objective-C
Page 15: Object Calisthenics in Objective-C

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

Page 16: Object Calisthenics in Objective-C

How to Fix...

● return Early

● Use Polymorphism

● See Refactoring Methods:○ Replace Type Code with State/Strategy○ Replace Conditional with Polymorphism○ Introduce Null Object

Page 17: Object Calisthenics in Objective-C
Page 18: Object Calisthenics in Objective-C
Page 19: Object Calisthenics in Objective-C

3rd Rule of thumb

Wrap all primitives and strings

Page 20: Object Calisthenics in Objective-C
Page 21: Object Calisthenics in Objective-C

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

Page 22: Object Calisthenics in Objective-C
Page 23: Object Calisthenics in Objective-C

Benefits...

● More maintainable code

● Type Hinting○ benefits both compiler and programmer

● Context of value is more obvious

● Avoids Primitive Obsession Code Smell

Page 24: Object Calisthenics in Objective-C

4th Rule of thumb

Use only one dot per line

Page 25: Object Calisthenics in Objective-C
Page 26: Object Calisthenics in Objective-C

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”

Page 27: Object Calisthenics in Objective-C

5th Rule of thumb

Don’t Abbreviate

Page 28: Object Calisthenics in Objective-C

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] ✔

Page 29: Object Calisthenics in Objective-C

6th Rule of thumb

Keep all entities small

Page 30: Object Calisthenics in Objective-C

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

Page 31: Object Calisthenics in Objective-C

7th Rule of thumb

Don’t use any classes with more

than two instance variables

Page 32: Object Calisthenics in Objective-C
Page 33: Object Calisthenics in Objective-C

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!

Page 34: Object Calisthenics in Objective-C
Page 35: Object Calisthenics in Objective-C

8th Rule of thumb

Use First-Class Collections

Page 36: Object Calisthenics in Objective-C

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.

Page 37: Object Calisthenics in Objective-C

9th Rule of thumb

Don’t use any getters/setters/properties

Page 38: Object Calisthenics in Objective-C

Basically...

● Stated as, “Tell, don’t ask”

● Don’t expose private variables

● Please do use these to handle retains/releases

Page 39: Object Calisthenics in Objective-C

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

Page 40: Object Calisthenics in Objective-C

The Real Challenge

Page 41: Object Calisthenics in Objective-C

Try It!

Write a 1000 line program that follow 100% of these rules

Page 42: Object Calisthenics in Objective-C

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

Page 43: Object Calisthenics in Objective-C

Overall

“ The goal is code that concisely expresses simple and elegant abstractions for the incidental complexity we deal with all day long.”

- Jeff Bay

Page 44: Object Calisthenics in Objective-C

Thank you for listening! :D

Try It and see what

happens :)