Patterns, Code Smells, and The Pragmattic Programmer
-
Upload
jason-mccreary -
Category
Software
-
view
96 -
download
6
Transcript of Patterns, Code Smells, and The Pragmattic Programmer
Patterns, Code Smells, and The Pragmatic
Programmer
JMac@gonedark
The journey from apprentice to craftsman
JMac != expert
JMac == craftsman
– @gonedark
“In programming, communication is king.”
Agile Software Development
Agile Testing
Analysis Patterns
Art of Capacity Planning
Art of Software Testing
Clean Code
Code Complete 2
Continuous Delivery
Continuous Integration
Design Patterns
Domain Driven Design
Even Faster Web Sites
Experiences of Test Automation
Extreme Programming Explained: Embrace Change
Founders at Work
Fundamentals of Object Oriented Design in UML
Growing Object-Oriented Software Guided by Tests
High Performance MySQL
High Performance Web Sites
Implementation Patterns
JavaScript The Good Parts
The Reading List
http://bit.ly/1bbslO0
The Pragmatic Programmer
Andrew Hunt David Thomas
Tips
• “Always use source control”
• “Don’t live with broken windows”
• “Don’t program by coincidence”
• “Work with a user to think like a user”
Stories
• Tracer Bullets
• Stone Soup and Boiled Frogs
• Rubber Ducking
– TA from CS 181
“Please stop coming to me saying, ‘gcc is broken’. It’s not. gcc has been compiling C for 30 years. You have for a month. Your code is
broken.”
– Tip #26 from The Pragmatic Programmer
“‘select’ isn’t broken.”
Invest in your Knowledge Portfolio
• Learn a new language every year
• Read books related to your field
• Be active in your community
• Experiment with new technology
• Stay current
Design Patterns
Elements of Reusable Object-Oriented Software
Erich Gamma Richard Helm
Ralph Johnson John Vlissides
Catalog of Patterns
• Creational Patterns: Builder, Singleton, Factory…
• Structural Patterns: Adapter, Facade, Decorator…
• Behavioral Patterns: Command, Iterator, Observer…
– @DHH
“It becomes quite compelling to apply these fancy ever more complicated patterns to
something… and it works, but the code isn’t better. And by ‘better’ I mean ‘clear’.”
– @gonedark
“Don’t play design pattern bingo.”
• Router
• Controller
• DataManager
• DataCreator
• JobQueue
• Job
• Repository
• Model
“…there’s a big conversation about decompiling everything and nothing should
depend on any concrete class and everything should be talking through narrow interfaces
because you just never know when you’ll need to swap out your database for an API so your
application should be ignorant of that and blah blah blah.” – @AdamWathan
– YAGNI a principle of Extreme Programming
“You aren't gonna need it.”
Refactoring Improving the Design of
Existing CodeMartin Fowler
Code Smells
• Duplicate Code
• Long Method
• Feature Envy
• Primitive Obsession
• Lazy Class
• Speculative Generality
Refactoring
– Martin Fowler
“Refactoring is the process of changing a software system in such a way that it does not
alter the external behavior of the code yet improves its internal structure.”
– Martin Fowler
“Refactoring is the process of changing a software system in such a way that it does not
alter the external behavior of the code yet improves its internal structure.”
– Martin Fowler
“… the program works. Is [refactoring] not just an aesthetic judgement, a dislike of ugly code? It is, until we want to change the system. The
compiler doesn't care whether the code is ugly or clean. But when we want to change the
system, there is a human involved, and humans do care.”
Refactor Process
• small change
• test
• repeat
Decompose Conditional
if (date.before(x) || date.after(y)) { charge = quantity * winterRate } else { charge = quantity * summerRate }
Decompose Conditional
if (isWinter(date)) { charge = winterCharge() } else { charge = summerCharge() }
Implementation Patterns
Kent Beck
Programming Values
• Communication
• Simplicity
• Flexibility
Programming Principles• Local Consequence
• Minimize Repetition
• Logic and Data Together
• Symmetry
• Declarative Expression
• Rate of Change
Symmetry
process() { input() count++ output() }
Symmetry
process() { input() increment() output() }
Symmetry
process() { input() tally() output() }
Rate of Change
setAmount(value, currency) { this.value = value this.currency = currency }
Rate of Change
setAmount(value, currency) { this.amount = new Money(val, cur) }
Programming Costs
cost[total] = cost[develop] + cost[maintain]
cost[maintain] = cost[understand] + cost[change]
+ cost[test]
+ cost[deploy]
– Kent Beck
“Programmers read more code than they write.”
– Kent Beck
“Programming would be more effective if programmers spent less time on the mundane,
repetitive parts of their job so they had more time to spend doing a good job of solving truly
unique problems.”
– The Audience
“Cool book report. So, what now?”
4 simple practices
#1 Adopt a Coding Standard
– @gonedark
“A coding standard is not about curly brace placement or spaces versus tabs. It’s about
signal versus noise.”
XXXXXX X XXXXXXXX XXXXXXXXXX
XXXXXX X XXXXXXXX X XXXXXXXXXX XXXXXXXXXXX X XXXXXXXXXX XXXXXXXXXXXXXX
XX XXXXXXXXXXXXXXX XXXXXX X XXXXXXXX XXXX XXXXXX X XXXXXXXX
XX XXXXXXXXXXXXXXX XXXXXX X XXXXXXXX
XXXX XXXXXX X XXXXXXXX
XXXXXX XXXXXXXX XXXXX XX XXXXX XXXX XXXXXX X XXXXXXXX XXXX XXXXXX X XXXXXXXX
XXXXXX XXXXXXXX XXXXX XX XXXXX XXXX
XXXXXX X XXXXXXXX XXXX XXXXXX X XXXXXXXX
#2 Write Good Enough Software
– Antoine de Saint Exupéry
“…perfection is attained not when there is nothing more to add, but when there is nothing
more to remove.”
#3 Remove Comments
“[comments are] a delicate matter, requiring taste and judgment. I tend to err on the side of
eliminating comments, for several reasons. First, if the code is clear, and uses good type names and variable names, it should explain
itself. Second, comments aren’t checked by the compiler, so there is no guarantee they’re right,
especially after the code is modified. A misleading comment can be very confusion.
Third, the issue of typography: comments clutter code.”
– Rob Pike
#4 Avoid else
testFunc(expr) { ret = false if (expr) { ret = true } else { ret = false }
return ret }
testFunc(expr) { ret = false if (expr) { ret = true } else { ret = false }
return ret }
testFunc(expr) { ret = false if (expr) { ret = true }
return ret }
testFunc(expr) { if (expr) { return true }
return false }
testFunc(expr) { return !!expr }
– @gonedark
“In programming, communication is king.”
Resources• The Reading List - http://bit.ly/1bbslO0
• What is good code? - http://bit.ly/1C535D7
• Ineffective Coding Habits - http://bit.ly/1HGgipm
• Removing Comments - http://bit.ly/1JG8n8J
• Basically anything I’ve tweeted in the last month - @gonedark