code, comments, concepts, comprehension – conclusion?

124
We write code

Upload: tobias-pfeiffer

Post on 29-Jan-2018

1.322 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Code, Comments, Concepts, Comprehension – Conclusion?

We write code

Page 2: Code, Comments, Concepts, Comprehension – Conclusion?

Isn't it moreabout reading?

Page 3: Code, Comments, Concepts, Comprehension – Conclusion?

Written once -read many times

Page 4: Code, Comments, Concepts, Comprehension – Conclusion?

(…) 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, Comments, Concepts, Comprehension – Conclusion?

Any fool can write code that a computer can understand. Good programmers write code that

humans can understand.Martin Fowler

Page 6: Code, Comments, Concepts, Comprehension – Conclusion?

Not about architecture

Page 7: Code, Comments, Concepts, Comprehension – Conclusion?

Methods & Code

Page 8: Code, Comments, Concepts, Comprehension – Conclusion?

Nurturing a code base

Page 9: Code, Comments, Concepts, Comprehension – Conclusion?

Extra effort

Page 10: Code, Comments, Concepts, Comprehension – Conclusion?

Save time!

Page 11: Code, Comments, Concepts, Comprehension – Conclusion?

Your code base?

Page 12: Code, Comments, Concepts, Comprehension – Conclusion?
Page 13: Code, Comments, Concepts, Comprehension – Conclusion?

It's about joy!

Page 14: Code, Comments, Concepts, Comprehension – Conclusion?

Code, Comments, Concepts, Comprehension – Conclusion?

Tobias Pfeiffer@PragTob

pragtob.info

Page 15: Code, Comments, Concepts, Comprehension – Conclusion?

Crazy?

Page 16: Code, Comments, Concepts, Comprehension – Conclusion?

Methods & Code

Page 17: Code, Comments, Concepts, Comprehension – Conclusion?
Page 18: Code, Comments, Concepts, Comprehension – Conclusion?

KeepItSimpleStupid

Page 19: Code, Comments, Concepts, Comprehension – Conclusion?

Are comments a code smell?

Page 20: Code, Comments, Concepts, Comprehension – Conclusion?

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

Page 21: Code, Comments, Concepts, Comprehension – Conclusion?

Outdated comments are the worst

Page 22: Code, Comments, Concepts, Comprehension – Conclusion?

not the

WHYWHAT

Page 23: Code, Comments, Concepts, Comprehension – Conclusion?

def paint_control(event) # some painting coderescue => e # Really important to rescue here. Failures that escape this method # cause odd-ball hangs with no backtraces. See #559 for an example. # puts "SWALLOWED PAINT EXCEPTION ON #{@obj} - go take care of it: " + e.to_s puts 'Unfortunately we have to swallow it because it causes odd failures :('end

WHY comment

Page 24: Code, Comments, Concepts, Comprehension – Conclusion?

Comments are the smell that tries to make other smells seem ok

Page 25: Code, Comments, Concepts, Comprehension – Conclusion?

# do one thing............

# do another thing............

# do something more......

Page 26: Code, Comments, Concepts, Comprehension – Conclusion?

# do one thing............

# do another thing............

# do something more......

Page 27: Code, Comments, Concepts, Comprehension – Conclusion?

# do one thing............

# do another thing............

# do something more......

Cocepts

Page 28: Code, Comments, Concepts, Comprehension – Conclusion?

Method too long

Page 29: Code, Comments, Concepts, Comprehension – Conclusion?

Short Methods

Page 30: Code, Comments, Concepts, Comprehension – Conclusion?

<= 8 LOC

Page 31: Code, Comments, Concepts, Comprehension – Conclusion?

Extract Methods

Page 32: Code, Comments, Concepts, Comprehension – Conclusion?

do_one_thingdo_another_thingdo_something_more

Concepts

Page 34: Code, Comments, Concepts, Comprehension – Conclusion?
Page 35: Code, Comments, Concepts, Comprehension – Conclusion?

"The more complex the code, the more comments it should have."

You should make the code less complex not add more comments.

Tiago Teixeira

Page 36: Code, Comments, Concepts, Comprehension – Conclusion?

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

Page 37: Code, Comments, Concepts, Comprehension – Conclusion?

Incomprehensible names

Page 38: Code, Comments, Concepts, Comprehension – Conclusion?

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

Page 39: Code, Comments, Concepts, Comprehension – Conclusion?

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

Page 40: Code, Comments, Concepts, Comprehension – Conclusion?

Explanatory names

Page 41: Code, Comments, Concepts, Comprehension – Conclusion?

Naming is hard

Page 42: Code, Comments, Concepts, Comprehension – Conclusion?

One Language

Page 43: Code, Comments, Concepts, Comprehension – Conclusion?

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

Page 44: Code, Comments, Concepts, Comprehension – Conclusion?

Argument order dependency

Page 45: Code, Comments, Concepts, Comprehension – Conclusion?

Try to keep it to 2 parameters

Page 46: Code, Comments, Concepts, Comprehension – Conclusion?

Example

Page 47: Code, Comments, Concepts, Comprehension – Conclusion?

