inheritance - the myth of code reuse | andrei raifura | codeway 2015

85
Andrei Raifura iOS Department Manager, YOPESO #CodeWăy Inheritance - The myth of code reuse

Upload: yopeso

Post on 15-Feb-2017

236 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Andrei Raifura iOS Department Manager, YOPESO

#CodeWăy

Inheritance - The myth of code reuse

Page 2: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

• How does Inheritance help us to reuse the code

• Why it doesn’t work

• The pitfalls of Inheritance

• To use or not to use

Agenda

Page 3: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

• Encapsulation

• Abstraction

• Inheritance

• Polymorphism

OOP principlesI will teach you OOP

Page 4: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Inheritance

“…It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces…”

Wikipedia, Inheritance (object-oriented programming)

Page 5: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Inheritance

‣ Two ears ‣ For legs ‣ Tail

Animal

Page 6: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

Page 7: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

CustomerYou

Page 8: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

Can you program a Toyota Corolla for me?

CustomerYou

Page 9: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

Mm..Sure!!

CustomerYou

Page 10: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Toyota Corolla

Wheels

Manufacturer

Transmission

4

Toyota

Front-wheel drive

Page 11: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Car

class Car { let frontLeft = Wheel() let frontRight = Wheel() let rearLeft = Wheel() let rearRight = Wheel()

Page 12: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Car

class Car { let frontLeft = Wheel() let frontRight = Wheel() let rearLeft = Wheel() let rearRight = Wheel() var manufacturer: String { get { return "Undefined" } }

Page 13: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Car

class Car { . . .

func turnLeft(degrees: Double) { frontLeft.turnLeft(degrees) frontRight.turnLeft(degrees) } func turnRight(degrees: Double) { frontLeft.turnRight(degrees) frontRight.turnRight(degrees) }

Page 14: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Car

class Car { . . .

func accelerate(kph: Double) { frontLeft.rotate(kph) frontRight.rotate(kph) } }

Page 15: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Wheel

class Wheel { private var angle = 0.0 private var rotationSpeed = 0.0 func turnRight(degrees: Double) { angle += degrees } func turnLeft(degrees: Double) { angle -= degrees } func rotate(kph: Double) { rotationSpeed = kph } }

Page 16: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Toyota Corolla

class ToyotaCorolla: Car { override var manufacturer: String { get { return "Toyota" } } }

Page 17: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

ToyotaCorolla

- turnLeft - turnRight - rotate

Wheel

Page 18: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

Great! Now, i’d like to have a Toyota Corolla Sport

CustomerYou

Page 19: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

Ok! will do!

CustomerYou

Page 20: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Toyota Corolla

Wheels

Manufacturer

Transmission

4

Toyota

Front-wheel drive

Page 21: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Toyota Corolla

Wheels

Manufacturer

Transmission

4

Toyota

Front-wheel drive

Page 22: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Toyota Corolla Sport

Wheels

Manufacturer

Transmission

4

Toyota

Rear-wheel drive

Page 23: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Toyota Corolla Sport

class ToyotaCorollaSport: ToyotaCorolla { override func accelerate(kph: Double) { rearLeft.rotate(kph) rearRight.rotate(kph) } }

Page 24: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

ToyotaCorolla

- turnLeft - turnRight - rotate

Wheel

Page 25: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

ToyotaCorollaSport

ToyotaCorolla

- turnLeft - turnRight - rotate

Wheel

Page 26: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

Woww! Now a Honda Civic and a Honda Civic

Sport

CustomerYou

Page 27: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

Mmnn.. ok

CustomerYou

Page 28: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Honda Civic

Wheels

Manufacturer

Transmission

4

Front-wheel drive

Honda

Page 29: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Honda Civic Sport

Wheels

Manufacturer

Transmission

4

Rear-wheel drive

Honda

Page 30: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

ToyotaCorollaSport

ToyotaCorolla

- turnLeft - turnRight - rotate

Wheel

Page 31: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

ToyotaCorollaSport

ToyotaCorolla

- turnLeft - turnRight - rotate

Wheel

HondaCivic

HondaCivicSport

Page 32: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Front-wheel Drive

class FrontWheelDriveCar: Car { override func accelerate(kph: Double) { frontLeft.rotate(kph) frontRight.rotate(kph) } }

Page 33: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Rear-wheel Drive

class RearWheelDriveCar: Car { override func accelerate(kph: Double) { rearLeft.rotate(kph) rearRight.rotate(kph) } }

Page 34: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

ToyotaCorollaSport

ToyotaCorolla

- turnLeft - turnRight - rotate

Wheel

HondaCivic

HondaCivicSport

Page 35: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

FrontWheelDriveCar

ToyotaCorollaSport

RearWheelDriveCar

ToyotaCorolla

- turnLeft - turnRight - rotate

Wheel

HondaCivic HondaCivicSport

Page 36: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Refactoring Toyota Corolla

class ToyotaCorolla: FrontWheelDriveCar { override var manufacturer: String { get { return "Toyota" } } }

Page 37: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Refactoring Toyota Corolla Sport

class ToyotaCorollaSport: RearWheelDriveCar { override var manufacturer: String { get { return "Toyota" } } }

Page 38: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Honda Civic

class HondaCivic: FrontWheelDriveCar { override var manufacturer: String { get { return "Honda" } } }

Page 39: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Honda Civic Sport

class HondaCivicSport: RearWheelDriveCar { override var manufacturer: String { get { return "Honda" } } }

Page 40: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Lexus GX

Wheels

Manufacturer

Transmission

4

All-wheel drive

Lexus

Page 41: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

FrontWheelDriveCar

ToyotaCorollaSport

RearWheelDriveCar

ToyotaCorolla

- turnLeft - turnRight - rotate

Wheel

HondaCivic HondaCivicSport

Page 42: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

FrontWheelDriveCar

ToyotaCorollaSport

RearWheelDriveCar

ToyotaCorolla

- turnLeft - turnRight - rotate

Wheel

HondaCivic HondaCivicSport

AllWheelDriveCar

LexusGX

Page 43: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining All-wheel Drive

class AllWheelDriveCar: Car { override func accelerate(kph: Double) { frontLeft.rotate(kph) frontRight.rotate(kph) rearLeft.rotate(kph) rearRight.rotate(kph) } }

Page 44: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Defining Lexus GX

class LexusGX: AllWheelDriveCar { override var manufacturer: String { get { return "Lexus" } } }

Page 45: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

Amazing! But, can you make

me an experimental car?

CustomerYou

Page 46: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

It will switch between two-wheel drive and all-wheel

drive…

CustomerYou

Page 47: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Case study

And will turn with all four wheels.

CustomerYou

Page 48: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Experimental

Wheels

Manufacturer

Transmission

4

Front-wheel drive

Experimental

Page 49: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Experimental

Wheels

Manufacturer

Transmission

4

Front-wheel drive & All-wheel drive

Experimental

Page 50: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Experimental

Wheels

Manufacturer

Transmission

4

Front-wheel drive & All-wheel drive

Experimental

Steering All-wheel steering

Page 51: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Are you Seriously?

Page 52: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

FrontWheelDriveCar RearWheelDriveCar

- turnLeft - turnRight - rotate

Wheel

AllWheelDriveCar

Page 53: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

FrontWheelDriveCar RearWheelDriveCar

- turnLeft - turnRight - rotate

Wheel

AllWheelDriveCar

ExperimentalCar

Page 54: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

FrontWheelDriveCar RearWheelDriveCar

- turnLeft - turnRight - rotate

Wheel

AllWheelDriveCar

ExperimentalCar

Page 55: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

FrontWheelDriveCar RearWheelDriveCar

- turnLeft - turnRight - rotate

Wheel

AllWheelDriveCar

ExperimentalCar

Page 56: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Class Diagram

- manufacturer - turnLeft - turnRight - accelerate

Car

FrontWheelDriveCar RearWheelDriveCar

- turnLeft - turnRight - rotate

Wheel

AllWheelDriveCar

ExperimentalCar

The Diamond of Dread

Page 57: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Code reuse

“…It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces…”

Wikipedia, Inheritance (object-oriented programming)

Page 58: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Code reuse

“…It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces…”

Wikipedia, Inheritance (object-oriented programming)

Page 59: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Solutions

Page 60: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Demo

Page 61: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Inheritance pitfalls

• A very tight binding between a superclass and its subclasses

• Makes the code fragile

• Difficult to debug

• Inheritance relationships generally can't be altered at runtime.

• Leads to violation of Liskov Substitution Principle

• Difficult to test

Page 62: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Inheritance pitfalls

• A very tight binding between a superclass and its subclasses

• Makes the code fragile

• Difficult to debug

• Inheritance relationships generally can't be altered at runtime.

• Leads to violation of Liskov Substitution Principle

• Difficult to test

Page 63: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Inheritance pitfalls

• A very tight binding between a superclass and its subclasses

• Difficult to debug

• Inheritance relationships generally can't be altered at runtime.

• Leads to violation of Liskov Substitution Principle

• Difficult to test

• Makes the code fragile

Page 64: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Inheritance pitfalls

• A very tight binding between a superclass and its subclasses

• Difficult to debug

• Inheritance relationships generally can't be altered at runtime.

• Leads to violation of Liskov Substitution Principle

• Difficult to test

• Makes the code fragile

Page 65: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Inheritance pitfalls

• A very tight binding between a superclass and its subclasses

• Difficult to debug

• Inheritance relationships generally can't be altered at runtime.

• Leads to violation of Liskov Substitution Principle

• Difficult to test

• Makes the code fragile

Page 66: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Liskov Substitution Principle

Subtypes must be substitutable for their base types.

Robert C. Martin (Uncle Bob)

Page 67: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Violation of LSP

class File { var path: String! var data: NSData! func loadData() { // Retrieve data from disk } func saveData() { // Write data to disk } }

Page 68: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

class ReadOnlyFile: File { override func saveData() { print("Cannot write data") } }

Violation of LSP

Page 69: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Inheritance pitfalls

• A very tight binding between a superclass and its subclasses

• Difficult to debug

• Inheritance relationships generally can't be altered at runtime.

• Leads to violation of Liskov Substitution Principle

• Difficult to test

• Makes the code fragile

Page 70: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Inheritance pitfalls

• A very tight binding between a superclass and its subclasses

• Makes the code fragile

• Difficult to debug

• Inheritance relationships generally can't be altered at runtime.

• Leads to violation of Liskov Substitution Principle

• Difficult to test

Page 71: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Should we avoid inheritance altogether?

Page 72: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

To use?

Use Inheritance when your derived class truly is the type you're extending.And it will always be!

Page 73: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

The Employee

- firstName - lastName - age - department

Employee

Page 74: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

The Employee and the Student

- firstName - lastName - age - year - faculty

Student

- firstName - lastName - age - department

Employee

Page 75: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

The Employee and the Student

- firstName - lastName - age

Person

- department

Employee

- year - faculty

Student

Page 76: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

The Employee and the Student

- firstName - lastName - age

Person

- department

Employee

- year - faculty

Student

I am a Student. And an Employee.

Page 77: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

The Employee and the Student

- firstName - lastName - age

Person

- department

Employee

- year - faculty

Student

I am a Student. And an Employee.

I've just become unemployed.

Page 78: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

The Employee and the Student

- firstName - lastName - age

Person

Occupation

- department

Employee- year - faculty

Student Unemployed

Page 79: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Or not to use?

How about polymorphism?

Consider using Protocols* to achieve a polymorphic behaviour.* Also known as Interfaces in other languages

Page 80: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

I need to reuse some code from superclass. No

The derived class is almost the extending type No

The derived class is the extending type but might change in the future No

I need a polymorphic behaviour No

The derived class truly is the extending type. And it won't change. I swear!

Yes

To use or Not to use?

Page 81: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

• How does Inheritance help us to reuse the code

• Why it doesn’t work

• The pitfalls of Inheritance

• To use or not to use

Review

Page 82: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Use Inheritance Wisely!

Page 83: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Questions?

Page 84: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015

Contacts

Raifura Andrei iOS Department Manager, YOPESO

[email protected]

[email protected]

#CodeWăy

Page 85: Inheritance - the myth of code reuse | Andrei Raifura | CodeWay 2015