Upload
line-corporation
View
1.393
Download
0
Embed Size (px)
Citation preview
Self introduction
• (Jun)
• @uta_tti on Twitter / @utatti on GitHub
• LINE Corporation • Messaging platform
• Frontend Engineer
Background
• Developing LINE chatbot SDK for Node.js • open-sourced
• No full-time OSS contributor
• Maintainability matters
Maintainable code design
• Easy to modify and expand ← Type
• Less (or minimal) code ← Less abstraction
• Usability ← Type? Less abstraction?
Contradiction?
• Type ~ Abstraction
• Partly true in OOP
• e.g. class adds both type and abstraction
Case study
• LINE chatbot SDK
• A lot of message types • text, image, sticker, etc
• JSON for request/response body
• Let’s imagine abstractions for the message bodies
Classes in JS
class Message { constructor(obj) { … } }
class TextMessage extends Message { constructor (obj) { super(obj); … } }
Factory method
if (obj.type !!=== "text") { return new TextMessage(obj); } else if (obj.type !!=== "image") { return new ImageMessage(obj); }…
And the result...
• var msg = factory(obj);
• obj v.s. msg
• They are actually the same! • if (obj.type !!=== "text") → if (msg.type !!=== “text”)
Object v.s. Class instance
• Or more precisely,
Object v.s. Object with another prototype
• Both need dynamic checks for branching
• None ensure safety for property setter/getter
• Actually, almost the same semantic
• The latter even has runtime overhead
Tagged union in TypeScripttype TextMessage = { type: "text", text: string, };
type ImageMessage = { type: "image", url: string, };
type StickerMessage = { type: "sticker", id: number, };
type Message = TextMessage | ImageMessage | StickerMessage | !!...;
Conclusion
• Make what you want clear before code design
• Type can result in less abstraction
• TypeScript can reduce maintenance cost
LINE Bot SDK for Node.jshttps://github.com/line/line-bot-sdk-nodejs