# allowed to drink?if customer.age >= 18 say 'Okay' drink = 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 #{['cola', 'mate'].sample}?"end

Page 48: Code, Comments, Concepts, Comprehension – Conclusion?

# allowed to drink?if customer.age >= 18 say 'Okay' drink = 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 #{['cola', 'mate'].sample}?"end

Page 49: Code, Comments, Concepts, Comprehension – Conclusion?

# allowed to drink?if customer.age >= 18 say 'Okay' drink = 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 #{['cola', 'mate'].sample}?"end

Magic „Numbers“

Page 50: Code, Comments, Concepts, Comprehension – Conclusion?

NON_ALCOHOLIC_DRINKS = ['cola', 'mate']MIN_DRINKING_AGE = 18

Page 51: Code, Comments, Concepts, Comprehension – Conclusion?

# allowed to drink?if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = 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 52: Code, Comments, Concepts, Comprehension – Conclusion?

# allowed to drink?if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = 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 53: Code, Comments, Concepts, Comprehension – Conclusion?

# allowed to drink?if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = 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 54: Code, Comments, Concepts, Comprehension – Conclusion?

Query method

Page 55: Code, Comments, Concepts, Comprehension – Conclusion?

Intention revealing method

Page 56: Code, Comments, Concepts, Comprehension – Conclusion?

# ...text.color = red# ...

Page 57: Code, Comments, Concepts, Comprehension – Conclusion?

# ...text.color = red# ...

Page 58: Code, Comments, Concepts, Comprehension – Conclusion?

# ...highlight(text)# ...

Page 59: Code, Comments, Concepts, Comprehension – Conclusion?

def highlight(text) text.color = redend

Page 60: Code, Comments, Concepts, Comprehension – Conclusion?

def highlight(text) text.color = red text.underline = true update_highlightsend

Page 61: Code, Comments, Concepts, Comprehension – Conclusion?

# ...text.color = redtext.underline = trueupdate_highlights# ...

Page 62: Code, Comments, Concepts, Comprehension – Conclusion?

# ...highlight(text)# ...

Page 63: Code, Comments, Concepts, Comprehension – Conclusion?

# allowed to drink?if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = 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 64: Code, Comments, Concepts, Comprehension – Conclusion?

# allowed to drink?if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = 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 65: Code, Comments, Concepts, Comprehension – Conclusion?

# allowed to drink?if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = 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 66: Code, Comments, Concepts, Comprehension – Conclusion?

# allowed to drink?if customer.age >= MIN_DRINKING_AGE say 'Okay' drink = 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 67: Code, Comments, Concepts, Comprehension – Conclusion?

if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customerelse propose_non_alcoholic_drinkend

Page 68: Code, Comments, Concepts, Comprehension – Conclusion?

„If you have a good name for a method you don't need to look at

the body.“Martin Fowler

Page 69: Code, Comments, Concepts, Comprehension – Conclusion?

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

all.“Tom Stuart (Berlin)

Page 70: Code, Comments, Concepts, Comprehension – Conclusion?

def serve_alcoholic_drink(customer, requested_drink) if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink endend

# more public methods

privatedef allowed_to_drink_alcohol?(customer)end

def serve_drink(requested_drink, customer)end

def propose_non_alcoholic_drinkend

Method Order

Page 71: Code, Comments, Concepts, Comprehension – Conclusion?

def serve_alcoholic_drink(customer, requested_drink) if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink endend

# more public methods

privatedef allowed_to_drink_alcohol?(customer)end

def serve_drink(requested_drink, customer)end

def propose_non_alcoholic_drinkend

Page 72: Code, Comments, Concepts, Comprehension – Conclusion?

Method Order

def serve_alcoholic_drink(customer, requested_drink) if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink endend

defp allowed_to_drink_alcohol?(customer)end

defp serve_drink(requested_drink, customer)end

defp propose_non_alcoholic_drinkend

# more public methods

Page 73: Code, Comments, Concepts, Comprehension – Conclusion?

def serve_alcoholic_drink(customer, requested_drink) if allowed_to_drink_alcohol?(customer) serve_drink requested_drink, customer else propose_non_alcoholic_drink endend

defp allowed_to_drink_alcohol?(customer)end

defp serve_drink(requested_drink, customer)end

defp propose_non_alcoholic_drinkend

# more public methods

Page 74: Code, Comments, Concepts, Comprehension – Conclusion?

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

Page 75: Code, Comments, Concepts, Comprehension – Conclusion?

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

Page 76: Code, Comments, Concepts, Comprehension – Conclusion?

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

Page 77: Code, Comments, Concepts, Comprehension – Conclusion?

Same level of abstraction in a method

Page 78: Code, Comments, Concepts, Comprehension – Conclusion?

prepare_drink requested_drinkprepare_check requested_drink

Page 79: Code, Comments, Concepts, Comprehension – Conclusion?

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

Code Formatting

Page 80: Code, Comments, Concepts, Comprehension – Conclusion?

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

Code Formatting

Page 81: Code, Comments, Concepts, Comprehension – Conclusion?

80 character width limit

Page 82: Code, Comments, Concepts, Comprehension – Conclusion?

