code is read many more times than written

74
We write code

Upload: tobias-pfeiffer

Post on 14-Jan-2015

271 views

Category:

Technology


0 download

DESCRIPTION

"Code is read many more times than written." - this statement changed my view of Software Engineering. From that point on I made it my personal mission to write the best code I could possibly write. This talk is about my very favorite coding wisdoms that I picked up on my journey so far. All of these will help you to write better code, be more productive and have more fun. And good code pays it's interest rate every day. Also if you're a clean code junkie looking for a fix - this talk is for you.

TRANSCRIPT

Page 1: Code is read many more times than written

We write code

Page 2: Code is read many more times than written

Isn't it more aboutreading?

Page 3: Code is read many more times than written

Written once – read many times

Page 4: Code is read many more times than written

„(…) when you program, you have to think about how someone will read your code, not just how a computer will interpret it.“Kent Beck

Page 5: Code is read many more times than written

Not about Architecture

Page 6: Code is read many more times than written

Methods & Code

Page 7: Code is read many more times than written

Extra effort

Page 8: Code is read many more times than written

Save time

Page 9: Code is read many more times than written

Your code base?

Page 10: Code is read many more times than written
Page 11: Code is read many more times than written

It's about joy!

Page 12: Code is read many more times than written

Code is read many more times than written

Tobias Pfeiffer@PragTob

pragtob.wordpress.com

Page 13: Code is read many more times than written

Sources

Page 14: Code is read many more times than written

Crazy?

Page 15: Code is read many more times than written

Are comments a smell?

Page 16: Code is read many more times than written

Outdated comments are the worst

Page 17: Code is read many more times than written

The why not the what

Page 18: Code is read many more times than written

Comments are an excuse of the code that it could not be

clearer.

Page 19: Code is read many more times than written

# do one thing………

# do another thing………

# do something more……

Page 20: Code is read many more times than written

Extract Methods

Page 21: Code is read many more times than written

do_one_thingdo_another_thingdo_something_more

Page 22: Code is read many more times than written

# context, outlet, times, time per step, state, datadef pattern(c, o, t, l, s, d) # …end

Page 23: Code is read many more times than written

Explanatory and meaningful names

Page 24: Code is read many more times than written

def pattern(context, outlet, time, time_per_step, state, data) # …end

Page 25: Code is read many more times than written

Try to keep it to 2 parameters

Page 26: Code is read many more times than written

Argument order dependency

Page 27: Code is read many more times than written

No magic numbers

Page 28: Code is read many more times than written

Explanatory variable

Page 29: Code is read many more times than written

Short Methods (<= 8 LOC)

Page 30: Code is read many more times than written

# allowed to drink?if customer.age > 18 say 'Okay' prepare_drink requested_drink say 'here you go' hand_drink_over drink, customerelse say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?"end

Page 31: Code is read many more times than written

# allowed to drink?if customer.age > 18 say 'Okay' prepare_drink requested_drink say 'here you go' hand_drink_over drink, customerelse say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?"end

Page 32: Code is read many more times than written

Query method

Page 33: Code is read many more times than written

Intention revealing method

Page 34: Code is read many more times than written

# …text.color = red# …

Page 35: Code is read many more times than written

def highlight(text) text.color = redend

Page 36: Code is read many more times than written

# …highlight(text)# …

Page 37: Code is read many more times than written

if customer.age > 18 say 'Okay' prepare_drink requested_drink say 'here you go' hand_drink_over drink, customerelse say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?"end

Page 38: Code is read many more times than written

if customer.age > 18 say 'Okay' prepare_drink requested_drink say 'here you go' hand_drink_over drink, customerelse say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?"end

Page 39: Code is read many more times than written

if customer.age > 18 say 'Okay' prepare_drink requested_drink say 'here you go' hand_drink_over drink, customerelse say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?"end

Page 40: Code is read many more times than written

if customer.age > 18 say 'Okay' prepare_drink requested_drink say 'here you go' hand_drink_over drink, customerelse say 'I am sorry you are not legally allowed rather to drink here' say "Would you rather have a #{NON_ALCOHOLIC_DRINKS.sample}?"end

Page 41: Code is read many more times than written

if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customerelse propose_non_alcoholic_drinkend

Page 42: Code is read many more times than written

„The easiest code to understand is the code you don't have to read at all.“

Tom Stuart (Berlin)

Page 43: Code is read many more times than written

