WRITING the NEXT GREAT KOTLIN NOVEL or , Kotlin Beyond the Style - - PowerPoint PPT Presentation

writing the next great kotlin novel
SMART_READER_LITE
LIVE PREVIEW

WRITING the NEXT GREAT KOTLIN NOVEL or , Kotlin Beyond the Style - - PowerPoint PPT Presentation

WRITING the NEXT GREAT KOTLIN NOVEL or , Kotlin Beyond the Style Guide Lisa Wray @lisawrayz WRITING the NEXT GREAT KOTLIN NOVEL or , Kotlin Beyond the Style Guide Lisa Wray @lisawrayz My vision: Ill read books about writing On Writing ,


slide-1
SLIDE 1

WRITING the NEXT GREAT KOTLIN NOVEL

  • r,

Kotlin Beyond the Style Guide

Lisa Wray @lisawrayz

slide-2
SLIDE 2

WRITING the NEXT GREAT KOTLIN NOVEL

  • r,

Kotlin Beyond the Style Guide

Lisa Wray @lisawrayz

slide-3
SLIDE 3

My vision: I’ll read books about writing

On Writing, Steven King On Writing Well, William Zinsser Bird by Bird, Annie Lamar What I Talk About When I Talk About Running, Haruki Murakami

slide-4
SLIDE 4

Reality: Let’s go back to basics.

The Elements of Style, Strunk & White Politics & The English Language, George Orwell

slide-5
SLIDE 5

natural language

noun a language that has developed naturally in use (as contrasted with an artificial language or computer code).

slide-6
SLIDE 6

Natural language depends on context

slide-7
SLIDE 7

Natural language is ambiguous “Take a bow.”

slide-8
SLIDE 8

“Take a bow.”

slide-9
SLIDE 9

“Take a bow.”

slide-10
SLIDE 10

“Take a bow.”

slide-11
SLIDE 11

“[English] becomes ugly and inaccurate because our thoughts are foolish, but the slovenliness of our language makes it easier for us to have foolish thoughts.”

George Orwell

slide-12
SLIDE 12

formal language

noun a language designed for use in situations in which natural language is unsuitable, as for example in mathematics, logic,

  • r computer programming
slide-13
SLIDE 13

If it compiles, then it’s clear to the computer.

slide-14
SLIDE 14

If it compiles, then it’s clear to the computer. Only clear to the computer

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>—.+++++++..++ +.>>.<-.<.+++.———.--------.>>+.>++.

Brainfuck: “Hello World”

slide-15
SLIDE 15

If it compiles, then it’s clear to the computer. Only clear to the computer

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>—.+++++++..++ +.>>.<-.<.+++.———.--------.>>+.>++.

Brainfuck: “Hello World”

Only clear to the author

"^\(*\d{3}\)*( |-)*\d{3}( |-)*\d{4}$”

Regex: ‘Validate’ a phone number in regex

slide-16
SLIDE 16

Clear to anyone

Stage is a room. The old lady is a woman in the Stage. Understand "mother" or "stepmother" as the old lady. The old lady is active. […] The Prince is a man in the Stage. […] The prince carries a glass

  • slipper. The glass slipper is wearable. Understand "shoe" or "heel" or

"toe" or “foot" as the slipper. The description of the slipper is "It is very small for an adult woman's foot."

Inform 7: “Glass”, by Emily Short

slide-17
SLIDE 17

Clear to anyone … but is this really better?

Stage is a room. The old lady is a woman in the Stage. Understand "mother" or "stepmother" as the old lady. The old lady is active. […] The Prince is a man in the Stage. […] The prince carries a glass

  • slipper. The glass slipper is wearable. Understand "shoe" or "heel" or

"toe" or “foot" as the slipper. The description of the slipper is "It is very small for an adult woman's foot."

Inform 7: “Glass”, by Emily Short

slide-18
SLIDE 18

Kotlin falls in the middle

Takes its source vocabulary from English define terms more narrowly (ex: open, return, if / else ) modify (ex: fun(ction), foreach) some is just added ( -> )

slide-19
SLIDE 19

This is not a programming talk

If your code compiles, you can communicate with your computer. This talk is about communicating with people.

slide-20
SLIDE 20

The official Kotlin style guide

Mechanics: Whitespace, capitalization, punctuation, ordering Naming Idiomatic usage: immutability, when, defaults vs overloads, var vs function, scope functions

slide-21
SLIDE 21

Naming

An object is a noun “The name of a class is usually a noun or a noun phrase explaining what the class is: List, PersonReader.” A method is a verb “The name of a method is usually a verb or a verb phrase saying what the method does: close, readPersons.”

slide-22
SLIDE 22

Choosing good names

What are bad names? “The names should make it clear what the purpose of the entity is, so it's best to avoid using meaningless words (Manager, Wrapper, etc.) in names.”