80 character width limit

Page 83: Code, Comments, Concepts, Comprehension – Conclusion?

80 character width limit

Page 84: Code, Comments, Concepts, Comprehension – Conclusion?

80 character width limit

Page 85: Code, Comments, Concepts, Comprehension – Conclusion?

80 character width limit

Page 86: Code, Comments, Concepts, Comprehension – Conclusion?

Identify concepts

Page 87: Code, Comments, Concepts, Comprehension – Conclusion?

Don'tRepeatYourself

Page 88: Code, Comments, Concepts, Comprehension – Conclusion?

Don'tRepeatYourself(In Concepts)

Page 89: Code, Comments, Concepts, Comprehension – Conclusion?

Virtues

Page 90: Code, Comments, Concepts, Comprehension – Conclusion?

Simplicity

Page 91: Code, Comments, Concepts, Comprehension – Conclusion?

The least powerful construct

Page 92: Code, Comments, Concepts, Comprehension – Conclusion?

Explicit vs Implicit

Page 93: Code, Comments, Concepts, Comprehension – Conclusion?

person = Person.new(attributes)do_something(person)insert_in_db(person)

Immutable Data

Page 94: Code, Comments, Concepts, Comprehension – Conclusion?

person = Person.new(attributes)person = do_something(person)insert_in_db(person)

Immutable Data

Page 95: Code, Comments, Concepts, Comprehension – Conclusion?

Transformation of data

Page 96: Code, Comments, Concepts, Comprehension – Conclusion?

Reads like a book

Page 97: Code, Comments, Concepts, Comprehension – Conclusion?

Nurturing a code base

Page 98: Code, Comments, Concepts, Comprehension – Conclusion?

Code bases detoriate

Page 99: Code, Comments, Concepts, Comprehension – Conclusion?

No broken windows!

Page 100: Code, Comments, Concepts, Comprehension – Conclusion?
Page 101: Code, Comments, Concepts, Comprehension – Conclusion?
Page 102: Code, Comments, Concepts, Comprehension – Conclusion?
Page 103: Code, Comments, Concepts, Comprehension – Conclusion?
Page 104: Code, Comments, Concepts, Comprehension – Conclusion?

Magical time?

Page 105: Code, Comments, Concepts, Comprehension – Conclusion?

The boyscout rule

Page 106: Code, Comments, Concepts, Comprehension – Conclusion?

Opportunistic Refactoring

Page 107: Code, Comments, Concepts, Comprehension – Conclusion?

Confident Tests

Page 108: Code, Comments, Concepts, Comprehension – Conclusion?

80% Code Coverage

Page 109: Code, Comments, Concepts, Comprehension – Conclusion?

20% never executed

Page 110: Code, Comments, Concepts, Comprehension – Conclusion?

Code Review Culture

Page 111: Code, Comments, Concepts, Comprehension – Conclusion?

Always the code, never the person

Page 112: Code, Comments, Concepts, Comprehension – Conclusion?

Always the code, never the person

Say something positive

Page 113: Code, Comments, Concepts, Comprehension – Conclusion?

Always the code, never the person

Say something positive

Use automated linters

Page 114: Code, Comments, Concepts, Comprehension – Conclusion?

Always the code, never the person

Say something positive

Use automated lintersUse automated linters

Everyone reviews

Page 115: Code, Comments, Concepts, Comprehension – Conclusion?

Always the code, never the person

Say something positive

Use automated lintersUse automated linters

Everyone reviews

Pairing Review

Page 116: Code, Comments, Concepts, Comprehension – Conclusion?

Pair Programming

Page 117: Code, Comments, Concepts, Comprehension – Conclusion?

The power of magic hats

Page 118: Code, Comments, Concepts, Comprehension – Conclusion?

Reaping the benefits

Page 119: Code, Comments, Concepts, Comprehension – Conclusion?

When to break the rules

Page 120: Code, Comments, Concepts, Comprehension – Conclusion?

If you still like your code from two years ago,

then you are not learning fast enough.

Page 121: Code, Comments, Concepts, Comprehension – Conclusion?

Enjoy writing readable code!

Tobias Pfeiffer@PragTob

pragtob.info

Page 122: Code, Comments, Concepts, Comprehension – Conclusion?

RUG::B

benchmarking

Open Source

Testing

Ruby Implementations

Page 124: Code, Comments, Concepts, Comprehension – Conclusion?

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

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

● http://www.osnews.com/story/19266/WTFs_m

● (CC BY-SA 2.0)

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

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

– https://www.flickr.com/photos/93425126@N00/313056379/

● (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/

– https://www.flickr.com/photos/eyewash/2603717864/

– https://www.flickr.com/photos/stevie_gill/3950697539/

– https://www.flickr.com/photos/randar/15787696685/

● 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/

– https://www.flickr.com/photos/garryknight/5650367750/

– https://www.flickr.com/photos/alper/10742816123/

● (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/

– https://www.flickr.com/photos/denverjeffrey/4392418334/

● (CC BY-NC 2.0)

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

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

– https://www.flickr.com/photos/whitecatsg/3146092196/