the productive programmer: practice 10 ways to improve your code - - PowerPoint PPT Presentation

the productive programmer practice 10 ways to improve
SMART_READER_LITE
LIVE PREVIEW

the productive programmer: practice 10 ways to improve your code - - PowerPoint PPT Presentation

Thought Works the productive programmer: practice 10 ways to improve your code NEAL FORD software architect / meme wrangler Thought Works nford@thoughtworks.com 3003 Summit Boulevard, Atlanta, GA 30319 www.nealford.com www.thoughtworks.com


slide-1
SLIDE 1

ThoughtWorks

NEAL FORD software architect / meme wrangler

ThoughtWorks

nford@thoughtworks.com

3003 Summit Boulevard, Atlanta, GA 30319

www.nealford.com www.thoughtworks.com memeagora.blogspot.com

the productive programmer: practice 10 ways to improve your code

slide-2
SLIDE 2
slide-3
SLIDE 3

from whence?

2 parts: mechanics practices 2 parts: mechanics practices

slide-4
SLIDE 4

composed method

1

slide-5
SLIDE 5
slide-6
SLIDE 6

Keep all of the operations in a method at the same level of abstraction. Divide your program into methods that perform one identifiable task. This will naturally result in programs with many small methods, each a few lines long.

composed method

slide-7
SLIDE 7

refactoring to composed method

slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10

populate() getDatabaseConnection() createResultSet() addPartToListFromResultSet() PartDb populate() getDatabaseConnection() createResultSet() addPartToListFromResultSet() PartDb

getDatabaseConnection() BoundaryBase

populate() createResultSet() addPartToListFromResultSet() PartDb

slide-11
SLIDE 11

populate() createResultSet() addPartToListFromResultSet() PartDb

getDatabaseConnection() BoundaryBase

populate() createResultSet() addPartToListFromResultSet() PartDb

slide-12
SLIDE 12

BoundaryBase PartDb

slide-13
SLIDE 13

populate()

getSqlForEntity()

addPartToListFromResultSet() PartDb

getDatabaseConnection() getSqlForEntity() createResultSet() BoundaryBase

slide-14
SLIDE 14
slide-15
SLIDE 15
slide-16
SLIDE 16

addEntityToListFromResultSet() getSqlForEntity()

addPartToListFromResultSet() PartDb

getDatabaseConnection() getSqlForEntity() createResultSet() addEntityToListFromResultSet() populate() BoundaryBase

populate()

getSqlForEntity()

addPartToListFromResultSet() PartDb

getDatabaseConnection() getSqlForEntity() createResultSet() BoundaryBase

slide-17
SLIDE 17

BoundaryBase

slide-18
SLIDE 18

PartDb

slide-19
SLIDE 19

large number of very cohesive methods shorter methods easier to test method names become documentation discover reusable assets that you didn’t know were there

benefits of composed method

slide-20
SLIDE 20

test-driven development test-driven design

2

slide-21
SLIDE 21

creates consumption awareness first consumer think about how the rest of the world uses this class

design benefits of tdd

slide-22
SLIDE 22

cleaner metrics forces mocking of dependent objects naturally creates composed method

design benefits of tdd

slide-23
SLIDE 23

static analysis

3

slide-24
SLIDE 24

byte-code analysis: findbugs

slide-25
SLIDE 25

bad practice violation of recommended & essential coding practice correctness probable bug dodgy confusing, anomalous, written poorly

bug categories

slide-26
SLIDE 26
slide-27
SLIDE 27
slide-28
SLIDE 28

good citizenship

4

slide-29
SLIDE 29

static methods

Math.sqrt(25) Math.sqrt()

slide-30
SLIDE 30

mixes responsibilities singleton is bad because: the object version of global variables untestable

mixing static + state

singleton

slide-31
SLIDE 31

testable!

  • 1. create a pojo for the business behavior

simple also testable

  • 2. create a factory to create the pojo

avoiding singletons

slide-32
SLIDE 32
slide-33
SLIDE 33
slide-34
SLIDE 34
slide-35
SLIDE 35
slide-36
SLIDE 36

yagni

you ain’t gonna need it

5

slide-37
SLIDE 37

increases software entropy build the simplest thing that we need right now don’t indulge in speculative development leads to frameworks

  • nly saves time if you can guarantee you

won’t have to change it later

discourages gold plating

slide-38
SLIDE 38
slide-39
SLIDE 39

This is just what they need!

slide-40
SLIDE 40
slide-41
SLIDE 41

changeability

anticipatory design refactorable rate of change higher lower

slide-42
SLIDE 42

corporate code smells

slide-43
SLIDE 43
  • 6. We have an Architect who reviews all code pre-

