Write Code For The Future You - Tulsa TechFest 2016

Post on 22-Feb-2017

131 views 1 download

Transcript of Write Code For The Future You - Tulsa TechFest 2016

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

Please help us!Thank our Sponsors:

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

Write Code For The Future You

Paul GowerPrincipal Consultant / OwnerLunamark

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Full Disclosure…

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Full Disclosure…Summer 2000

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Full Disclosure…Summer 2000

Obfuscated Perl Contest

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Full Disclosure…Summer 2000

Obfuscated Perl Contest

Mentality was to write “elite code” that was hard to understand

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Trajectory Shift

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Trajectory ShiftFirst job out of college, 2001

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Trajectory ShiftFirst job out of college, 2001Alltel Client/Server University

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Trajectory ShiftFirst job out of college, 2001Alltel Client/Server University

Textbook: Code CompleteDaily Lectures, Labs, and Code Reviews

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Real World Learning

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Real World Learning

Added to project towards end of development

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Real World Learning

Added to project towards end of developmentFixing bugs in code you didn’t write

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Real World Learning

Added to project towards end of developmentFixing bugs in code you didn’t writeThe Mythical Man Month by Fred Brooks

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Maintenance? Not me…

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Maintenance? Not me…

Creating Code30%

Maintaining Code70%

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Who reads code? Not me…

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Who reads code? Not me…Writing Code

10%

Reading Code90%

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Why write code for the reader?

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Sept 1991, Comp.lang.c++ newsgroup discussion

Why write code for the reader?

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Sept 1991, Comp.lang.c++ newsgroup discussionWhy not use comma more?

Why write code for the reader?

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Sept 1991, Comp.lang.c++ newsgroup discussionWhy not use comma more?Like this:

if (condition)var = value, anothervar = anothervalue;

Why write code for the reader?

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Sept 1991, Comp.lang.c++ newsgroup discussionWhy not use comma more?Like this:

if (condition)var = value, anothervar = anothervalue;

However consider this:if (condition)var = value; anothervar = anothervalue;

Why write code for the reader?

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

“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

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

How To Write Better Code?

@paulmgower

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

How To Write Better Code?Code Complete published in 1993, 2004

@paulmgower

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

How To Write Better Code?Code Complete published in 1993, 2004

Agile Manifesto signed 2001

@paulmgower

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

How To Write Better Code?Code Complete published in 1993, 2004

Agile Manifesto signed 2001

Clean Code book published in 2009

@paulmgower

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Power in Variable Names

Describe the entity it represents

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Power in Variable Names

Describe the entity it represents

Easy to understand and read

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Power in Variable Names

Describe the entity it represents

Easy to understand and read

Be specific as possible

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Variable Name Examples

Bad Namestotal, ct, checks, CHKTTLvelt, tv, traincd, current, c, datelpp, lines

Good NamesrunningTotal, checkTotaltrainVelocity, velocityInMphcurrentDate, todaysDatelinesPerPage

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Magic Numbers

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Magic Numbers == BADDon’t use magic numbers

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Magic Numbers == BADDon’t use magic numbers

Instead use constants, enums or config

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Magic Numbers == BADDon’t use magic numbers

Instead use constants, enums, or config

Makes code more readable / self documenting

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Don’t Do Thispublic void setPassword(String password){ // don't do this if (password.length() > 7) { throw newInvalidArgumentException("password"); } }

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

public void setPassword(String password){ const int MAX_PASSWORD_SIZE = 7; if (password.length() > MAX_PASSWORD_SIZE) { throw new InvalidArgumentException("password"); }}

Use Constants or Configuration

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

D.R.Y.

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

D.R.Y. (Don’t Repeat Yourself)

Duplication leads to maintenance issues

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

D.R.Y. (Don’t Repeat Yourself)

Duplication leads to maintenance issues

Y2K Culprit?

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

D.R.Y. (Don’t Repeat Yourself)

Duplication leads to maintenance issues

Y2K Culprit?

“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system”

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Classes: Do One Thing Well

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Classes: Do One Thing Well

Each responsibility is an axis of change

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Classes: Do One Thing Well

Each responsibility is an axis of change

Change is going to happen

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Classes: Do One Thing Well

Each responsibility is an axis of change

Change is going to happen

Loose Coupling

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

More than 1 Responsibility

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Single Responsibility

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Stop Writing Legacy Code

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Stop Writing Legacy Code

Legacy Code = Code without Tests

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Stop Writing Legacy Code

Legacy Code = Code without Tests

Great Step-by-step guide

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Stop Writing Legacy Code

Legacy Code = Code without Tests

Great Step-by-step guide

Write unit tests as you use legacy code

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Code Reviews

Author + 2 Reviewers

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Code Reviews

Author + 2 Reviewers

Using “peer pressure”

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Code Reviews

Author + 2 Reviewers

Using “peer pressure”

Encourages collaboration

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Code Reviews Tips

Keep a positive attitude

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Code Reviews Tips

Keep a positive attitudeCheck your ego

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Code Reviews Tips

Keep a positive attitudeCheck your egoReview all code

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Code Reviews Tips

Keep a positive attitudeCheck your egoReview all codeCode Review early and often

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Final Review

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Final ReviewGive better variable names

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Final ReviewGive better variable namesDon’t use magic numbers

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Final ReviewGive better variable namesDon’t use magic numbersKeep your code DRY

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Final ReviewGive better variable namesDon’t use magic numbersKeep your code DRYClasses should do one thing

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Final ReviewGive better variable namesDon’t use magic numbersKeep your code DRYClasses should do one thingWrite Unit Tests

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Final ReviewGive better variable namesDon’t use magic numbersKeep your code DRYClasses should do one thingWrite Unit TestsHave regular Code Reviews

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

Questions?

http://lunamark.com

http://bit.ly/ttf2016-wcfy

Tulsa TechFest 2016 | Fri, Aug 5th, 2016 | OSU - Tulsa | 70+ Speakers, 20+ Tracks & 85+ Sessions!

@paulmgower

ResourcesThe Mythical Man Month by Frederick P. Brooks Jr (http://www.amazon.com/The-Mythical-Man-Month-Engineering-Anniversary/dp/0201835959) Code Complete by Steve McConnell (http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670) Clean Code by Robert C. Martin (http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882) Working Effectively with Legacy Code by Michael Feathers (http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052) FogCreek Software Blog

Effective Code Reviews - 9 Tips from a Converted Skeptic (http://blog.fogcreek.com/effective-code-reviews-9-tips-from-a-converted-skeptic/) Stop More Bugs with our Code Review Checklist (http://blog.fogcreek.com/increase-defect-detection-with-our-code-review-checklist-example/)