10 Ways To Improve Your Code( Neal Ford)
description
Transcript of 10 Ways To Improve Your Code( Neal Ford)
![Page 1: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/1.jpg)
ThoughtWorks
NEAL FORD software architect / meme wrangler
ThoughtWorks
[email protected] 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
![Page 2: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/2.jpg)
![Page 3: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/3.jpg)
from whence?
2 parts:
mechanics
practices
2 parts:
mechanics
practices
![Page 4: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/4.jpg)
composed method
1
![Page 5: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/5.jpg)
![Page 6: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/6.jpg)
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
![Page 7: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/7.jpg)
refactoring to composed method
![Page 8: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/8.jpg)
![Page 9: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/9.jpg)
![Page 10: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/10.jpg)
populate()
getDatabaseConnection()
createResultSet()
addPartToListFromResultSet()
PartDb
populate()
getDatabaseConnection()
createResultSet()
addPartToListFromResultSet()
PartDb
getDatabaseConnection()
BoundaryBase
populate()
createResultSet()
addPartToListFromResultSet()
PartDb
![Page 11: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/11.jpg)
populate()
createResultSet()
addPartToListFromResultSet()
PartDb
getDatabaseConnection()
BoundaryBase
populate()
createResultSet()
addPartToListFromResultSet()
PartDb
![Page 12: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/12.jpg)
BoundaryBase
PartDb
![Page 13: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/13.jpg)
populate()
getSqlForEntity()addPartToListFromResultSet()
PartDb
getDatabaseConnection()
getSqlForEntity()
createResultSet()
BoundaryBase
![Page 14: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/14.jpg)
![Page 15: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/15.jpg)
![Page 16: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/16.jpg)
addEntityToListFromResultSet()
getSqlForEntity()addPartToListFromResultSet()
PartDb
getDatabaseConnection()
getSqlForEntity()
createResultSet()
addEntityToListFromResultSet()
populate()
BoundaryBase
populate()
getSqlForEntity()addPartToListFromResultSet()
PartDb
getDatabaseConnection()
getSqlForEntity()
createResultSet()
BoundaryBase
![Page 17: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/17.jpg)
BoundaryBase
![Page 18: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/18.jpg)
PartDb
![Page 19: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/19.jpg)
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
![Page 20: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/20.jpg)
test-driven development
test-driven design2
![Page 21: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/21.jpg)
creates consumption awareness
first consumer
think about how the rest of the world uses this class
design benefits of tdd
![Page 22: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/22.jpg)
cleaner metrics
forces mocking of dependent objects
naturally creates composed method
design benefits of tdd
![Page 23: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/23.jpg)
static analysis
3
![Page 24: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/24.jpg)
byte-code analysis: findbugs
![Page 25: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/25.jpg)
bad practiceviolation of recommended & essential coding practice
correctnessprobable bug
dodgyconfusing, anomalous, written poorly
bug categories
![Page 26: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/26.jpg)
![Page 27: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/27.jpg)
![Page 28: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/28.jpg)
good citizenship
4
![Page 29: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/29.jpg)
static methods
Math.sqrt(25)
Math.sqrt()
![Page 30: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/30.jpg)
mixes responsibilities
singleton is bad because:
the object version of global variables
untestable
mixing static + state
singleton
![Page 31: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/31.jpg)
testable!
1. create a pojo for the business behavior
simple
also testable
2. create a factory to create the pojo
avoiding singletons
![Page 32: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/32.jpg)
![Page 33: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/33.jpg)
![Page 34: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/34.jpg)
![Page 35: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/35.jpg)
![Page 36: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/36.jpg)
yagni
you ain’t gonna need it5
![Page 37: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/37.jpg)
increases software entropy
build the simplest thing that we need right now
don’t indulge in speculative development
leads to frameworks
only saves time if you can guarantee you won’t have to change it later
discourages gold plating
![Page 38: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/38.jpg)
![Page 39: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/39.jpg)
This is just what they need!
![Page 40: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/40.jpg)
![Page 41: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/41.jpg)
changeability
anticipatory design
refactorable
rate of change
higherlower
![Page 42: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/42.jpg)
corporate code smells
![Page 43: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/43.jpg)
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 our 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.
![Page 44: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/44.jpg)
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.
![Page 45: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/45.jpg)
question authority6
![Page 46: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/46.jpg)
angry monkeys
![Page 47: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/47.jpg)
test namestestUpdateCacheAndVerifyThatItemExists() {
}
test_Update_cache_and_verify_that_item_exists() {
}
![Page 48: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/48.jpg)
non-intuitive
![Page 49: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/49.jpg)
![Page 50: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/50.jpg)
pair programming studies
after adjusting, pairs produced code 15% more slowly than individuals...
![Page 51: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/51.jpg)
pair programming studies
...with 15% fewer defects
![Page 52: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/52.jpg)
slap
7 single level of abstraction principle
![Page 53: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/53.jpg)
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
![Page 54: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/54.jpg)
![Page 55: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/55.jpg)
![Page 56: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/56.jpg)
polyglot
programming
8
![Page 57: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/57.jpg)
leveraging existing platforms with languages
targeted at specific problems and applications
![Page 58: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/58.jpg)
schedule pressure
massively parallel threading
use a functional language: jaskell, scala
jruby on rails, grails
looming problems/opportunities
![Page 59: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/59.jpg)
writing more declarative code via dsls
build fluent interfaces
looming problems/opportunities
![Page 60: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/60.jpg)
swiby: jruby + swing
![Page 61: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/61.jpg)
![Page 62: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/62.jpg)
every nuance
9
![Page 63: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/63.jpg)
no longer true
reflection
“reflection is slow”
elegant solutions to problems
java’s back alleys
![Page 64: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/64.jpg)
![Page 65: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/65.jpg)
![Page 66: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/66.jpg)
![Page 67: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/67.jpg)
regular expressions &
![Page 68: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/68.jpg)
![Page 69: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/69.jpg)
learn the nuances of java...
...then tell the other people on your project
![Page 70: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/70.jpg)
anti-objects
10
![Page 71: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/71.jpg)
“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
![Page 72: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/72.jpg)
![Page 73: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/73.jpg)
![Page 74: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/74.jpg)
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 evaluationsslides & samples available at nealford.com
NEAL FORD software architect / meme wrangler
ThoughtWorks
[email protected] 3003 Summit Boulevard, Atlanta, GA 30319
www.nealford.com
www.thoughtworks.com
memeagora.blogspot.com
![Page 75: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/75.jpg)
An Initial Investigation of Test Driven Development in Industry - Laurie Williams, Boby Georgehttp://collaboration.csc.ncsu.edu/laurie/Papers/TDDpaperv8.pdf
AntiPatterns Cataloghttp://c2.com/cgi/wiki?AntiPatternsCatalog
The legend of the leaning towerhttp://physicsworld.com/cws/article/print/16806
findbugshttp://findbugs.sourceforge.net/
pmd/cpdhttp://pmd.sourceforge.net/
resources
![Page 76: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/76.jpg)
resourcesSmalltalk Best Practice Patterns Kent BeckPrentice Hall PTR (October 13, 1996)ISBN-10: 013476904X
Polyglot Programminghttp://memeagora.blogspot.com/2006/12/polyglot-programming.html
Optical Illusions http://en.wikipedia.org/wiki/Optical_illusion
Collaborative Diffusion: Programming Anti-objects - A Repenninghttp://www.cs.colorado.edu/~ralex/papers/PDF/OOPSLA06antiobjects.pdf
![Page 77: 10 Ways To Improve Your Code( Neal Ford)](https://reader034.fdocuments.net/reader034/viewer/2022042613/548331c4b07959490c8b49dc/html5/thumbnails/77.jpg)
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