checkin and decides whether or not to allow it into version control.

  • 7. We can’t use any open source code because
  • ur lawyers say we can’t.
  • 8. We use WebSphere because...(I always stop

listening at this point)

  • 10. We invented our own web/persistence/

messaging/caching framework because none of the existing ones was good enough.

  • 9. We bought the entire tool suite (even though

we only needed about 10% of it) because it was cheaper than buying the individual tools.

slide-44
SLIDE 44
  • 1. There is a reason that WSAD isn’t called

WHAPPY.

  • 2. The initial estimate must be within 15% of the

final cost, the post-analysis estimate must be within 10%, and the post-design estimate must be with 5%

  • 3. We don’t have time to write unit tests (we’re

spending too much time debugging)

  • 5. The only JavaDoc is the Eclipse message

explaining how to change your default JavaDoc template.

  • 4. We keep all of our business logic in stored

procedures...for performance reasons.

slide-45
SLIDE 45

question authority

6

slide-46
SLIDE 46

angry monkeys

slide-47
SLIDE 47

test names

testUpdateCacheAndVerifyThatItemExists() { } test_Update_cache_and_verify_that_item_exists() { }

slide-48
SLIDE 48

non-intuitive

slide-49
SLIDE 49
slide-50
SLIDE 50

pair programming studies

after adjusting, pairs produced code 15% more slowly than individuals...

slide-51
SLIDE 51

pair programming studies

...with 15% fewer defects

slide-52
SLIDE 52

slap

7

single level of abstraction principle

slide-53
SLIDE 53

composed method => slap keep all lines of code in a method at the same level of abstraction jumping abstraction layers makes code hard to understand even if it means single-line methods refactor to slap

s l a p

slide-54
SLIDE 54
slide-55
SLIDE 55
slide-56
SLIDE 56

polyglot programming

8

slide-57
SLIDE 57

leveraging existing platforms with languages targeted at specific problems and applications

slide-58
SLIDE 58

schedule pressure massively parallel threading use a functional language: jaskell, scala jruby on rails, grails

looming problems/

  • pportunities
slide-59
SLIDE 59

writing more declarative code via dsls build fluent interfaces

looming problems/

  • pportunities
slide-60
SLIDE 60

swiby: jruby + swing

slide-61
SLIDE 61
slide-62
SLIDE 62

every nuance

9

slide-63
SLIDE 63

no longer true reflection “reflection is slow” elegant solutions to problems

java’s back alleys

slide-64
SLIDE 64
slide-65
SLIDE 65
slide-66
SLIDE 66
slide-67
SLIDE 67

regular expressions &

slide-68
SLIDE 68
slide-69
SLIDE 69

learn the nuances of java... ...then tell the other people on your project

slide-70
SLIDE 70

anti-objects

10

slide-71
SLIDE 71

“The metaphor of objects can go too far by making us try to create objects that are too much inspired by the real world. “ “...an antiobject is a kind of object that appears to essentially do the opposite of what we generally think the object should be doing.”

collaborative diffusion

slide-72
SLIDE 72
slide-73
SLIDE 73
slide-74
SLIDE 74

ThoughtWorks

This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 2.5 License. http://creativecommons.org/licenses/by-nc-sa/2.5/

questions?

please fill out the session evaluations slides & samples available at nealford.com

NEAL FORD software architect / meme wrangler

ThoughtWorks

nford@thoughtworks.com

3003 Summit Boulevard, Atlanta, GA 30319

www.nealford.com www.thoughtworks.com memeagora.blogspot.com

slide-75
SLIDE 75

An Initial Investigation of Test Driven Development in Industry - Laurie Williams, Boby George

http://collaboration.csc.ncsu.edu/laurie/Papers/TDDpaperv8.pdf

AntiPatterns Catalog

http://c2.com/cgi/wiki?AntiPatternsCatalog

The legend of the leaning tower

http://physicsworld.com/cws/article/print/16806

findbugs

http://findbugs.sourceforge.net/

pmd/cpd

http://pmd.sourceforge.net/

resources

slide-76
SLIDE 76

resources

Smalltalk Best Practice Patterns Kent Beck

Prentice Hall PTR (October 13, 1996) ISBN-10: 013476904X

Polyglot Programming

http://memeagora.blogspot.com/2006/12/polyglot-programming.html

Optical Illusions

http://en.wikipedia.org/wiki/Optical_illusion

Collaborative Diffusion: Programming Anti-objects - A Repenning

http://www.cs.colorado.edu/~ralex/papers/PDF/OOPSLA06antiobjects.pdf

slide-77
SLIDE 77

resources

http://www.cs.utah.edu/~lwilliam/Papers/ ieeeSoftware.PDF

http://collaboration.csc.ncsu.edu/laurie/Papers/ XPSardinia.PDF

http://www.xprogramming.com/Practices/PracPairs.html http://c2.com/cgi/wiki?PairProgramming

pair programming