影片筆記: Domain Modeling Made Functional by Scott Wlaschin
https://www.youtube.com/watch?v=2JB1_e5wZmU
https://www.slideshare.net/ScottWlaschin/domain-modeling-made-functional-kandddinsky-2019
講者 Scott Wlaschin 是 Domain Modeling Made Functional 的作者
前言
之後主要從下面這個例子展開,展示 type 的重要和在設計中的美妙之處
type Contact = {
FirstName: string
MiddleInitial: string
LastName: string
EmailAddress: string
IsEmailVerified: bool
}
Functional programming is really good for… Boring Line Of Business Applications (BLOBAs)
Part.1 The importance of design
下面是一個 F# 的程式碼,即使沒有學過 F# 甚至沒有學過程式語言,還是可以很清楚地猜到這應該是個撲克牌的遊戲
module CardGame =
type Suit = Club | Diamond | Spade | Heart
type Rank = Two | Three | Four | Five | Six | Seven | Eight
| Nine | Ten | Jack | Queen | King
type Card = Suit * Rank
type Hand = Card list
type Deck = Card list
type Player = { Name:string; Hand:Hand }
type Game = { Deck:Deck; Players: Player list }
type Deal = Deck –› (Deck * Card)
type PickupCard = (Hand * Card) –› Hand
在這裡,動作被 model 成 function
type Deal = Deck –› (Deck * Card)
在 Deal 這個 function 中,input 是 Deck,而 output 是一個新的 Deck 和一張 Card
type PickupCard = (Hand * Card) –› Hand
而在 PickupCard 這個 function 中,input 是 Hand 和 Card,而 output 是新的 Hand
回到一開始 Contact 的例子,
這些欄位都一定要有嗎? 不一定. 例如不一定有 MiddleName
FirstName 的 type 是 String,但他真的可以是任意 String 嗎? 不. 他應該要限制長度、哪些字元等等
Part.2 Understanding FP type systems
FP principle: types are not classes
type 是 sets
FP principle: composition everywhere
- compose with AND
s ss
- compose with OR
s ss
FP principle:
Part.3 Domain modeling with composable types ?
Modeling optional values
Modeling simple values and constraint values
…
Replacing flags with choices
Part.4 Encoding business rules with types
Summary
Use code to represent the shared mental model and ubiquitous language
Designs will evolve. Embrace change.
- Refactor towards deeper insight
- Static types give you confidence to make changes
Use the power of a composable type system
- Choices rather than inheritance
- Options instead of null
- Wrappers for constrained types
- Make illegal states unrepresentable