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

The Elements of Style in Programming (HWSW Mobile! 2016)

The Elements of Style in Programming (HWSW Mobile! 2016)

Slide deck from my presentation at HWSW Mobile! 2016 in Budapest.

Bozhidar Batsov

November 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. Szervusz!

    View Slide

  3. View Slide

  4. Божидар

    View Slide

  5. Bozhidar

    View Slide

  6. Bozho
    cool

    View Slide

  7. Bozo
    not cool

    View Slide

  8. Bug
    cool

    View Slide

  9. View Slide

  10. Magical Sofia City
    Sofia, Bulgaria

    View Slide

  11. Sofia, Bulgaria

    View Slide

  12. View Slide

  13. I’m an Emacs
    fanatic

    View Slide

  14. bbatsov

    View Slide

  15. View Slide

  16. Our
    beloved
    leader/CTO

    View Slide

  17. View Slide

  18. View Slide

  19. View Slide

  20. View Slide

  21. View Slide

  22. Why am I here?

    View Slide

  23. View Slide

  24. 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

  25. Languages are hard

    View Slide

  26. View Slide

  27. View Slide

  28. View Slide

  29. Basic usage of a
    language is easy

    View Slide

  30. “Two parts,
    sometimes lyun,
    sometimes win,
    so,
    first day relyax…”

    View Slide

  31. I need an interpreter!

    View Slide

  32. “You are the BIGGEST!”

    View Slide

  33. The correct usage of
    a language is hard

    View Slide

  34. The effective usage of a
    language is very hard

    View Slide

  35. not really

    View Slide

  36. try 10
    years

    View Slide

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

    View Slide

  38. View Slide

  39. View Slide

  40. View Slide

  41. Use the active voice.

    View Slide

  42. My first visit to
    Budapest will always
    be remembered by me.
    lame

    View Slide

  43. I shall always
    remember my first
    visit to Budapest.
    legit

    View Slide

  44. The word personally
    is often unnecessary.

    View Slide

  45. Personally, I love
    Emacs.

    View Slide

  46. 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

  47. Style guides aren’t a
    replacement for thinking

    View Slide

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

    View Slide

  49. Similar rules apply to
    natural and
    programming languages

    View Slide

  50. View Slide

  51. English

    View Slide

  52. 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

  53. 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

  54. Magyar

    View Slide

  55. View Slide

  56. View Slide

  57. A Fideszt démonizálók közül meglepően sokan
    képzelik úgy, hogy a kor mánypár t valami
    nindzsahadsereghez hasonló, tökéletesen kiképzett,
    arctalan, fegyelmezett katonákból álló sereg, egyfajta
    f é l e l m e t e s e n h a t é k o n y p o l i t i k a i - é s
    propagandagépezet.
    Ugyanezt képzeli ráadásul, nem kis büszkeséggel, egy
    rakás Fidesz-rajongó is. Egy jó propagandaminiszter
    viszont tudja, hogy ez az imidzs egy határig kedvező
    ugyan a pártnak, de ha túlzottan kizárólagossá válik, a
    választók már nem tudnak azonosulni vele és a
    túlhajtott profizásnak meg klónhadseregezésnek
    elhidegülés lesz a vége.
    Fejezet 1

    View Slide

  58. A FIDESZT DéMONIZáLóK KöZüL MEGLEPőEN
    SOKAN KéPZELIK úGY, HOGY A KORMáNYPáRT
    VALAMI NINDZSAHADSEREGHEZ HASONLó,
    T ö K é L E T E S E N K I K é P Z E T T, A R C TA L A N ,
    FEGYELMEZETT KATONáKBóL áLLó SEREG,
    EGYFAJTA FéLELMETESEN HATéKONY POLITIKAI- éS
    PROPAGANDAGéPEZET.
    FeJeZet 1
    U G YA N E Z T K é P Z E L I R á A D á S U L , N E M K I S
    BüSZKESéGGEL, EGY RAKáS FIDESZ-RAJONGó IS. EGY
    Jó PROPAGANDAMINISZTER VISZONT TUDJA, HOGY EZ
    AZ IMIDZS EGY HATáRIG KEDVEZő UGYAN A PáRTNAK,
    DE HA TúLZOTTAN KIZáRóLAGOSSá VáLIK, A VáLASZTóK
    MáR NEM TUDNAK AZONOSULNI VELE éS A
    T ú L H A J T O T T P R O F I Z á S N A K M E G
    KLóNHADSEREGEZéSNEK ELHIDEGüLéS LESZ A VéGE.

    View Slide

  59. Ruby

    View Slide

  60. def some_method
    do_something
    do_more
    do_more
    end

    View Slide

  61. def some_method
    do_something
    do_more
    do_more
    end

    View Slide

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

    View Slide

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

    View Slide

  64. English

    View Slide

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

    View Slide

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

    View Slide

  67. Ruby

    View Slide

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

    View Slide

  69. even?
    completed?
    batman?

    View Slide

  70. English

    View Slide

  71. Make the paragraph
    the unit of
    composition.

    View Slide

  72. Ruby

    View Slide

  73. Make the method the
    unit of composition.

    View Slide

  74. English

    View Slide

  75. Omit needless words.

    View Slide

  76. he is a man who

    View Slide

  77. Her story is a
    strange one.

    View Slide

  78. Ruby

    View Slide

  79. Omit needless words.

    View Slide

  80. if some_condition then
    # body omitted
    end

    View Slide

  81. def some_method
    # ...
    return result
    end

    View Slide

  82. English

    View Slide

  83. farther
    further

    View Slide

  84. Do we have to go
    much farther?

    View Slide

  85. I decided to pursue
    the subject further.

    View Slide

  86. Ruby

    View Slide

  87. ==
    ===
    equal?
    eql?

    View Slide

  88. An approach to
    style

    View Slide

  89. Be consistent

    View Slide

  90. English

    View Slide

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

    View Slide

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

    View Slide

  93. Ruby

    View Slide

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

    View Slide

  95. Be clear

    View Slide

  96. Magyar

    View Slide

  97. MÁV

    View Slide

  98. Megint Állunk Vazze

    View Slide

  99. BKV

    View Slide

  100. Biztos Késünk Vazze

    View Slide

  101. FIDESZ

    View Slide

  102. Forintot Ide De
    SZaporán

    View Slide

  103. Ruby

    View Slide

  104. $:

    View Slide

  105. $LOAD_PATH

    View Slide

  106. $;

    View Slide

  107. $FIELD_SEPARATOR

    View Slide

  108. $*

    View Slide

  109. $ARGV

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  114. Ruby
    is
    optimized
    for
    programmer
    happiness

    View Slide

  115. Ruby is a huge
    language

    View Slide

  116. Making the “right”
    decisions is hard

    View Slide

  117. View Slide

  118. puts "Hello, Budapest!"
    puts "Hello, Budapest!"
    puts "Hello, Budapest!"

    View Slide

  119. for i in 1..3
    puts "Hello, Budapest!"
    end

    View Slide

  120. 3.times do
    puts "Hello, Budapest!"
    end

    View Slide

  121. 3.times do
    puts %(Hello, Budapest!)
    end

    View Slide

  122. 3.times do
    puts %Q(Hello, Budapest!)
    end

    View Slide

  123. 3.times do
    puts 'Hello, Budapest!'
    end

    View Slide

  124. 3.times do
    puts %q(Hello, Budapest!)
    end

    View Slide

  125. View Slide

  126. View Slide

  127. Are all those options
    worth our while?

    View Slide

  128. View Slide

  129. View Slide

  130. View Slide

  131. View Slide

  132. 56 simple rules

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  142. •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

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

    View Slide

  144. Use variable names
    that mean something.

    View Slide

  145. a
    s
    l

    View Slide

  146. age
    sex
    location

    View Slide

  147. age_over_18?

    View Slide

  148. adult?

    View Slide

  149. Don’t repeat
    yourself.

    View Slide

  150. Parenthesize to
    avoid ambiguity.

    View Slide

  151. x || y && z

    View Slide

  152. x || (y && z)

    View Slide

  153. Keep the code & its
    comments in sync.

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  160. # increment counter by 1
    counter += 1

    View Slide

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

    View Slide

  162. //
    // 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

  163. Fuck this
    shit!!!

    View Slide

  164. Style guides
    for
    programming languages

    View Slide

  165. Python

    View Slide

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

    View Slide

  167. Java

    View Slide

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

    View Slide

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

    View Slide

  170. Go

    View Slide

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

    View Slide

  172. gofmt

    View Slide

  173. View Slide

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

    View Slide

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

    View Slide

  176. PHP

    View Slide

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

    View Slide

  178. View Slide

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

    View Slide

  180. Clojure

    View Slide

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

    View Slide

  182. Swift

    View Slide

  183. Swift

    View Slide

  184. Swift Style Guide
    https://github.com/raywenderlich/swift-style-guide

    View Slide

  185. Languages are
    always evolving

    View Slide

  186. Good style practices
    are evolving as well

    View Slide

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

    View Slide

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

    View Slide

  189. Style consistency is of
    paramount importance

    View Slide

  190. Community
    PersonalProject
    Company

    View Slide

  191. Community
    Project
    Company

    View Slide

  192. Style
    separates
    the good
    from
    the great!

    View Slide

  193. Felina

    View Slide

  194. Epilogue
    twitter: @bbatsov
    github: @bbatsov
    http//batsov.com
    http://emacsredux.com
    HWSW Mobile!
    Budapest,
    Hungary
    23.11.2016

    View Slide