things every professional programmer should know

61
Things Every Professional Programmer Should Know OP Student Conference 2015 @DanielSawano

Upload: daniel-sawano

Post on 08-Aug-2015

199 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Things Every Professional Programmer Should Know

Things Every Professional Programmer

Should KnowOP Student Conference 2015

@DanielSawano

Page 2: Things Every Professional Programmer Should Know

The IT Revolution

[The Delorian]

Page 3: Things Every Professional Programmer Should Know

The IT Revolution

creating the future[The Delorian]

Page 4: Things Every Professional Programmer Should Know

The IT Revolution

revolutionizing

[The Delorian]

Page 5: Things Every Professional Programmer Should Know

The Landscape is changing

[Change Machine]

Page 6: Things Every Professional Programmer Should Know

The Landscape is changing

• Competitors everywhere

[Change Machine]

Page 7: Things Every Professional Programmer Should Know

The Landscape is changing

• Competitors everywhere

• Time to market essential

[Change Machine]

Page 8: Things Every Professional Programmer Should Know

The Landscape is changing

• How to keep up?

• What to learn?

[Question Mark]

Page 9: Things Every Professional Programmer Should Know

Know Your Language

• Low level characteristics• How common data structures and

constructs affect• CPU usage• Memory usage

Page 10: Things Every Professional Programmer Should Know

Liskov Substitution Principle

“… If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of Pis unchanged when o1 is substituted for o2 then S is a subtype of T.”

- Barbara Liskov, 1988

Page 11: Things Every Professional Programmer Should Know

Liskov Substitution Principle

class Rectangle { private int width; private int height; public int width() {return width;} public void setWidth(final int width) { this.width = width; } public int height() {return height;} public void setHeight(final int height) { this.height = height; } }

Page 12: Things Every Professional Programmer Should Know

Liskov Substitution Principle

void setAndVerifyArea(final Rectangle rectangle) { rectangle.setHeight(5); rectangle.setWidth(4); int area = rectangle.height() * rectangle.width(); assertEquals(20, area); }

Page 13: Things Every Professional Programmer Should Know

Liskov Substitution Principle

void setAndVerifyArea(final Rectangle rectangle) { rectangle.setHeight(5); rectangle.setWidth(4); int area = rectangle.height() * rectangle.width(); assertEquals(20, area); }

@Test void should_verify_area() { setAndVerifyArea(new Rectangle()); }

Page 14: Things Every Professional Programmer Should Know

Liskov Substitution Principle

class Square extends Rectangle { @Override public void setWidth(final int width) { super.setWidth(width); super.setHeight(width); } @Override public void setHeight(final int height) { super.setHeight(height); super.setWidth(height); }}

Page 15: Things Every Professional Programmer Should Know

Liskov Substitution Principle

@Test void should_verify_area() { setAndVerifyArea(new Rectangle()); setAndVerifyArea(new Square()); }

Page 16: Things Every Professional Programmer Should Know

Liskov Substitution Principle

@Test void should_verify_area() { setAndVerifyArea(new Rectangle()); setAndVerifyArea(new Square()); }

void setAndVerifyArea(final Rectangle rectangle) { rectangle.setHeight(5); rectangle.setWidth(4); int area = rectangle.height() * rectangle.width(); assertEquals(20, area); }

Page 17: Things Every Professional Programmer Should Know

Liskov Substitution Principle

Page 18: Things Every Professional Programmer Should Know

Liskov Substitution Principle

• Use as guidance for inheritance

Page 19: Things Every Professional Programmer Should Know

Liskov Substitution Principle

• Use as guidance for inheritance

• Common problems with inheritance:

• overused as means to achieve polymorphism

• “broken” inheritance caused by bad domain knowledge

• used as a bad way for code reuse (DRY)

Page 20: Things Every Professional Programmer Should Know

Liskov Substitution Principle

• Use as guidance for inheritance

• Common problems with inheritance:

• overused as means to achieve polymorphism

• “broken” inheritance caused by bad domain knowledge

