$30 off During Our Annual Pro Sale. View Details »

The Elements of Style in Programming (WebCamp Zagreb 2015)

The Elements of Style in Programming (WebCamp Zagreb 2015)

Slide deck from my talk at WebCamp Zagreb 2015.

Bozhidar Batsov

March 23, 2016
Tweet

More Decks by Bozhidar Batsov

Other Decks in Programming

Transcript

  1. The Elements
    of
    Style
    in
    Programming
    by Bozhidar Batsov

    View Slide

  2. Dobar dan Zagrebe!

    View Slide

  3. Божидар

    View Slide

  4. Božidar

    View Slide

  5. View Slide

  6. View Slide

  7. I’m an Emacs
    fanatic

    View Slide

  8. bbatsov

    View Slide

  9. View Slide

  10. Sasha,
    our
    beloved
    leader/CTO

    View Slide

  11. View Slide

  12. @bbatsov

    View Slide

  13. View Slide

  14. Why am I here?

    View Slide

  15. View Slide

  16. size=2
    start=0
    c=['Н', 'Н' ,'Н' ,'Н' ,'Н','Н','Н','Н','Н','Н','Н','Н','Н','Н']
    code_dictionary=Array.new()
    begin_size_code_dictionary=Array.new()
    end_string=String.new()
    code_dictionary=c.uniq.to_a
    decode_dictionary=c.uniq.to_a
    for i in 0...code_dictionary.size
    begin_size_code_dictionary[i]=i
    end
    c.size.times{
    if !code_dictionary.include?(c[start,size].to_s)#Comparation
    code_dictionary.push(c[start,size].to_s.chomp)
    start=start+(code_dictionary.last.size)/2-1
    size=2
    else
    size+=1;
    end
    }
    end_code=Array.new(code_dictionary.size)
    for i in begin_size_code_dictionary.size...code_dictionary.size
    end_code[i]=code_dictionary.index(code_dictionary[i].chop.chop)+1
    end_string+=code_dictionary[i].chop.chop
    end
    govnokod.ru

    View Slide

  17. Languages are hard

    View Slide

  18. View Slide

  19. Basic usage of a
    language is easy

    View Slide

  20. Pipl mast trast as

    View Slide

  21. "I was day after 

    yesterday

    in Hungary"

    View Slide

  22. The correct usage of
    a language is hard

    View Slide

  23. The effective usage of
    a language is very hard

    View Slide

  24. not really

    View Slide

  25. try 10 years

    View Slide

  26. http://norvig.com/21-days.html
    Teach Yourself
    Programming in Ten Years
    Peter Norvig

    View Slide

  27. View Slide

  28. View Slide

  29. View Slide

  30. Use the active voice.

    View Slide

  31. My first visit to Zagreb will
    always be remembered by me.
    lame

    View Slide

  32. I shall always remember my
    first visit to Zagreb.
    legit

    View Slide

  33. The word personally
    is often unnecessary.

    View Slide

  34. Personally, I love Emacs.

    View Slide

  35. A style guide
    • a curated set of (supposedly) good practices
    • spares you the process of making trivial
    decisions
    • allows you to focus on the actual problems
    • simple and concise

    View Slide

  36. Style guides aren’t a
    replacement for thinking

    View Slide

  37. Programs must be
    written for people to
    read, and only
    incidentally for
    machines to execute.
    — Hal Abelson

    View Slide

  38. Similar rules apply to
    natural and programming
    languages

    View Slide

  39. View Slide

  40. English

    View Slide

  41. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
    veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
    commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
    velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
    cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
    est laborum.
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
    veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
    commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
    velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
    cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
    est laborum.
    Chapter I

    View Slide

  42. LOREM IPSUM DOLOR SIT AMET, CONSECTETUR ADIPISCING ELIT, SED DO
    EIUSMOD TEMPOR INCIDIDUNT UT LABORE ET DOLORE MAGNA ALIQUA. UT
    ENIM AD MINIM VENIAM, QUIS NOSTRUD EXERCITATION ULLAMCO LABORIS
    NISI UT ALIQUIP EX EA COMMODO CONSEQUAT. DUIS AUTE IRURE DOLOR
    IN REPREHENDERIT IN VOLUPTATE VELIT ESSE CILLUM DOLORE EU FUGIAT
    NULLA PARIATUR. EXCEPTEUR SINT OCCAECAT CUPIDATAT NON PROIDENT,
    SUNT IN CULPA QUI OFFICIA DESERUNT MOLLIT ANIM ID EST LABORUM.
    LOREM IPSUM DOLOR SIT AMET, CONSECTETUR
    ADIPISCING ELIT, SED DO EIUSMOD TEMPOR INCIDIDUNT UT
    LABORE ET DOLORE MAGNA ALIQUA. UT ENIM AD MINIM
    VENIAM, QUIS NOSTRUD EXERCITATION ULLAMCO LABORIS
    NISI UT ALIQUIP EX EA COMMODO CONSEQUAT. DUIS AUTE
    IRURE DOLOR IN REPREHENDERIT IN VOLUPTATE VELIT
    ESSE CILLUM DOLORE EU FUGIAT NULLA PARIATUR.
    EXCEPTEUR SINT OCCAECAT CUPIDATAT NON PROIDENT,
    SUNT IN CULPA QUI OFFICIA DESERUNT MOLLIT ANIM ID
    EST LABORUM.
    chapter i

    View Slide

  43. Hrvatski

    View Slide

  44. Trebaju nam promjene prema otkrivanju i održavanju perspektive razvoja i
    napretka. Naša koalicija najavljuje promjene – promjene koje će oživjeti
    gospodarstvo kao podlogu razvitka, koje će teret reformi ravnomjernije
    podijeliti između bogatih i siromašnih, koje će omogućiti odgovornu,
    funkcionalnu i poštenu upravu, koje će racionalizirati potrošnju i javni rashod,
    a osigurati standard građana, koje će uvesti pravo zajednice na dio dobiti
    koju pojedinci izvlače ekstraprofitom, pretvarajući tuđi rad u svoju imovinu.

    Mi najavljujemo promjenu prirode hrvatskoga kapitalizma i konačni odmak
    od prakse kapitalizma perifernoga, nekontroliranog, pa i nezakonitog profita,
    bogaćenja na štetu nacionalnih interesa i podcjenjivanja i odbacivanja prava
    rada i radnika. Naše promjene ne vode prema slabljenju države, nego prema
    jačanju uloge odgovorne države, a istodobno jačanju uloge njezinih građana.
    Poglavlje 1

    View Slide

  45. TREBAJU NAM PROMJENE PREMA OTKRIVANJU I ODRŽAVANJU
    PERSPEKTIVE RAZVOJA I NAPRETKA. NAŠA KOALICIJA NAJAVLJUJE
    PROMJENE – PROMJENE KOJE ĆE OŽIVJETI GOSPODARSTVO KAO
    PODLOGU RAZVITKA, KOJE ĆE TERET REFORMI RAVNOMJERNIJE
    PODIJELITI IZMEĐU BOGATIH I SIROMAŠNIH, KOJE ĆE OMOGUĆITI
    ODGOVORNU, FUNKCIONALNU I POŠTENU UPRAVU, KOJE ĆE
    RACIONALIZIRATI POTROŠNJU I JAVNI RASHOD, A OSIGURATI STANDARD
    GRAĐANA, KOJE ĆE UVESTI PRAVO ZAJEDNICE NA DIO DOBITI KOJU
    POJEDINCI IZVLAČE EKSTRAPROFITOM, PRETVARAJUĆI TUĐI RAD U
    SVOJU IMOVINU.
    pOgLaVljE 1
    MI NAJAVLJUJEMO PROMJENU PRIRODE HRVATSKOGA
    KAPITALIZMA I KONAČNI ODMAK OD PRAKSE KAPITALIZMA
    PERIFERNOGA, NEKONTROLIRANOG, PA I NEZAKONITOG PROFITA,
    B O G A Ć E N J A N A Š T E T U N A C I O N A L N I H I N T E R E S A I
    PODCJENJIVANJA I ODBACIVANJA PRAVA RADA I RADNIKA.
    SADAŠNJA VLAST SAMO JE ZAŠTITNIK I JAMAC NASTAVKA TAKVE
    PRAKSE. NAŠE PROMJENE NE VODE PREMA SLABLJENJU DRŽAVE,
    NEGO PREMA JAČANJU ULOGE ODGOVORNE DRŽAVE

    View Slide

  46. Ruby

    View Slide

  47. def some_method
    do_something
    do_more
    do_more
    end

    View Slide

  48. def some_method
    do_something
    do_more
    do_more
    end

    View Slide

  49. sum = 1 + 2
    a, b = 1, 2
    [1, 2, 3].each { |e| puts e }
    class FooError < StandardError; end

    View Slide

  50. sum=1+2
    a,b=1,2
    [1,2,3].each{|e|puts e}
    class FooError

    View Slide

  51. English

    View Slide

  52. Form possessive
    singular of nouns by
    adding ’s.

    View Slide

  53. Bozhidar’s talk
    Batman’s batclaw
    Emacs’s magic

    View Slide

  54. Ruby

    View Slide

  55. Form predicate method
    names by adding “?”.

    View Slide

  56. even?
    completed?
    batman?

    View Slide

  57. English

    View Slide

  58. Make the paragraph
    the unit of composition.

    View Slide

  59. Ruby

    View Slide

  60. Make the method the
    unit of composition.

    View Slide

  61. English

    View Slide

  62. Omit needless words.

    View Slide

  63. he is a man who

    View Slide

  64. Her story is a strange
    one.

    View Slide

  65. Ruby

    View Slide

  66. Omit needless words.

    View Slide

  67. if some_condition then
    # body omitted
    end

    View Slide

  68. def some_method
    # ...
    return result
    end

    View Slide

  69. English

    View Slide

  70. farther
    further

    View Slide

  71. Do we have to go
    much farther?

    View Slide

  72. I decided to pursue
    the subject further.

    View Slide

  73. Ruby

    View Slide

  74. ==
    ===
    equal?
    eql?

    View Slide

  75. An approach to
    style

    View Slide

  76. Be consistent

    View Slide

  77. English

    View Slide

  78. The user should update
    his config accordingly.
    They should also check…

    View Slide

  79. The user should update
    his config accordingly.
    They should also check…

    View Slide

  80. Ruby

    View Slide

  81. def something coll
    coll.collect(&:ala)
    end
    def something2(coll)
    coll.map { |x| x.bala }
    end

    View Slide

  82. Be clear

    View Slide

  83. Hrvatski

    View Slide

  84. HP

    View Slide

  85. Hladno Pivo

    View Slide

  86. DNK

    View Slide

  87. Da
    Nije
    Komšijin

    View Slide

  88. USKOK

    View Slide

  89. Ured za suzbijanje korupcije
    i organiziranog kriminaliteta

    View Slide

  90. Ruby

    View Slide

  91. $:

    View Slide

  92. $LOAD_PATH

    View Slide

  93. $;

    View Slide

  94. $FIELD_SEPARATOR

    View Slide

  95. $*

    View Slide

  96. $ARGV

    View Slide

  97. '%d %d' % [20, 10]

    View Slide

  98. sprintf('%d %d', 20, 10)

    View Slide

  99. sprintf(
    '%{first} %{second}',
    first: 20, second: 10
    )

    View Slide

  100. format('%{first} %{second}',
    first: 20, second: 10)

    View Slide

  101. Ruby
    is
    optimized
    for
    programmer
    happiness

    View Slide

  102. Ruby is a huge
    language

    View Slide

  103. Making the “right”
    decisions is hard

    View Slide

  104. puts "Hello, WebCamp!"
    puts "Hello, WebCamp!"
    puts "Hello, WebCamp!"

    View Slide

  105. for i in 1..3
    puts "Hello, WebCamp!"
    end

    View Slide

  106. 3.times do
    puts "Hello, WebCamp!"
    end

    View Slide

  107. result = []
    names.each do |word|
    result << name.upcase
    end

    View Slide

  108. names.map { |name| name.upcase }

    View Slide

  109. names.map(&:upcase)

    View Slide

  110. View Slide

  111. View Slide

  112. View Slide

  113. 56 simple rules

    View Slide

  114. Format a program
    to help the reader
    understand it.

    View Slide

  115. Write clearly --
    don't be too clever.

    View Slide

  116. def user_signed_in?
    !!@current_user
    end
    def user_signed_in?
    !@current_user.nil?
    end

    View Slide

  117. Don't sacrifice
    clarity for small
    gains in efficiency.

    View Slide

  118. i = 1
    res = 0
    while i <= 10
    res += i
    i += 1
    end

    View Slide

  119. (1..10).reduce(:+)

    View Slide

  120. (N * (N + 1)) / 2

    View Slide

  121. (10 * (10 + 1)) / 2

    View Slide

  122. Don't diddle code to
    make it faster -- find a
    better algorithm.

    View Slide

  123. •Make it right before you make
    it faster.
    •Make it fail-safe before you
    make it faster.
    •Make it clear before you make
    it faster.

    View Slide

  124. Instrument your
    programs. Measure
    before making efficiency
    changes.

    View Slide

  125. Use variable names
    that mean something.

    View Slide

  126. a
    s
    l

    View Slide

  127. age
    sex
    location

    View Slide

  128. age_over_18?

    View Slide

  129. adult?

    View Slide

  130. Don’t repeat
    yourself.

    View Slide

  131. Parenthesize to
    avoid ambiguity.

    View Slide

  132. x || y && z

    View Slide

  133. x || (y && z)

    View Slide

  134. Keep the code & its
    comments in sync.

    View Slide

  135. # This method does bla bla bla.
    # It returns an array of results.
    def bla_bla_bla
    res = Set.new
    # ...
    res
    end

    View Slide

  136. bla_bla_bla[0]
    # => Boom Boom Pow
    bla_bla_bla.uniq
    # => Kaboom!

    View Slide

  137. //private instance variable for storing age
    public static int age;

    View Slide

  138. //private instance variable for storing age
    public static int age;

    View Slide

  139. //private instance variable for storing age
    public static int age;

    View Slide

  140. Don’t repeat the code
    in the comments.

    View Slide

  141. # increment counter by 1
    counter += 1

    View Slide

  142. Don’t comment
    shitty code — fix it.

    View Slide

  143. //
    // Dear maintainer:
    //
    // Once you are done trying to 'optimize' this routine,
    // and have realized what a terrible mistake that was,
    // please increment the following counter as a warning
    // to the next guy:
    //
    // total_hours_wasted_here = 42
    //

    View Slide

  144. Fuck this
    shit!!!

    View Slide

  145. Style guides
    for
    programming languages

    View Slide

  146. Python

    View Slide

  147. PEP-8
    http://legacy.python.org/dev/peps/pep-0008/

    View Slide

  148. Java

    View Slide

  149. Sun Style Guide
    http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

    View Slide

  150. Google Style Guide
    https://google-styleguide.googlecode.com/svn/trunk/javaguide.html

    View Slide

  151. Go

    View Slide

  152. Effective Go
    https://golang.org/doc/effective_go.html

    View Slide

  153. gofmt

    View Slide

  154. View Slide

  155. Airbnb Style Guide
    https://github.com/airbnb/javascript

    View Slide

  156. Google Style Guide
    https://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml

    View Slide

  157. View Slide

  158. PHP FIG Coding Style
    Guide
    http://www.php-fig.org/psr/psr-1/
    http://www.php-fig.org/psr/psr-2/

    View Slide

  159. View Slide

  160. Ruby Style Guide
    https://github.com/bbatsov/ruby-style-guide

    View Slide

  161. View Slide

  162. Clojure Style Guide
    https://github.com/bbatsov/clojure-style-guide

    View Slide

  163. Languages are always
    evolving

    View Slide

  164. Good style practices
    are evolving as well

    View Slide

  165. { :batman => 'Bruce',
    :superman => 'Clark',
    :zorro => 'Diego',
    :catwoman => 'Selina' }

    View Slide

  166. { batman: 'Bruce',
    superman: 'Clark',
    zorro: 'Diego',
    catwoman: 'Selina' }

    View Slide

  167. Style consistency is of
    paramount importance

    View Slide

  168. Community
    Personal Project
    Company

    View Slide

  169. Community
    Project
    Company

    View Slide

  170. Style separates
    the good from the
    great

    View Slide

  171. Felina

    View Slide

  172. One more thing…

    View Slide

  173. View Slide