Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Write Code For The Future You

Write Code For The Future You

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.

Paul Gower

October 16, 2015
Tweet

More Decks by Paul Gower

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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;
  5. 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
  6. Writing Better Code u Code Complete published in 1993, 2004

    u Agile Manifesto signed 2001 u Clean Code book published in 2009
  7. Power in Variable Names u Describe the entity it represents

    u Easy to understand and read u Be specific as possible
  8. 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
  9. Ban Magic Numbers u Don’t use magic numbers u Instead

    use constants or enums u Makes code more readable / self documenting
  10. Bad Magic Number public void setPassword(String password) { // don't

    do this if (password.length() > 7) { throw new InvalidArgumentException("password"); } }
  11. Use Constants public void setPassword(String password) { const int MAX_PASSWORD_SIZE

    = 7; if (password.length() > MAX_PASSWORD_SIZE) { throw new InvalidArgumentException("password"); } }
  12. 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”
  13. Classes: Do One Thing Well u Each responsibility is an

    axis of change u Change is going to happen u Loose Coupling
  14. 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
  15. Code Reviews u Should involve the programmer and at least

    2 reviewers. u Using “peer pressure” u Encourages collaboration
  16. Code Reviews Tips u Keep a positive attitude u Check

    your ego u Review all code u Code Review early and often
  17. 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
  18. 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/)