TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in...

47
DESIGN

Transcript of TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in...

Page 1: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

DESIGN

Page 3: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

Why design matters?

Page 4: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

Longevity.

Code is used for years and years.

Page 5: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

productivity vs time

Prod

uctiv

ity

0

25

50

75

100

Time

Page 6: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

CLEAN CODE

Page 7: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

Clean Code A Handbook of Agile

Software Craftsmanship

Robert C. Martin, 2008“Uncle Bob”

Page 8: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

–Ward Cunninghaminventor of Wiki and Fit

co-inventor of eXtreme Programming

“You know you are working on clean code when each routine you read

turns out to be pretty much what you expected.”

Page 9: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

FOUR ELEMENTS OF SIMPLE DESIGN

1. Passes all tests

2. No duplication

3. Expresses intent

4. Small

Kent Beck — Creator of Extreme Programming, author of JUnit

Page 10: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

NAMINGvariables, methods, classes, ...

Page 11: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

let’s look at some examples…

Page 12: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

CORRECT ENGLISH ONLY, PLEASE!

• Word-for-word translation might not be the correct solution

• Use correct domain terminology - ask domain experts what they use

• Spelling and grammar

• Can you read your code like a story?

Page 13: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

USE THE SAME TERM CONSISTENTLY

• Customer – Client – Party – Counterparty

• state – status

• Contract – Agreement

• get – load – fetch – find – retrieve

• regCode – regNumber – personalCode

• … and don’t reuse the same term in another meaning/context

Page 14: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

FUNCTIONS

Page 15: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

A GOOD FUNCTION• short

• very few indentation levels

• does only one thing - only one level of abstraction

• “Functions should do one thing. They should do it well. They should do it only.” —Robert “Uncle Bob” Martin

Page 16: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

for example…

Page 17: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

FUNCTION ARGUMENTS

• Less is more!

• Ideally none, one and two are OK

• Three is usually too much, anything more is a design smell

Page 18: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

for example…

Page 19: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

NO SIDE-EFFECTS

• A function that seems to be read-only according to its name, should behave as such

• If a function changes state then name it accordingly

Page 20: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

COMMENTS

Page 21: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

NO COMMENTS!• Comments lie:

• You cannot tie them to code

• You cannot refactor them

• You cannot test them

• The truth is in the code

• Do not use a comment when an appropriately named variable or function would do!

Page 22: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

for example…

Page 23: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

WHEN ARE COMMENTS OK?“The proper use of comments is to compensate for our failure to

express ourselves in code.”

• Legal comments

• Warning of consequences

• TODO comments

• Amplification

• javadocs in public APIs

Page 24: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

KEEP YOUR CODE CLEAN

Page 25: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

THE BOY SCOUT RULE

Leave the campground cleaner than you found it

Can you imagine working on a project where code simply got better as time passed?

Do you believe that any other option is professional?

Page 26: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

OBJECT-ORIENTED DESIGN

Page 27: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

Agile Software Development

Principles, Patterns, and Practices

Robert C. Martin, 2003

Page 28: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

INTERFACES AND ABSTRACT CLASSES

Ignore implementation detailsProgram to interfaces

Page 29: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

JAVA COLLECTIONS API• Iterable

• Collection

• List: ArrayList, LinkedList

• Set: HashSet, LinkedHashSet

• SortedSet: TreeSet

• Map: HashMap, LinkedHashMap

• SortedMap: TreeMap

Page 30: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

JAVA.IO• byte based I/O

• InputStream & OutputStream

• FileInputStream, SocketInputStream, ByteArrayInputStream, GZIPInputStream

• character based I/O

• Reader & Writer

• FileWriter, StringWriter, OutputStreamWriter

Page 31: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

abstract class example…

Page 32: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

DEPENDENCY INJECTION INVERSION OF CONTROL

• The Hollywood Principle: Don’t call us – we’ll call you

• Don’t create your dependencies - they will be given to you

• Cleaner design

• Easier to test

Page 33: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

DI example…

Page 34: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

DEPENDENCY INJECTION

• Guice – github.com/google/guice

• Dagger - google.github.io/dagger/

• Spring Framework – projects.spring.io/spring-framework/

• Mockito – mockito.org

Page 35: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

FAVOR COMPOSITION OVER INHERITANCE

• Inheritance is for “is-a” relationships - when the same interface is most logical

• Composition is for “consists-of”, “contains”, “uses”, “has” relationships - providing decoupling – independence of each other's changes

Page 36: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

for example…

Page 37: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

IMMUTABILITY

• An immutable object is an object whose state cannot be modified after it is created

• What is the most common immutable object in the Java SDK?

• Why?Caching, thread-safety, map keys

Page 38: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

DESIGN PATTERNS

Page 39: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

Design Patterns Elements of Reusable

Object-Oriented Software

[Gang of Four Book – GOF]

Erich GammaRichard Helm Ralph Johnson John Vlissides

1994

Page 40: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

PATTERNS

• Common solutions to frequent problems arising in object-oriented design

• A systematized catalog of solutions by skilled and experienced developers

• Simplifies communication between developers –“we are using the Strategy pattern”

Page 41: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

CREATIONAL PATTERNS

the process of object creation

Factory MethodAbstract Factory

BuilderPrototypeSingleton

Page 42: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

STRUCTURAL PATTERNS the composition of classes

DecoratorAdapter (Wrapper)

ProxyFaçade

FlyweightBridge

CompositeNull Object

Page 43: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

BEHAVIORAL PATTERNShow classes or objects interact and distribute

responsibility

Template methodObserver (Publish-Subscribe, Listener)

InterpreterMemento

Chain of Responsibility

Page 44: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

BEHAVIORAL PATTERNShow classes or objects interact and distribute

responsibility

StateCommandStrategyIteratorVisitor

Mediator

Page 45: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

Refactoring to Patterns

Joshua Kerievsky, 2004

Avoid:- over-engineering

- under-engineering

Page 46: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

–Antoine de Saint-Exupery

“A designer knows he has achieved perfection not when there is nothing left to add,

but when there is nothing left to take away.”

Page 47: TVT 2015 Design - ut · PATTERNS • Common solutions to frequent problems arising in object-oriented design • A systematized catalog of solutions by skilled and experienced developers

FINAL WORLDS

The Boy Scout Rule

Experience, experience, experience

Read a lot of others’ code