Same level of abstraction in a method

Page 44: Code is read many more times than written

prepare_drink requested_drinkprice = requested_drink.priceCheck = Check.newcheck.add_price pricesay 'That whill be ' + check.total

Page 45: Code is read many more times than written

prepare_drink requested_drinkprice = requested_drink.priceCheck = Check.newcheck.add_price pricesay 'That whill be ' + check.total

Page 46: Code is read many more times than written

prepare_drink requested_drinkprice = requested_drink.priceCheck = Check.newcheck.add_price pricesay 'That whill be ' + check.total

Page 47: Code is read many more times than written

prepare_drink requested_drinkprepare_check requested_drink

Page 48: Code is read many more times than written

DRY

Page 49: Code is read many more times than written

Nice code formatting

Page 50: Code is read many more times than written

@left ||= 0 @top ||= 0 @width ||= 1.0 @height ||= 0

Page 51: Code is read many more times than written

double character: 'something weird', stateMask: CTRL | modifier, KeyCode: character.downcase.ord

Page 52: Code is read many more times than written

80 characterWidth limit

Page 53: Code is read many more times than written

80 characterWidth limit

Page 54: Code is read many more times than written

80 characterWidth limit

Page 55: Code is read many more times than written

80 characterWidth limit

Page 56: Code is read many more times than written

80 characterWidth limit

Page 57: Code is read many more times than written

Don't program by coincedence

Page 58: Code is read many more times than written

Code bases detoriate

Page 59: Code is read many more times than written

No broken windows

Page 60: Code is read many more times than written
Page 61: Code is read many more times than written
Page 62: Code is read many more times than written

Magical time?

Page 63: Code is read many more times than written

The BoyscoutRule

Page 64: Code is read many more times than written
Page 65: Code is read many more times than written

TDD

Page 66: Code is read many more times than written

80% Code Coverage

Page 67: Code is read many more times than written

20% is never executed

Page 68: Code is read many more times than written

„Incoming messages should be tested for the state they return. Outgoing command messages should be tested to ensure they get sent. Outgoing query messages should not be tested.“Sandi Metz

Page 69: Code is read many more times than written

Know when tobreak the rules

Page 70: Code is read many more times than written

If you still like your code from two years ago,

then you are not learning fast enough.

Page 71: Code is read many more times than written

Enjoy Coding!

Tobias Pfeiffer@PragTob

pragtob.wordpress.com

Page 73: Code is read many more times than written

Photo Credit● http://officeimg.vo.msecnd.net/en-us/images/MP900439313.jpg

● http://officeimg.vo.msecnd.net/en-us/images/MC900021328.wmf

● (CC BY-SA 2.0)

– http://www.flickr.com/photos/83633410@N07/7658272558/in/photostream/

– http://www.flickr.com/photos/83633410@N07/7658165122/

– http://en.wikipedia.org/wiki/File:Kent_Beck_no_Workshop_Mapping_XP.jpg

● (CC BY-NC-ND 2.0)

– http://www.flickr.com/photos/andih/86577529/

– http://www.flickr.com/photos/12584908@N08/3293117576/

– http://www.flickr.com/photos/jasonlparks/4525188865/

– http://www.flickr.com/photos/20714221@N04/2293045156/

● http://www.flickr.com/photos/47833351@N02/5488791911/(CC BY-ND 2.0)

● (CC BY 2.0)

– http://www.flickr.com/photos/barry_b/76055201/

– http://www.flickr.com/photos/25165196@N08/7725273678/

– http://www.flickr.com/photos/29254399@N08/3187186308/

● (CC BY-NC-SA 2.0)

– http://www.flickr.com/photos/dolescum/7380616658/

– http://www.flickr.com/photos/antonkovalyov/5795281215/

– http://www.flickr.com/photos/doug88888/2792209612/

● (CC BY-NC 2.0)

– http://www.flickr.com/photos/37996583811@N01/5757983532/

– http://www.flickr.com/photos/sevendead/5650065458/

Page 74: Code is read many more times than written

Source pictures● http://imagery.pragprog.com/products/59/tpp.jpg

● http://www.informit.com/ShowCover.aspx?isbn=9780132852111&type=f

● http://coding-in.net/blog/wp-content/uploads/clean_code.jpg

● http://www.informit.com/ShowCover.aspx?isbn=9780321721334&type=f

● http://mendicantuniversity.org/images/logo.png