53
We write code

Code is read many mor times than written - short

Embed Size (px)

DESCRIPTION

the slides from the 15 minute "Code is read many more times than written" talk I gave at the Rails Girls Berlin workshop on the 15th of February

Citation preview

Page 1: Code is read many mor times than written - short

We write code

Page 2: Code is read many mor times than written - short

Isn't it more aboutreading?

Page 3: Code is read many mor times than written - short

Written once – read many times

Page 4: Code is read many mor times than written - short

„(…) 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 mor times than written - short

Not about Architecture

Page 6: Code is read many mor times than written - short

Methods & Code

Page 7: Code is read many mor times than written - short

Extra effort

Page 8: Code is read many mor times than written - short

Save time

Page 9: Code is read many mor times than written - short

Your code base?

Page 10: Code is read many mor times than written - short
Page 11: Code is read many mor times than written - short

It's about joy!

Page 12: Code is read many mor times than written - short

Code is read many more times than written

Tobias Pfeiffer@PragTob

pragtob.info

Page 13: Code is read many mor times than written - short

Sources

Page 14: Code is read many mor times than written - short

Crazy?

Page 15: Code is read many mor times than written - short

Are comments a smell?

Page 16: Code is read many mor times than written - short

Outdated comments are the worst

Page 17: Code is read many mor times than written - short

The why not the what

Page 18: Code is read many mor times than written - short

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

clearer.

Page 19: Code is read many mor times than written - short

# do one thing………

# do another thing………

# do something more……

Page 20: Code is read many mor times than written - short

Extract Methods

Page 21: Code is read many mor times than written - short

do_one_thingdo_another_thingdo_something_more

Page 22: Code is read many mor times than written - short

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

Page 23: Code is read many mor times than written - short

Explanatory and meaningful names

Page 24: Code is read many mor times than written - short

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

Page 25: Code is read many mor times than written - short

Try to keep it to 2 parameters

Page 26: Code is read many mor times than written - short

Short Methods (<= 8 LOC)

Page 27: Code is read many mor times than written - short

# 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 28: Code is read many mor times than written - short

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 29: Code is read many mor times than written - short

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 30: Code is read many mor times than written - short

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 mor times than written - short

if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customerelse propose_non_alcoholic_drinkend

Page 32: Code is read many mor times than written - short

Nice code formatting

Page 33: Code is read many mor times than written - short

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

Page 34: Code is read many mor times than written - short

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

Page 35: Code is read many mor times than written - short

80 characterWidth limit

Page 36: Code is read many mor times than written - short

80 characterWidth limit

Page 37: Code is read many mor times than written - short

80 characterWidth limit

Page 38: Code is read many mor times than written - short

80 characterWidth limit

Page 39: Code is read many mor times than written - short

80 characterWidth limit

Page 40: Code is read many mor times than written - short

Code bases detoriate

Page 41: Code is read many mor times than written - short

No broken windows

Page 42: Code is read many mor times than written - short
Page 43: Code is read many mor times than written - short
Page 44: Code is read many mor times than written - short

Magical time?

Page 45: Code is read many mor times than written - short

The BoyscoutRule

Page 46: Code is read many mor times than written - short
Page 47: Code is read many mor times than written - short

TDD

Page 48: Code is read many mor times than written - short

Know when tobreak the rules

Page 49: Code is read many mor times than written - short

If you still like your code from two years ago,

then you are not learning fast enough.

Page 50: Code is read many mor times than written - short

Enjoy Coding!

Tobias Pfeiffer@PragTob

pragtob.info

Page 52: Code is read many mor times than written - short

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 53: Code is read many mor times than written - short

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