82
Clean Code Days 2016 28.06.2016 http://slides.com/carstenwindler/the-lone-stranger

Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Clean CodeDays 2016

28.06.2016

http://slides.com/carstenwindler/the-lone-stranger

Page 2: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Why you can't establishClean Code on your own

The Lone Stranger

Carsten Windler, HolidayPirates GmbH

Page 3: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

if ( is_array($fields ) ) { if ( isset($fields['blog_id']) ) { $blog_id = $fields['blog_id']; } elseif ( isset($fields['domain']) && isset($fields['path']) ) { $key = md5( $fields['domain'] . $fields['path'] ); $blog = wp_cache_get($key, 'blog-lookup'); if ( false !== $blog ) return $blog; if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { $nowww = substr( $fields['domain'], 4 ); $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC" } else { $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) ); } if ( $blog ) { wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); $blog_id = $blog->blog_id; } else { return false; } } elseif ( isset($fields['domain']) && is_subdomain_install() ) { $key = md5( $fields['domain'] ); $blog = wp_cache_get($key, 'blog-lookup'); if ( false !== $blog ) return $blog; if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { $nowww = substr( $fields['domain'], 4 ); $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields[ } else { $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) ); } if ( $blog ) { wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); $blog_id = $blog->blog_id; } else { return false; } } else { return false; }} else { if ( ! $fields ) $blog_id = get_current_blog_id(); elseif ( ! is_numeric( $fields ) ) $blog_id = get_id_from_blogname( $fields ); else $blog_id = $fields;}

$blog_id = (int) $blog_id;

$all = $get_all == true ? '' : 'short';$details = wp_cache_get( $blog_id . $all, 'blog-details' );

if ( $details ) { if ( ! is_object( $details ) ) { if ( $details == -1 ) { return false; } else { // Clear old pre-serialized objects. Cache clients do better with that.

Page 4: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

unprofessionalwill make you unhappy

a.k.a. Sisyphos™other developers will keep the coderottingwill introduce new bugsleads to trouble with other developers

Page 5: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

So the answer isnot really surprisingly

Page 6: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Clean Code!... but how?

Page 7: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

So youdecided tointroduceClean Codeto your

projectSorry...

Page 8: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Introducing Clean Code toan existing project

takes timeis painfulis cumbersome

...is totally worth it

Page 9: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Management

Customer

Co-Worker

Otherdepartments

People you might need to convince:

Page 10: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

#1 - The Arguments

Page 11: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

The Management

Page 12: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Now you have to explain why you wantto spent more time on doing the same

stuff as before.

The Business likes charts.Let's give them some charts.

Page 13: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Project without Clean Code*

LifeTechnology changeRequirements changePersonnel change

*simplification; not necessarily related to any real project

Page 14: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Technology changeFramework updateLanguage updateInfrastructure change because of technical need

Requirements changeNew top prio feature requires many changes

Personnel changeTruck factor hits you

Page 15: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Truck Factor explained

your former lead dev(yes, under the truck)

Page 16: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Project as a rollercoaster

Page 17: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Project with Clean Code

Page 18: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Sloppiness Short TermBoost (SSTB)

Clean Code LongTerm Benefit

(CCLTB)

Projects compared

Page 19: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Time considerations

t 0 t1 t2

StartupPhase

ConsolidationPhase

Credit Interest

Page 20: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

f(x ) dx < f(x ) dx∫t0

t1s ∫

t0

t1c

fewer backlog items are left when doing sloppyprogramming instead of clean code in the

startup phase

t 0 t 1

f(x )s

f(x )c

Page 21: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

f(x ) dx > f(x ) dx∫t0

t2s ∫

t0

t2c

more backlog items left when doing sloppyprogramming instead of Clean Code over

project lifetime

t 0 t 2

f(x )c

f(x )s

Page 22: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Sometimes it's a Businessdecision to make debts

StartupMan

Page 23: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Sometimes it's ignorance

Page 24: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

???

Wait, wait, wait... it's for theManagement, so keep it simple!

f(x ) dx > f(x ) dx∫t0

t2s ∫

t0

t2c

Page 25: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Quality is an investment

Developer scalability

Lower per-bug-costs

Customer satisfaction

Why Clean Code matters

Page 26: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Quality as an investmentdon't think short term future changes on platform orrequirements will be easieravoid piling up technical debt

Page 27: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Developer scalabilitynew developers can be added to aproject easiergathered experience and techniquescan be transferred to other projects

Page 28: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Lower per-bug-costscatching a bug on developmentstage is much cheaper than onLivepreventing regressions with liveoutage

Page 29: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Customer SatisfactionCustomer experiences morestable productbetter reputation

Page 30: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

The Customer...ouch...Leave it up to the Management, if you can!

Page 31: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Now you have to explain why you wantthem to spent more money on getting

the same results as before.

Page 32: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Quality is an investment

End-Customer satisfaction

Develop agile

Why Clean Code matters foryour customer

Page 33: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Quality is an investmentdon't think short termfuture changes on platform orrequirements will be easierCustomer is lesser depending on yourcompany*

*you probably don't want to tell that though

Page 34: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

End-Customer satisfactionmore stable producthappier customersbetter reputationlesser outages

Page 35: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Develop agileIntroduce and evaluate newfeatures faster

Page 36: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Co-Workers

Page 37: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Now you have to explain whyyou want them to spent moretime on doing the same stuff

than beforeYes, once again!

Page 38: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Happy refactoring

Saves development timeEgo

Lesser stress during deploys

Developer scalability

Page 39: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Happy refactoringtests give you a much moreconfidence when refactoringthus it will encourage to refactor ifneeded

Page 40: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Saves timeautomated tests can save a lot of timeyou don't need to click through half ofthe application to test a single smallfeaturebugs are usually easier to solve whenyou just worked on a topic than weeksafter

Page 41: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Egobugs discovered by others are often abit embarassingoverall project / developer reputationincreases

Page 42: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Lesser stressduring deploys

deploy with confidence

Page 43: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Developer scalabilityeasier to integrate new colleagues

Page 44: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Turn developers from this...

Page 45: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

... into this

Page 46: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

How to get developers on your sidelive what you meanprovide good examples on the projectinhouse workshopsfind alliesprovide THE TOOLS

Page 47: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

#2 The Tools

Page 48: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Code StyleGuides

Improve readabilityA matter ofprofessionalismVery easy to introduce

Page 49: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Code StyleGuides

best use existing codestandardssniffers help you check andreformat automaticallyrun e.g. on pre-commit onchanged files only

Page 50: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Software Metrics

Page 51: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

What can you do with these numbers?

Page 52: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Hey Bob, how is yourproject going?

Awesome! During last the Refactoring Sprint we managed to dropthe overall cyclomatic complexity by ~5 points (which is great).However we still have a maintenability index of 64.32 on the

Middleware which we will change ASAP.

Nice

Page 53: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of
Page 54: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of
Page 55: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Your project on a T-Shirt

Page 56: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of
Page 57: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

REFACTORING TIME!!

Page 58: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Why is my code so bad?

Page 59: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Software

Metricseasy to introducewill show you how good /how bad your codebaseactually ismake developers awareof software qualityhelps identifying problemareas

Source CodeAnalyzer

easy to introducewill give you direct hintswhere in your code thingsgo wrongCopy & Paste detectorswill not allow lazynessprevents bad code fromgetting added to thecodebase

Page 60: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Putting the finger where it hurts

Page 61: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

However no tool will makeyou a better programmer

Page 62: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Never stop learning!

Page 63: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Code Reviewsfairly easy it introduce*actually a must have

can prevent really nasty bugsgreat for sharing knowledge

be careful - don't be arrogantsome devs prefer eye-to-eye reviews,others are ok with comments in theMerge Request

*assuming you use Version Control Systems

Page 64: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Tests

Page 65: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

KISSKeep It teSted, Stupid!

Tests are not optional, they are mandatory.

Period.

We mean it.

Seriously.

Page 66: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Write / RefactorAutomated Tests

Ideal world

Page 67: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

But what if wehave no Unit Tests

yet??

Page 68: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Write / RefactorBugs

Harsh reality

Page 69: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Writing Tests for untested classesusually requires refactoring

refactoring without tests == risky!chicken-egg-problemconcentrate on Unit Tests for new classesfor legacy code, try to find simple classes to start with

Page 70: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

ManualTests

E2E

Unit Tests

The Almighty Test Pyramid

E2Enot (yet) available

not an option

Page 71: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

- slow- a bit fragile

- require test mode

E2E Tests

your way to go before you start with refactoring

+ require no refactoring+ require no deep code knowledge

+ can be done in a sane amount of time

Page 72: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Workflowstools are nothing without controlsetup clearly documented workflowsbe stubborn, otherwise sloppiness willcome backjust like animals, some developers willSMELL any fear or weaknessso it's a good idea to automize

Page 73: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Continuous IntegrationFail build immediately

Code SnifferSource Code AnalyzerUnit TestsAcceptance Tests

ReportsSoftware MetricsTest coverage

Page 74: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Whatisleft?

Page 75: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Clean Code is NOT aboutbeing the best coder in the

world.(although you have to be pretty good)

Page 76: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Clean Code is aboutprofessionalism.

Page 77: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Clean Code is aboutknowing that sloppy code

will cost a lot of money.

Page 78: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

And:

Clean Code is about beingproud of your code!

Page 79: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

And your job.

Page 80: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

It's probably the best job inthe world.

Page 81: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Thank you.

Page 82: Clean Code Days 2016 - embedded-testing.de...catching a bug on development stage is much cheaper than on Live preventing regressions with live ... automated tests can save a lot of

Image creditsOffice work businesshttps://stocksnap.io/photo/HZCASACP3N Top view of phone, earphones pen and diaryhttps://www.pexels.com/photo/top-view-of-phone-earphones-pen-and-diary-6662/

Guy working manhttps://stocksnap.io/photo/GDAGX2JKI4

Red fire truckhttps://stocksnap.io/photo/I2W591P4EV Man wearing black and red checkered long sleeve...https://www.pexels.com/photo/man-wearing-black-and-red-checkered-long-sleeve-shirt-wearing-black-wayfarer-sunglasses-sitting-on-white-wooden-chair-69212/ Man in white shirt using Macbookhttps://www.pexels.com/photo/man-in-white-shirt-using-macbook-pro-52608/

Bow tie fashion man personhttps://www.pexels.com/photo/bow-tie-fashion-man-person-5198/ All above images are licensed under CC0. Thank you very much for making this great work available freely!