slide-23
SLIDE 23

Choosing good names

What are bad names? “The names should make it clear what the purpose of the entity is, so it's best to avoid using meaningless words (Manager, Wrapper, etc.) in names.” Translation: #$@&%*! Java

slide-24
SLIDE 24

The optional feature

In general, if a certain syntactic construction in Kotlin is

  • ptional […], you should omit it in your code. Do not leave

unnecessary syntactic elements in code just "for clarity”.

Kotlin style guide

slide-25
SLIDE 25

Orwell: “If it is possible to cut a word out, always cut it out.” Strunk: “Omit needless words.”

slide-26
SLIDE 26

Android Studio is less opinionated …

fun function(): Boolean = true fun function() = true

slide-27
SLIDE 27

Infix functions

class function or extension function with one parameter

a and b “Hello, World" matches “^Hello".toRegex()

slide-28
SLIDE 28

Infix: custom

“Lisa likes food.”

enum Interest ( Food, Wine, Hiking, Yoga ) data class Person(val interests: List<Interest>) val lisa = Person(listOf(Food, Wine)) fun Person.likes(interest: Interest) = person.interests.contains(interest) lisa.likes(Food)

slide-29
SLIDE 29

“Lisa likes food.”

infix fun Person.likes(interest: Interest) {} lisa likes Food // true lisa likes Hiking // false

slide-30
SLIDE 30

infix Interest.and(other: Interest) = listOf(this, other) infix fun Person.likes(interests: List<Interest>) = … lisa likes (Food and Wine)

slide-31
SLIDE 31

Why not infix everywhere?

All custom infix functions have the same precedence

(Lisa likes (Food and Wine)) and (Bob likes (Yoga and Hiking))

slide-32
SLIDE 32

Why not infix everywhere?

All custom infix functions have the same precedence

(Lisa likes (Food and Wine)) and (Bob likes (Yoga and Hiking))

Kotlin does not have verb conjugation Most functions are written in the imperative case.

val trash = TrashBag() if (trash.isFull) Lisa.takeOut(trash) Lisa takeOut trash Lisa takesOut trash

slide-33
SLIDE 33

Great use: testing

infix fun Person.likes(interest: Interest) {…} infix fun Person.dislikes(interest: Interest) {…} assert(lisa likes Food) assert(lisa dislikes Hiking)

slide-34
SLIDE 34

The passive voice

Active: Bob throws the ball. Passive: The ball was thrown by Bob.

slide-35
SLIDE 35

The passive voice

King: “You should avoid the passive tense.” Strunk: “Use the active voice.” Orwell: “Never use the passive where you can use the active.”

slide-36
SLIDE 36

The passive voice

Can omit the subject Active: I performed the experiment. Passive: The experiment was performed. “I would have done my homework, but my data was deleted.”

slide-37
SLIDE 37

The “evasive voice”

“Darth Vader Dies On Death Star”

enum State { Alive, Dead } class Person (var state: State = Alive) val vader = Person() vader.state = Dead

slide-38
SLIDE 38

Original: Darth Vader dies on Death Star Passive: Darth Vader killed by Emperor Palpatine Active: Emperor Palpatine kills Darth Vader

slide-39
SLIDE 39

Original: Vader dies Passive: Vader killed by Palpatine Active: Palpatine kills Vader

slide-40
SLIDE 40

Transitive: Palpatine kills Vader Intransitive: Vader dies Can’t do: Palpatine dies Vader

slide-41
SLIDE 41

Bad headline: Vader dies. Good headline: Palpatine kills Vader. (Implied: Vader dies.)

slide-42
SLIDE 42

enum State { Alive, Dead } class Person(var state: State = Alive)

slide-43
SLIDE 43

sealed class State 


  • bject Alive: State()


class Dead(val cause: Cause): State() class Person(var state: State = Alive)

slide-44
SLIDE 44

sealed class State 


  • bject Alive: State()


class Dead(val cause: Cause): State() class Person(var state: State = Alive)
 
 sealed class Cause
 class NaturalDeath(val reason: String): Cause()
 class Accident(val reason: String): Cause()
 class Killed(val by: Person): Cause()
 
 val PeacefullyDead = Dead(NaturalDeath("old age"))

slide-45
SLIDE 45

This is still an exhaustive when.

when (state) {
 is Alive -> { } // do something
 else -> { }
 }

slide-46
SLIDE 46

“Vader dies”

vader.state = Dead() No value passed for parameter “cause”

slide-47
SLIDE 47

“Palpatine kills Vader”

val palpatine = Person() vader.state = Dead(Killed(by = palpatine))

slide-48
SLIDE 48

Scope functions

The conjunctions of Kotlin “A common way to fall into wordiness is to present a single complex idea, step by step, in a series of sentences that might to advantage be combined into one.”

