Many believe the days of staying at a job long enough to retire is gone. A typical worker in the United States switches jobs ever 4-5 years and in the tech industry it is even shorter. Due to this there is an even greater need now to improve the quality of the code you write and / or maintain in your job as a software developer. In this session, we will discuss what it means to improve the quality of the code and how you can work towards improving it every day. Also we will go over a few practical tips you can start using today to improve the quality of your code.
2. Full Disclosure…
u First Internship writing Perl script, summer 2000
u Obfuscated Perl Contest
u Annual competition between 1996 and 2000
u Entries were judged on incomprehensibility among other
things
u Mentality was to write “elite code” that was hard to
understand
3. Story Time
u First job out of college, 2001
u Alltel Client/Server University
u 6 month training class
u Textbook: Code Complete
u 8a to 5p every day
u Lecture
u Lab
u Code Reviews
4. Story Time
u Added to project towards end of
development
u Fixing bugs in code you didn’t write
u The Mythical Man Month by Fred Brooks
7. Why Write Code for Reader?
u Sept 1991, Comp.lang.c++ newsgroup discussion
u Why not use comma more?
u Like this:
if (condition)
var = value, anothervar = anothervalue;
u However consider this:
if (condition)
var = value; anothervar = anothervalue;
8. Food for thought
“Always code as if the guy who
ends up maintaining your code
will be a violent psychopath who
knows where you live”
John F. Woods
9. Writing Better Code
u Code Complete published in 1993, 2004
u Agile Manifesto signed 2001
u Clean Code book published in 2009
10. Power in Variable Names
u Describe the entity it represents
u Easy to understand and read
u Be specific as possible
11. Variable Name Examples
Bad Names
u total, ct, checks, CHKTTL
u velt, tv, train
u cd, current, c, date
u lpp, lines
Good Names
u runningTotal, checkTotal
u trainVelocity, velocityInMph
u currentDate, todaysDate
u linesPerPage
12. Ban Magic Numbers
u Don’t use magic numbers
u Instead use constants or enums
u Makes code more readable / self
documenting
13. Bad Magic Number
public void setPassword(String password) {
// don't do this
if (password.length() > 7) {
throw new InvalidArgumentException("password");
}
}
14. Use Constants
public void setPassword(String password) {
const int MAX_PASSWORD_SIZE = 7;
if (password.length() > MAX_PASSWORD_SIZE) {
throw new InvalidArgumentException("password");
}
}
15. Don’t Repeat Yourself (DRY)
u Duplication leads to maintenance issues
u Y2K Culprit?
“Every piece of knowledge must have a
single, unabiguous, authoritative
representation within a system”
16. Classes: Do One Thing Well
u Each responsibility is an axis of
change
u Change is going to happen
u Loose Coupling
19. Stop Writing Legacy Code
u Legacy Code = Code without Tests
u Great Step-by-step guide
u Write Unit Test as you use legacy
methods
20. Code Reviews
u Should involve the programmer and at
least 2 reviewers.
u Using “peer pressure”
u Encourages collaboration
21. Code Reviews Tips
u Keep a positive attitude
u Check your ego
u Review all code
u Code Review early and often
22. Final Review
u Give better variable names
u Don’t use magic numbers
u Keep your code DRY
u Classes should do one thing
u Write Unit Tests
u Have regular Code Reviews
24. Resources
u The Mythical Man Month by Frederick P. Brooks Jr
(http://www.amazon.com/The-Mythical-Man-Month-Engineering-
Anniversary/dp/0201835959)
u Code Complete by Steve McConnell
(http://www.amazon.com/Code-Complete-Practical-Handbook-
Construction/dp/0735619670)
u Clean Code by Robert C. Martin (http://www.amazon.com/Clean-
Code-Handbook-Software-Craftsmanship/dp/0132350882)
u Working Effectively with Legacy Code by Michael Feathers
(http://www.amazon.com/Working-Effectively-Legacy-Michael-
Feathers/dp/0131177052)
u FogCreek Software Blog
u Effective Code Reviews - 9 Tips from a Converted Skeptic
(http://blog.fogcreek.com/effective-code-reviews-9-tips-from-a-
converted-skeptic/)
u Stop More Bugs with our Code Review Checklist
(http://blog.fogcreek.com/increase-defect-detection-with-our-code-
review-checklist-example/)