• used as a bad way for code reuse (DRY)

• if we brake LSP we brake substitution

Page 21: Things Every Professional Programmer Should Know

Dependency Inversion Principle

A. High level modules should not depend upon low level modules. Both should depend upon abstractions.

B. Abstractions should not depend upon details. Details should depend upon abstractions.

- Robert C. Martin, 1995

Page 22: Things Every Professional Programmer Should Know

Dependency Inversion Principle

Foo

Bar

interface

Page 23: Things Every Professional Programmer Should Know

Dependency Inversion Principle

Foo

Bar

SimpleBar

interface

implementation

Page 24: Things Every Professional Programmer Should Know

Dependency Inversion Principle

Foo

Bar

SimpleBar

Page 25: Things Every Professional Programmer Should Know

Dependency Inversion Principle

Foo

Bar

SimpleBarInverting

dependencies

Page 26: Things Every Professional Programmer Should Know

Dependency Inversion Principle

• Decouples software modules• Essential for separation of concerns,

modularization, testability• Dependency Injection is a design pattern

typically used for realizing DIP• Violation of LSP will typically cause

problems

Page 27: Things Every Professional Programmer Should Know

Composition Over Inheritance

A technique to achieve polymorphic behavior and code reuse without inheritance

Page 28: Things Every Professional Programmer Should Know

Composition Over Inheritance

interface Shape { int area(); }

Page 29: Things Every Professional Programmer Should Know

Composition Over Inheritance

interface Shape { int area(); }

class Rectangle implements Shape { private final int width, height; Rectangle(final int width, final int height) { this.width = width; this.height = height; } @Override public int area() { return width * height; } }

Page 30: Things Every Professional Programmer Should Know

Composition Over Inheritance

class Square implements Shape { private final Rectangle rectangle; public Square(final int width) { rectangle = new Rectangle(width, width); } @Override public int area() { return rectangle.area(); }}

Page 31: Things Every Professional Programmer Should Know

Composition Over Inheritance

• Helps you avoid accidentally violating LSP

• Makes your design more stable in the long term

• Makes your code more flexible

• Solves situations where you otherwise would need multiple inheritance

Page 32: Things Every Professional Programmer Should Know

Encapsulation

Generally used to refer to one or both of:• information hiding

(e.g. hide the implementation)• bundling data with the methods that

operates on the data (e.g. angle.sin() vs Math.sin(x))

Page 33: Things Every Professional Programmer Should Know

Encapsulation

Helps make your code:• modularized• adaptive to change

Page 34: Things Every Professional Programmer Should Know

Encapsulation

Adaptive to change• Internals can be changed without

affecting others• One implementation can be substituted

for another

Page 35: Things Every Professional Programmer Should Know

Encapsulation

“Because inheritance exposes a subclass to details of its parent's implementation, it's often said that 'inheritance breaks encapsulation’”

- Design Patterns, “Gang of Four”, 1995

Page 36: Things Every Professional Programmer Should Know

Immutability

“An immutable object is one whose state cannot be changed after construction”

- Java Concurrency in Practice, B. Göetz et.al, 2006

Page 37: Things Every Professional Programmer Should Know

Immutability

• Mutability itself is not bad• Problem is when mutable state

becomes shared

Page 38: Things Every Professional Programmer Should Know

Immutabilityclass Rectangle implements Shape { private int width, height; Rectangle(final int width, final int height) { this.width = width; this.height = height; } public int width() { return width; }

public void setWidth(final int width) { this.width = width; } public int height() { return height; }

public void setHeight(final int height) { this.height = height; } }

Page 39: Things Every Professional Programmer Should Know

Immutability

class Rectangle implements Shape { private final int width, height; Rectangle(final int width, final int height) { this.width = width; this.height = height; } public Rectangle withWidth(final int width) { return new Rectangle(width, height); } public Rectangle withHeight(final int height) { return new Rectangle(width, height); } }

Page 40: Things Every Professional Programmer Should Know

How it all fits together

Page 41: Things Every Professional Programmer Should Know