Strunk, The Elements of Style

slide-49
SLIDE 49

Scope functions … in English?

“I’m going to the store now. The store gets busy after work.” vs: “I’m going to the store because it gets busy after work."

slide-50
SLIDE 50

“I’m going to the store. The store is probably busy right now. But I’m out of ice cream." vs: “I’m going to the store, which is probably busy now, but I’m

  • ut of ice cream.”
slide-51
SLIDE 51

“I’ll have an ice cream with sprinkles."

return IceCream().apply { add(sprinkles) }

“I’ll have an ice cream, and I need a receipt.”

return IceCream().also { printReceipt(it) }

slide-52
SLIDE 52

“I’ll have an ice cream with sprinkles, and I need a receipt.”

return IceCream().apply { 
 add(sprinkles); 
 printReceipt(this) 
 }

slide-53
SLIDE 53

“I’ll have an ice cream with sprinkles, and I need a receipt.”

return IceCream().apply { 
 add(sprinkles); 
 printReceipt(this) 
 } return IceCream().apply { add(sprinkles) }.also { printReceipt(it) }


slide-54
SLIDE 54

“If they still have ice cream, get me one with sprinkles and a receipt.”

iceCream?.apply { add(sprinkles) }.also { printReceipt(it) }

Receipt for “null” 🍧

slide-55
SLIDE 55

“If they still have ice cream, get me one with sprinkles and a receipt.”

iceCream?.apply { add(sprinkles) }?.also { printReceipt(it) }

Only get a receipt 
 if there is still ice cream.

slide-56
SLIDE 56

“Avoid a succession of loose sentences. This rule refers especially to sentences of a particular type: those consisting of two clauses, the second introduced by a conjunction … An unskilled writer will sometimes construct a whole paragraph of sentences of this kind.”

Strunk, The Elements of Style

slide-57
SLIDE 57

“Avoid a succession of loose sentences. This rule refers especially to sentences of a particular type: those consisting of two clauses, the second introduced by a conjunction … An unskilled writer will sometimes construct a whole paragraph of sentences of this kind.”

Strunk, The Elements of Style

slide-58
SLIDE 58

enum class Topping { Sprinkles, HotFudge, Caramel } class IceCream( vararg toppings: Topping, flavor: String, log: (IceCream) -> Unit )) IceCream(Sprinkles, HotFudge, flavor = “Vanilla”) { printReceipt(it)
 }

slide-59
SLIDE 59

enum class Topping { Sprinkles, HotFudge, Caramel } class IceCream( vararg toppings: Topping, flavor: String, log: (IceCream) -> Unit )) IceCream(Sprinkles, HotFudge, flavor = “Vanilla”) { printReceipt(it)
 }

slide-60
SLIDE 60

enum class Topping { Sprinkles, HotFudge, Caramel } class IceCream( vararg toppings: Topping, flavor: String, log: (IceCream) -> Unit ) IceCream(Sprinkles, HotFudge, flavor = “Vanilla”) { printReceipt(it)
 }

slide-61
SLIDE 61

enum class Topping { Sprinkles, HotFudge, Caramel } class IceCream( vararg toppings: Topping, flavor: String, log: (IceCream) -> Unit ) IceCream(Sprinkles, HotFudge, flavor = “Vanilla”) { printReceipt(it)
 }

slide-62
SLIDE 62

iceCream {
 toppings {
 sprinkles()
 }
 printReceipt()
 }

Custom DSL

slide-63
SLIDE 63

iceCream {
 toppings {
 sprinkles()
 }
 printReceipt()
 }

Custom DSL (Domain Specific Language)

slide-64
SLIDE 64

iceCream { this: IceCreamBuilder
 toppings {
 sprinkles()
 }
 printReceipt()
 }

Custom DSL (Domain Specific Language)

slide-65
SLIDE 65

Can you have too many custom DSLs?

slide-66
SLIDE 66

Can you have too many custom DSLs?

If you’re writing them — Yes Adds boilerplate and maintenance overhead to your code Don’t use a DSL where named parameters will do Can you pollute your code base with DSLs?

slide-67
SLIDE 67

Can you have too many custom DSLs?

If you’re writing them — Yes Adds boilerplate and maintenance overhead to your code Don’t use a DSL where named parameters will do Can you pollute your code base with DSLs? Not really

slide-68
SLIDE 68

Can you have too many custom DSLs?

If you’re writing them — Yes Adds boilerplate and maintenance overhead to your code Don’t use a DSL where named parameters will do Can you pollute your code base with DSLs? Not really “Custom DSLs” in Kotlin are internal DSLs

slide-69
SLIDE 69

Beyond the style guide

Kotlin gives you new choices and paradigms You choose how to use (or abuse) them

slide-70
SLIDE 70

Thank you!

Lisa Wray @lisawrayz