mpg feb08 gian lorenzetto

24
Does Good Code Matter? Gian Lorenzetto

Upload: melbournepatterns

Post on 22-Apr-2015

573 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Mpg Feb08 Gian Lorenzetto

Does Good Code Matter?

Gian Lorenzetto

Page 2: Mpg Feb08 Gian Lorenzetto

Implementation Patterns

By Kent Beck

• 157 Pages / 77 Implementation Patterns

• Not a “patterns” book

• Java programming habits for creating readable code

• Conscious approach

• what do I want to tell the reader about this code?

• what does the code communicate what I am thinking to others?

Page 3: Mpg Feb08 Gian Lorenzetto

This book is not ...

• Not a style guide

• Not a design book

• Not a patterns book (!?)

• Not a language guide

• Not for beginners (despite what Amazon thinks ...)

Page 4: Mpg Feb08 Gian Lorenzetto

This book is ...• About writing good code for others -

• to read, to understand, to learn from and to extend

• For those who think life is too short to waste on bad code

• For those who delight in writing beautiful code

• Like spending a few hours inside Kent Beck’s head while he’s hacking JUnit

Page 5: Mpg Feb08 Gian Lorenzetto

Premise

• “I have seen too much ugly code make too much money to believe that quality of code is either necessary or sufficient for commercial success or wide-spread use.”

• Communication - “ ... programming so other people can understand your code.”

• Does good code matter?

Page 6: Mpg Feb08 Gian Lorenzetto

Book OverviewIntroduction

Class

Behavior

Methods

State

CollectionsFrameworks

• Patterns• Values & Principles• Motivation

Similar Logic Different Data

Dividing Logic Multi-Valued Data

Page 7: Mpg Feb08 Gian Lorenzetto

1. Introduction

• Overview

• Question minute-to-minute decisions

• Why am I so sure this method should be called foo?

• Program consciously, for others

• Format - essays, diagrams, teaching story, examples

Page 8: Mpg Feb08 Gian Lorenzetto

Design Pattern Language Guide

Implementation Patterns

Few per day

Every fewseconds

Regulate object interaction

Why choose a particular construct?

Page 9: Mpg Feb08 Gian Lorenzetto

2. Patterns

• Programming laws (forces)

1. Read more often than written

2. No such thing as done

3. Structured using basic set of state and control flow concepts

4. Readers need to understand in detail and in concept

• Patterns illustrate a point of view about the relative priorities of these forces

Page 10: Mpg Feb08 Gian Lorenzetto

3. A Theory of Programming

• Values

• Over-arching universal themes of programming

• Communication, Simplicity, Flexibility

• Principles

• Bridge the values

Page 11: Mpg Feb08 Gian Lorenzetto

3. A Theory of Programming

Values PatternsPrinciples

MotivationTranslate motive

into actionWhat to do

Page 12: Mpg Feb08 Gian Lorenzetto

• Principles

• Local consequences

• Minimize repetition

• Logic and data together

• Symmetry

• Declarative expression

• Rate-of-change

3. A Theory of Programming

Page 13: Mpg Feb08 Gian Lorenzetto

4. Motivation

• cost total = cost develop + cost maintain

• Increase up front costs does not help

• Implementation patterns satisfy human and economic needs

Page 14: Mpg Feb08 Gian Lorenzetto

5. Class

• Bundles related state

• Class

• Simple Superclass Name

• Interface

• Value Object

• Inner Class

• Conditional

Page 15: Mpg Feb08 Gian Lorenzetto

6. State

• Communicate use of state

• Objects package:

• Behaviour (presented to outside world)

• State (supports behaviour)

• Objects make analysing state easier

Page 16: Mpg Feb08 Gian Lorenzetto

6. State

• Patterns

• Access (Direct, Indirect)

• State (Common, Variable, Extrinsic)

• Variable (Local, Field, Parameter)

• Role Suggesting Name

• Declared Type

• Initialisation (Eager, Lazy)

Page 17: Mpg Feb08 Gian Lorenzetto

The best intentions ...x = 10;

setX(10);

openGarageDoor(10);

openGarageDoor() { setX(10);}

Page 18: Mpg Feb08 Gian Lorenzetto

7. Behaviour

• How to express behaviour of a program

• Control Flow

• Message (Choosing, Double Dispatch, Reversing, Inviting, Explaining)

• Guard Clause

• Exception

Page 19: Mpg Feb08 Gian Lorenzetto

Guards, Guards!void initialise() { if (!isInitialised()) { ... }}

• One entry point, one exit point

void initialise() { if (isInitialised()) { return; } ...}

• Express simple and local exceptional situations with purely local consequences

• Highlight the difference between normal and exceptional processing

Page 20: Mpg Feb08 Gian Lorenzetto

8. Methods

• Why divide logic into chunks?

• Readability, critical flow, understanding, communication ...

• Logic into methods

• Methods into classes

• Classes into packages

Communicateconnection of

logic

Page 21: Mpg Feb08 Gian Lorenzetto

8. Methods• Deciding what goes where is hard

• Compose Method

• Intention-Revealing Name

• Method Visibility

• Method Object

• Collection Accessor

• Query Method

• Setting Method

Page 22: Mpg Feb08 Gian Lorenzetto

9. Collections

• Patterns

• Metaphors

• Issues

• Interfaces (Array, Iterable, Collection ...)

• Implementations (Collection, List, Set, Map)

• Collections (Searching, Sorting, Unmodiiable, Single Element, Empty)

• Extending Collections

Page 23: Mpg Feb08 Gian Lorenzetto

Example ...stripHeader();stripFooter();process();

Attempt 1

strip???();process();Attempt 2

findTableElement();process();

Attempt 3

Page 24: Mpg Feb08 Gian Lorenzetto

The End