Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Type Safety in an Unsafe World with Kotlin
Search
Cody Engel
February 02, 2021
Technology
0
26
Type Safety in an Unsafe World with Kotlin
Cody Engel
February 02, 2021
Tweet
Share
More Decks by Cody Engel
See All by Cody Engel
Kotlin 2 ½ Years Later
codyengel
0
48
Tech Community, Population Us
codyengel
0
53
Kotlin Lunch and Learn
codyengel
0
51
How To Make A Better Sandwich
codyengel
0
32
Other Decks in Technology
See All in Technology
PHPerのための計算量入門/Complexity101 for PHPer
hanhan1978
6
1.5k
Unlearn Product Development - Unleashed Edition
lemiorhan
PRO
2
160
20241220_S3 tablesの使い方を検証してみた
handy
4
850
Fearsome File Formats
ange
0
550
終了の危機にあった15年続くWebサービスを全力で存続させる - phpcon2024
yositosi
28
25k
実践! ソフトウェアエンジニアリングの価値の計測 ── Effort、Output、Outcome、Impact
nomuson
0
790
NOT VALIDな検査制約 / check constraint that is not valid
yahonda
1
100
20240522 - 躍遷創作理念 @ PicCollage Workshop
dpys
0
300
Evolving Architecture
rainerhahnekamp
2
170
Denoで作るチーム開発生産性向上のためのCLIツール
sansantech
PRO
0
120
.NET 9 のパフォーマンス改善
nenonaninu
0
2.1k
OPENLOGI Company Profile for engineer
hr01
1
17k
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Docker and Python
trallard
43
3.2k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Documentation Writing (for coders)
carmenintech
67
4.5k
Designing for Performance
lara
604
68k
GitHub's CSS Performance
jonrohan
1030
460k
Writing Fast Ruby
sferik
628
61k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
Optimising Largest Contentful Paint
csswizardry
33
3k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.5k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
18
2.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
220
Transcript
Type Safety in an Unsafe World with Kotlin Cody Engel,
Staff Software Engineer @ PayPal
What is Type Safety? It is kind of confusing. Photo:
Matthew Hamilton via Unsplash
Robin Milner, A Theory of Type Polymorphism in Programming “Well
typed programs cannot go wrong.”
Going Wrong Syntactically valid, but meaningless. Type Safety by Michael
Hicks - http://bit.ly/ple-type-safety
Well Typed It may crash, but that is by design
in Kotlin. Type Safety by Michael Hicks - http://bit.ly/ple-type-safety
Well Typed, Continued This is by design in Ruby and
may also crash. Type Safety by Michael Hicks - http://bit.ly/ple-type-safety
Cody Engel, Type Safety in an Unsafe World with Kotlin
“Some languages have stricter behavior than others.”
What is Kotlin? It is a strict, yet concise programming
language. Photo: Marc Reichelt via Unsplash
Define Types Name can be inferred to be a String.
Define Mutability A variable defined as “var” can change.
Define Structures Structured data is more predictable.
Define Nullability Nullability is opt-in.
Define Concurrency Coroutines make concurrency simpler.
And The Unsafe World? That would be our APIs. Photo:
Jason Yoder via Unsplash
is_admin As a boolean it works well. Unfortunately, this wasn’t
how it was actually modeled.
is_admin Our permissions class was never safer! However this is
how we would model that information in Kotlin.
is_admin Okay, technically this isn’t too bad. Although, it is
not the best way to model a boolean with JSON.
is_admin Extension functions to the rescue. This could also be
an extension property, but extension functions look better in Keynote
is_admin Strings can also represent numeric values which can represent
booleans. I was pretty excited to make use of extension functions, so it was fine.
is_admin Unfortunately String already has a toBoolean function. And that
function is for converting “true” and “false” into a boolean
is_admin I can’t recall if this was actually possible with
the API. However there is a good chance it could have been possible based on customer configurations.
is_admin Fortunately, it isn’t too difficult to add. Nonetheless, it’d
be great if this wasn’t required in the first place
is_admin It was nullable though. That’s okay though, nullability is
actually pretty simple with Kotlin.
is_admin Updating our extension function to be on a nullable
Any works. Since we type cast on non-null types, the else branch catches our null case.
Making The World a Safer Place How to design safer
APIs. Photo: Matthew Rumph via Unsplash
Stick to a strict API when rolling your own specification.
Use an industry standard specification, such as Open API.
Look at JSON alternatives such as Protocol Buffers.
Let’s build some great and safe APIs. • YouTube -
bit.ly/cody-yt • Twitter - bit.ly/cody-twitter • Medium - bit.ly/cody-medium Thanks!