Slide 1

Slide 1 text

The Elements of Style in Programming by Bozhidar Batsov

Slide 2

Slide 2 text

Szervusz!

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Божидар

Slide 5

Slide 5 text

Bozhidar

Slide 6

Slide 6 text

Bozho cool

Slide 7

Slide 7 text

Bozo not cool

Slide 8

Slide 8 text

Bug cool

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Magical Sofia City Sofia, Bulgaria

Slide 11

Slide 11 text

Sofia, Bulgaria

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

I’m an Emacs fanatic

Slide 14

Slide 14 text

bbatsov

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Our beloved leader/CTO

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Why am I here?

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Languages are hard

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Basic usage of a language is easy

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

I need an interpreter!

Slide 32

Slide 32 text

“You are the BIGGEST!”

Slide 33

Slide 33 text

The correct usage of a language is hard

Slide 34

Slide 34 text

The effective usage of a language is very hard

Slide 35

Slide 35 text

not really

Slide 36

Slide 36 text

try 10 years

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Use the active voice.

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

I shall always remember my first visit to Budapest. legit

Slide 44

Slide 44 text

The word personally is often unnecessary.

Slide 45

Slide 45 text

Personally, I love Emacs.

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Style guides aren’t a replacement for thinking

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Similar rules apply to natural and programming languages

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

English

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

Magyar

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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.

Slide 59

Slide 59 text

Ruby

Slide 60

Slide 60 text

def some_method do_something do_more do_more end

Slide 61

Slide 61 text

def some_method do_something do_more do_more end

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

English

Slide 65

Slide 65 text

Form possessive singular of nouns by adding ’s.

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

Ruby

Slide 68

Slide 68 text

Form predicate method names by adding “?”.

Slide 69

Slide 69 text

even? completed? batman?

Slide 70

Slide 70 text

English

Slide 71

Slide 71 text

Make the paragraph the unit of composition.

Slide 72

Slide 72 text

Ruby

Slide 73

Slide 73 text

Make the method the unit of composition.

Slide 74

Slide 74 text

English

Slide 75

Slide 75 text

Omit needless words.

Slide 76

Slide 76 text

he is a man who

Slide 77

Slide 77 text

Her story is a strange one.

Slide 78

Slide 78 text

Ruby

Slide 79

Slide 79 text

Omit needless words.

Slide 80

Slide 80 text

if some_condition then # body omitted end

Slide 81

Slide 81 text

def some_method # ... return result end

Slide 82

Slide 82 text

English

Slide 83

Slide 83 text

farther further

Slide 84

Slide 84 text

Do we have to go much farther?

Slide 85

Slide 85 text

I decided to pursue the subject further.

Slide 86

Slide 86 text

Ruby

Slide 87

Slide 87 text

== === equal? eql?

Slide 88

Slide 88 text

An approach to style

Slide 89

Slide 89 text

Be consistent

Slide 90

Slide 90 text

English

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

Ruby

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

Be clear

Slide 96

Slide 96 text

Magyar

Slide 97

Slide 97 text

MÁV

Slide 98

Slide 98 text

Megint Állunk Vazze

Slide 99

Slide 99 text

BKV

Slide 100

Slide 100 text

Biztos Késünk Vazze

Slide 101

Slide 101 text

FIDESZ

Slide 102

Slide 102 text

Forintot Ide De SZaporán

Slide 103

Slide 103 text

Ruby

Slide 104

Slide 104 text

$:

Slide 105

Slide 105 text

$LOAD_PATH

Slide 106

Slide 106 text

$;

Slide 107

Slide 107 text

$FIELD_SEPARATOR

Slide 108

Slide 108 text

$*

Slide 109

Slide 109 text

$ARGV

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

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

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

Ruby is optimized for programmer happiness

Slide 115

Slide 115 text

Ruby is a huge language

Slide 116

Slide 116 text

Making the “right” decisions is hard

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

puts "Hello, Budapest!" puts "Hello, Budapest!" puts "Hello, Budapest!"

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

Are all those options worth our while?

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

No content

Slide 132

Slide 132 text

56 simple rules

Slide 133

Slide 133 text

Format a program to help the reader understand it.

Slide 134

Slide 134 text

Write clearly -- don't be too clever.

Slide 135

Slide 135 text

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

Slide 136

Slide 136 text

Don't sacrifice clarity for small gains in efficiency.

Slide 137

Slide 137 text

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

Slide 138

Slide 138 text

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

Slide 139

Slide 139 text

(N * (N + 1)) / 2

Slide 140

Slide 140 text

(10 * (10 + 1)) / 2

Slide 141

Slide 141 text

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

Slide 142

Slide 142 text

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

Slide 143

Slide 143 text

Instrument your programs. Measure before making efficiency changes.

Slide 144

Slide 144 text

Use variable names that mean something.

Slide 145

Slide 145 text

a s l

Slide 146

Slide 146 text

age sex location

Slide 147

Slide 147 text

age_over_18?

Slide 148

Slide 148 text

adult?

Slide 149

Slide 149 text

Don’t repeat yourself.

Slide 150

Slide 150 text

Parenthesize to avoid ambiguity.

Slide 151

Slide 151 text

x || y && z

Slide 152

Slide 152 text

x || (y && z)

Slide 153

Slide 153 text

Keep the code & its comments in sync.

Slide 154

Slide 154 text

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

Slide 155

Slide 155 text

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

Slide 156

Slide 156 text

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

Slide 157

Slide 157 text

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

Slide 158

Slide 158 text

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

Slide 159

Slide 159 text

Don’t repeat the code in the comments.

Slide 160

Slide 160 text

# increment counter by 1 counter += 1

Slide 161

Slide 161 text

Don’t comment shitty code — fix it.

Slide 162

Slide 162 text

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

Slide 163

Slide 163 text

Fuck this shit!!!

Slide 164

Slide 164 text

Style guides for programming languages

Slide 165

Slide 165 text

Python

Slide 166

Slide 166 text

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

Slide 167

Slide 167 text

Java

Slide 168

Slide 168 text

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

Slide 169

Slide 169 text

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

Slide 170

Slide 170 text

Go

Slide 171

Slide 171 text

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

Slide 172

Slide 172 text

gofmt

Slide 173

Slide 173 text

No content

Slide 174

Slide 174 text

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

Slide 175

Slide 175 text

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

Slide 176

Slide 176 text

PHP

Slide 177

Slide 177 text

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

Slide 178

Slide 178 text

No content

Slide 179

Slide 179 text

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

Slide 180

Slide 180 text

Clojure

Slide 181

Slide 181 text

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

Slide 182

Slide 182 text

Swift

Slide 183

Slide 183 text

Swift

Slide 184

Slide 184 text

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

Slide 185

Slide 185 text

Languages are always evolving

Slide 186

Slide 186 text

Good style practices are evolving as well

Slide 187

Slide 187 text

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

Slide 188

Slide 188 text

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

Slide 189

Slide 189 text

Style consistency is of paramount importance

Slide 190

Slide 190 text

Community PersonalProject Company

Slide 191

Slide 191 text

Community Project Company

Slide 192

Slide 192 text

Style separates the good from the great!

Slide 193

Slide 193 text

Felina

Slide 194

Slide 194 text

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