How it all fits together

Know your languageLiskov Substitution Principle

Dependency InversionPrinciple

Composition Over Inheritance

Encapsulation

Immutability

Page 42: Things Every Professional Programmer Should Know

Continuous Integration

Page 43: Things Every Professional Programmer Should Know

Continuous Integration

• Testable

Page 44: Things Every Professional Programmer Should Know

Continuous Integration

• Testable=> DIP

Page 45: Things Every Professional Programmer Should Know

Continuous Integration

• Testable=> DIP

• Test different parts separately

Page 46: Things Every Professional Programmer Should Know

Continuous Integration

• Testable=> DIP

• Test different parts separately=> modularized code base

Page 47: Things Every Professional Programmer Should Know

Continuous Integration

• Testable=> DIP

• Test different parts separately=> modularized code base => separation of concerns/DIP

Page 48: Things Every Professional Programmer Should Know

Continuous Integration

• Testable=> DIP

• Test different parts separately=> modularized code base => separation of concerns/DIP

• Support small, incremental changes

Page 49: Things Every Professional Programmer Should Know

Continuous Integration

• Testable=> DIP

• Test different parts separately=> modularized code base => separation of concerns/DIP

• Support small, incremental changes=> encapsulation, composition, DIP

Page 50: Things Every Professional Programmer Should Know

Continuous Delivery

Code should always be deliverable

Page 51: Things Every Professional Programmer Should Know

Continuous Delivery

Code should always be deliverable

• Deployable in different environments

Page 52: Things Every Professional Programmer Should Know

Continuous Delivery

Code should always be deliverable

• Deployable in different environments=> IoC/DIP

Page 53: Things Every Professional Programmer Should Know

Continuous Delivery

Code should always be deliverable

• Deployable in different environments=> IoC/DIP

• Deliver small pieces separately

Page 54: Things Every Professional Programmer Should Know

Continuous Delivery

Code should always be deliverable

• Deployable in different environments=> IoC/DIP

• Deliver small pieces separately=> modularized code

Page 55: Things Every Professional Programmer Should Know

Continuous Delivery

Code should always be deliverable

• Deployable in different environments=> IoC/DIP

• Deliver small pieces separately=> modularized code

=> LSP, DIP, encapsulation, low coupling

Page 56: Things Every Professional Programmer Should Know

Continuous Delivery

Code should always be deliverable

• Deployable in different environments=> IoC/DIP

• Deliver small pieces separately=> modularized code

=> LSP, DIP, encapsulation, low coupling

• Keep business logic separated from infrastructure

Page 57: Things Every Professional Programmer Should Know

Continuous Delivery

Code should always be deliverable

• Deployable in different environments=> IoC/DIP

• Deliver small pieces separately=> modularized code

=> LSP, DIP, encapsulation, low coupling

• Keep business logic separated from infrastructure=> encapsulation, IoC, design patterns

Page 58: Things Every Professional Programmer Should Know

Deployable Anywhere

Your app should run:• containerless• in the cloud on a PaaS• on a company server

The Twelve-Factor App: http://12factor.net

Page 59: Things Every Professional Programmer Should Know

Deployable Anywhere

Your app should run:• containerless• in the cloud on a PaaS• on a company server

=> enabled by DIP

The Twelve-Factor App: http://12factor.net

Page 60: Things Every Professional Programmer Should Know

Awesome Images

1. [The Delorian - https://flic.kr/p/fFXKMu] by William Warby under license https://creativecommons.org/licenses/by/2.0/

2. [Change Machine - https://flic.kr/p/6BH6Kq] by Tracy Shaun under license https://creativecommons.org/licenses/by-sa/2.0/

3. [Cray CPU - https://flic.kr/p/dcy51L] by Brad Montgomery under license https://creativecommons.org/licenses/by/2.0/

4. [Question Mark - https://flic.kr/p/6okjAW] by Marco Belluci under license https://creativecommons.org/licenses/by/2.0/

Page 61: Things Every Professional Programmer Should Know

Thank you!@DanielSawano