SlideShare a Scribd company logo
Flow for JavaScript
Imposing structure on a freeform world
Ikai Lan
@ikai
About me
• Ikai Lan, new to Medium, new to JavaScript
• … hopefully not new to programming
• Based out of San Francisco, but I love New York
City
•
Favorite emoji: 😐
Here tonight
Platform engineering
Why type checking?
“I want to build a
house … ”
“… so I got some raw materials,
and I built one.”
More people want to
move in …
Now you need some
rules, before things get
out of hand…
The array that wasn’t.
// Before the fix



getLatestPosts(userId) {

if (!userId) {

return Q.resolve(null)

}



// More code here to fetch posts and 

// return a Promise

}
The array that wasn’t.
// Somewhere several layers up 

// the stack

let result = client.getLatestPosts(currentUserId)

result.forEach((value) => { ... }) 

The array that wasn’t.
// Somewhere several layers up 

// the stack

let result = client.getLatestPosts(currentUserId)

result.forEach((value) => { ... }) 

Uncaught TypeError: Cannot read property 'foreach' of
null(…)
The array that wasn’t.




getLatestPosts(userId) {

if (!userId) {

return Q.resolve([])

}



// More code here to fetch posts and 

// return a Promise

}
The array that wasn’t.




getLatestPosts(userId) {

if (!userId) {

return Q.resolve([])

}



// More code here to fetch posts and 

// return a Promise

}
[]
Yet, the docs …
/**

* @return {Promise<Array<Post>>} A sample of 

* the user's posts 

*/

getLatestPosts(userId) {

if (!userId) {

return Q.resolve(null)

}

// More code here to fetch posts and

// return a Promise

}
Yet, the docs …
/**

* @return {Promise<Array<Post>>} A sample of 

* the user's posts 

*/

getLatestPosts(userId) {

if (!userId) {

return Q.resolve(null)

}

// More code here to fetch posts and

// return a Promise

}
/**

* @return {Promise<Array<Post>>} A sample of 

* the user's posts 

*/
Without automatic checks,
there is decay …
I want an object!
- var recommendedPostRelationsPromise =
this._postUserService.getVotesForUserId(followedUserId,
recommendedSince)

+ var recommendedPostRelationsPromise =
this._postUserService.getVotesForUserId(followedUserId,
{votedAfter: recommendedSince})
I want an object!
- var recommendedPostRelationsPromise =
this._postUserService.getVotesForUserId(followedUserId,
recommendedSince)

+ var recommendedPostRelationsPromise =
this._postUserService.getVotesForUserId(followedUserId,
{votedAfter: recommendedSince})
What if that value is
undefined?
/**

* @returns an Array of only recent votes

*/

getVotesForUserId(userId, filterParams) {

// Limit results returned to items greater
// than this parameter

return queryForVotesAfter(
filterParams.recommendedSince)

}

This isn’t horror story
hour
Closure Flow
Google Facebook
Type annotations in docs Type annotations inline
Valid JavaScript Requires transformation
Convert entire codebase File-by-file
Closure Flow
Google Facebook
Type annotations in docs Type annotations inline
Valid JavaScript Requires transformation
Convert entire codebase File-by-file
How do we add types?
‘use strict’ // @flow
First line of a file to annotate
Flow annotated JavaScript
buildQuery(path, options) {
buildQuery(path: string,
options: GoSocialRequestOptions): string {
Flow annotated JavaScript
buildQuery(path, options) {
buildQuery(path: string,
options: GoSocialRequestOptions): string {
Parameter types
Flow annotated JavaScript
buildQuery(path, options) {
buildQuery(path: string,
options: GoSocialRequestOptions): string {
Return type
Lint
Flow
Test
Lint
Flow
Test
Which methods in files with @flow
are missing annotations?
Lint
Flow
Test
Do the types match up? Do they
make sense?
server/graph/common/goSocialClient.js:382
382: .then((result: any): Array<T> => {
^^^^^^^^ array type. This type is incompatible with
12: onFulfill?: (value: R) => KewPromise<U> | U,
^^^^^^^^^^^^^^^^^ union: type application of identifier
`KewPromise` | type parameter `U` of call of method `then`. See lib: style-guide/flow-interfaces.js:
12
Member 1:
12: onFulfill?: (value: R) => KewPromise<U> | U,
^^^^^^^^^^^^^ type application of identifier `KewPromise`. See
lib: style-guide/flow-interfaces.js:12
Error:
382: .then((result: any): Array<T> => {
^^^^^^^^ array type. This type is incompatible with
12: onFulfill?: (value: R) => KewPromise<U> | U,
^^^^^^^^^^^^^ KewPromise. See lib: style-guide/flow-
interfaces.js:12
Member 2:
381: return this.request(requestData)
^ type parameter `U` of call of method `then`
Error:
382: .then((result: any): Array<T> => {
^^^^^^^^ array type. This type is incompatible with
373: fetchObjects<T: RpcSchema>(Ctor: Class<T>, options: GoSocialRequestOptions): Promise<T> {
^ some incompatible instantiation of `T`
When flow complains …
Lint
Flow
Test
Do the unit and functional tests
still pass? Manual poking
type GoSocialRequestOptions = {

method?: string,

requestType?: string,

type?: string,

path?: string,

udpEvent?: Object,

shape?: string,

query?: Object,

properties?: Object,

}
{} as a parameter
What makes this project challenging?
We needed to figure out
the dependency graph.
LoginService
AuthManager
UserManager
Depends on …
Depends on …
Why the dependency graph
matters
Our graph 😭 😭 😭
The method docs are out of date.
Remember this?
/**

* @return {Promise<Array<Post>>} A sample of 

* the user's posts 

*/

getLatestPosts(userId) {

if (!userId) {

return Q.resolve(null)

}

// More code here to fetch posts and

// return a Promise

}
We have to define interfaces for
third party libraries
// Source: https://github.com/moment/moment/blob/develop/moment.d.ts

declare class Moment {

subtract(val: number, unit: string): this;

add(amount: number, unitOfTime: string): this;

diff(b: Moment, unitOfTime?: string, round?: boolean): number;

startOf(unit: string): this;

valueOf(): number;

}



declare module 'moment' {

declare function utc(): Moment;

declare function exports(): Moment;

}
Mistakes are buried
layers deep - changes need to
be made that don’t result in
unintended consequences
OCaml 😐
One more thing: nulls.
create(tag: Tag): Promise<?Tag>
This value is NOT allowed to be null
create(tag: Tag): Promise<?Tag>
This value is allowed to be null!
create(tag: Tag): Promise<Tag> {

let tag = null;



if (someCondition) {

tag = new Tag()

}



return Q.resolve(tag)

}

No ‘?’ to show that return type is nullable
ERROR
create(tag: Tag): Promise<?Tag> {

let tag = null;



if (someCondition) {

tag = new Tag()

}



return Q.resolve(tag)

}

‘?’ shows that return type is nullable
Correct!
Now everything this touches
• Has to null check (via a conditional) - subtle
changes in how code works
• Or accept a type ?Tag, which means “nullable
type”
• Adding to existing code can be tricky without
introducing subtle changes
But this is also good
• Makes us really think about which methods will
always have a valid object, which will not
• Will lead to better API design long term
But it can be confusing
function myNullableString(input: ?string): number {

return input.length;

}



function callerOfMyNullableString(input: string): number {

return myNullableString(input);

}

function myNullableString(input: ?string): number {

return input.length;

}



function callerOfMyNullableString(input: string): number {

return myNullableString(input);

}

input: ?string
input: string
This is valid
This is not valid
function myArray(input: Array<?string>): number {

return input.length;

}









function callerOfMyArray(input: Array<string>): number
{

return myArrayWithNullableObjects(input);

}
Here’s why
function myArray(input: Array<?string>): number
{

input.append(null);

return input.length;

}
function callerOfMyArray(input: Array<string>): number {

return myArrayWithNullableObjects(input);

}
No longer valid!
Organizational challenges
• General organization buy-in
• Flow is almost an entirely new language that
people need to learn
• Working against a moving target
• Funny things that happen after merges
To recap
• We are finding it hard to scale development
without types
• Adding types to untyped code has a number of
challenges, technical and otherwise
• We believe the payoff will be totally worth it.
Thank you!
• Thanks to Kelly, Nick, Gianni for being great
teammates and teaching me everything
• Thanks to Madeline for organizing this great
event!
• Image source: unsplash.com
Q&A
• If you come up with someone, just ask one of us!
• Twitter/Medium: @ikai

More Related Content

What's hot

Quick swift tour
Quick swift tourQuick swift tour
Quick swift tour
Kazunobu Tasaka
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
Ravi Bhadauria
 
Swift in SwiftUI
Swift in SwiftUISwift in SwiftUI
Swift in SwiftUI
Bongwon Lee
 
Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017
David Schmitz
 
JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp Developer
Sarvesh Kushwaha
 
Javaslang @ Devoxx
Javaslang @ DevoxxJavaslang @ Devoxx
Javaslang @ Devoxx
David Schmitz
 
What You Need to Know about Lambdas
What You Need to Know about LambdasWhat You Need to Know about Lambdas
What You Need to Know about Lambdas
Ryan Knight
 
Angular Weekend
Angular WeekendAngular Weekend
Angular Weekend
Troy Miles
 
Functional Principles for OO Developers
Functional Principles for OO DevelopersFunctional Principles for OO Developers
Functional Principles for OO Developers
jessitron
 
TDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with AndroidTDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with Android
Codemotion
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
Reem Alattas
 
Akka Futures and Akka Remoting
Akka Futures  and Akka RemotingAkka Futures  and Akka Remoting
Akka Futures and Akka Remoting
Knoldus Inc.
 
React Native One Day
React Native One DayReact Native One Day
React Native One Day
Troy Miles
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
Subramanyan Murali
 
Writing testable code
Writing testable codeWriting testable code
Writing testable code
Thiago Figueredo Cardoso
 
Architecture for scalable Angular applications
Architecture for scalable Angular applicationsArchitecture for scalable Angular applications
Architecture for scalable Angular applications
Paweł Żurowski
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
Solv AS
 
Workflow Foundation 4
Workflow Foundation 4Workflow Foundation 4
Workflow Foundation 4
Robert MacLean
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-C
Alexis Gallagher
 
Contracts in-clojure-pete
Contracts in-clojure-peteContracts in-clojure-pete
Contracts in-clojure-pete
jessitron
 

What's hot (20)

Quick swift tour
Quick swift tourQuick swift tour
Quick swift tour
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
 
Swift in SwiftUI
Swift in SwiftUISwift in SwiftUI
Swift in SwiftUI
 
Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017
 
JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp Developer
 
Javaslang @ Devoxx
Javaslang @ DevoxxJavaslang @ Devoxx
Javaslang @ Devoxx
 
What You Need to Know about Lambdas
What You Need to Know about LambdasWhat You Need to Know about Lambdas
What You Need to Know about Lambdas
 
Angular Weekend
Angular WeekendAngular Weekend
Angular Weekend
 
Functional Principles for OO Developers
Functional Principles for OO DevelopersFunctional Principles for OO Developers
Functional Principles for OO Developers
 
TDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with AndroidTDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with Android
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
Akka Futures and Akka Remoting
Akka Futures  and Akka RemotingAkka Futures  and Akka Remoting
Akka Futures and Akka Remoting
 
React Native One Day
React Native One DayReact Native One Day
React Native One Day
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Writing testable code
Writing testable codeWriting testable code
Writing testable code
 
Architecture for scalable Angular applications
Architecture for scalable Angular applicationsArchitecture for scalable Angular applications
Architecture for scalable Angular applications
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Workflow Foundation 4
Workflow Foundation 4Workflow Foundation 4
Workflow Foundation 4
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-C
 
Contracts in-clojure-pete
Contracts in-clojure-peteContracts in-clojure-pete
Contracts in-clojure-pete
 

Similar to Structure on a freeform world

Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
ShriKant Vashishtha
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
Movel
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
Kent Huang
 
Functional Vaadin talk at OSCON 2014
Functional Vaadin talk at OSCON 2014Functional Vaadin talk at OSCON 2014
Functional Vaadin talk at OSCON 2014
hezamu
 
Protocol-Oriented Networking
Protocol-Oriented NetworkingProtocol-Oriented Networking
Protocol-Oriented Networking
Mostafa Amer
 
Naver_alternative_to_jpa
Naver_alternative_to_jpaNaver_alternative_to_jpa
Naver_alternative_to_jpa
NAVER Engineering
 
Ajax Under The Hood
Ajax Under The HoodAjax Under The Hood
Ajax Under The Hood
WO Community
 
"Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin "Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin
Vasil Remeniuk
 
Rethinking Syncing at AltConf 2019
Rethinking Syncing at AltConf 2019Rethinking Syncing at AltConf 2019
Rethinking Syncing at AltConf 2019
Joe Keeley
 
Alternatives of JPA/Hibernate
Alternatives of JPA/HibernateAlternatives of JPA/Hibernate
Alternatives of JPA/Hibernate
Sunghyouk Bae
 
TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason Haffey
Ralph Johnson
 
API first with Swagger and Scala by Slava Schmidt
API first with Swagger and Scala by  Slava SchmidtAPI first with Swagger and Scala by  Slava Schmidt
API first with Swagger and Scala by Slava Schmidt
JavaDayUA
 
Extensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptExtensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScript
Brendan Eich
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013
yohanbeschi
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6
Andy Butland
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
Oswald Campesato
 
RIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWTRIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWT
Michael Galpin
 
Применение паттерна Page Object для автоматизации веб сервисов
Применение паттерна Page Object для автоматизации веб сервисовПрименение паттерна Page Object для автоматизации веб сервисов
Применение паттерна Page Object для автоматизации веб сервисов
COMAQA.BY
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with Swift
Fatih Nayebi, Ph.D.
 

Similar to Structure on a freeform world (20)

Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
 
Functional Vaadin talk at OSCON 2014
Functional Vaadin talk at OSCON 2014Functional Vaadin talk at OSCON 2014
Functional Vaadin talk at OSCON 2014
 
Protocol-Oriented Networking
Protocol-Oriented NetworkingProtocol-Oriented Networking
Protocol-Oriented Networking
 
Naver_alternative_to_jpa
Naver_alternative_to_jpaNaver_alternative_to_jpa
Naver_alternative_to_jpa
 
Ajax Under The Hood
Ajax Under The HoodAjax Under The Hood
Ajax Under The Hood
 
"Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin "Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin
 
Rethinking Syncing at AltConf 2019
Rethinking Syncing at AltConf 2019Rethinking Syncing at AltConf 2019
Rethinking Syncing at AltConf 2019
 
Alternatives of JPA/Hibernate
Alternatives of JPA/HibernateAlternatives of JPA/Hibernate
Alternatives of JPA/Hibernate
 
TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason Haffey
 
API first with Swagger and Scala by Slava Schmidt
API first with Swagger and Scala by  Slava SchmidtAPI first with Swagger and Scala by  Slava Schmidt
API first with Swagger and Scala by Slava Schmidt
 
Extensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptExtensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScript
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
 
Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
 
RIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWTRIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWT
 
Применение паттерна Page Object для автоматизации веб сервисов
Применение паттерна Page Object для автоматизации веб сервисовПрименение паттерна Page Object для автоматизации веб сервисов
Применение паттерна Page Object для автоматизации веб сервисов
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with Swift
 

Recently uploaded

SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 

Recently uploaded (20)

SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 

Structure on a freeform world

  • 1. Flow for JavaScript Imposing structure on a freeform world Ikai Lan @ikai
  • 2. About me • Ikai Lan, new to Medium, new to JavaScript • … hopefully not new to programming • Based out of San Francisco, but I love New York City • Favorite emoji: 😐
  • 6. “I want to build a house … ”
  • 7. “… so I got some raw materials, and I built one.”
  • 8. More people want to move in …
  • 9. Now you need some rules, before things get out of hand…
  • 10. The array that wasn’t. // Before the fix
 
 getLatestPosts(userId) {
 if (!userId) {
 return Q.resolve(null)
 }
 
 // More code here to fetch posts and 
 // return a Promise
 }
  • 11. The array that wasn’t. // Somewhere several layers up 
 // the stack
 let result = client.getLatestPosts(currentUserId)
 result.forEach((value) => { ... }) 

  • 12. The array that wasn’t. // Somewhere several layers up 
 // the stack
 let result = client.getLatestPosts(currentUserId)
 result.forEach((value) => { ... }) 
 Uncaught TypeError: Cannot read property 'foreach' of null(…)
  • 13. The array that wasn’t. 
 
 getLatestPosts(userId) {
 if (!userId) {
 return Q.resolve([])
 }
 
 // More code here to fetch posts and 
 // return a Promise
 }
  • 14. The array that wasn’t. 
 
 getLatestPosts(userId) {
 if (!userId) {
 return Q.resolve([])
 }
 
 // More code here to fetch posts and 
 // return a Promise
 } []
  • 15. Yet, the docs … /**
 * @return {Promise<Array<Post>>} A sample of 
 * the user's posts 
 */
 getLatestPosts(userId) {
 if (!userId) {
 return Q.resolve(null)
 }
 // More code here to fetch posts and
 // return a Promise
 }
  • 16. Yet, the docs … /**
 * @return {Promise<Array<Post>>} A sample of 
 * the user's posts 
 */
 getLatestPosts(userId) {
 if (!userId) {
 return Q.resolve(null)
 }
 // More code here to fetch posts and
 // return a Promise
 } /**
 * @return {Promise<Array<Post>>} A sample of 
 * the user's posts 
 */
  • 18. I want an object! - var recommendedPostRelationsPromise = this._postUserService.getVotesForUserId(followedUserId, recommendedSince)
 + var recommendedPostRelationsPromise = this._postUserService.getVotesForUserId(followedUserId, {votedAfter: recommendedSince})
  • 19. I want an object! - var recommendedPostRelationsPromise = this._postUserService.getVotesForUserId(followedUserId, recommendedSince)
 + var recommendedPostRelationsPromise = this._postUserService.getVotesForUserId(followedUserId, {votedAfter: recommendedSince})
  • 20. What if that value is undefined? /**
 * @returns an Array of only recent votes
 */
 getVotesForUserId(userId, filterParams) {
 // Limit results returned to items greater // than this parameter
 return queryForVotesAfter( filterParams.recommendedSince)
 }

  • 21. This isn’t horror story hour
  • 22. Closure Flow Google Facebook Type annotations in docs Type annotations inline Valid JavaScript Requires transformation Convert entire codebase File-by-file
  • 23. Closure Flow Google Facebook Type annotations in docs Type annotations inline Valid JavaScript Requires transformation Convert entire codebase File-by-file
  • 24. How do we add types? ‘use strict’ // @flow First line of a file to annotate
  • 25. Flow annotated JavaScript buildQuery(path, options) { buildQuery(path: string, options: GoSocialRequestOptions): string {
  • 26. Flow annotated JavaScript buildQuery(path, options) { buildQuery(path: string, options: GoSocialRequestOptions): string { Parameter types
  • 27. Flow annotated JavaScript buildQuery(path, options) { buildQuery(path: string, options: GoSocialRequestOptions): string { Return type
  • 29. Lint Flow Test Which methods in files with @flow are missing annotations?
  • 30. Lint Flow Test Do the types match up? Do they make sense?
  • 31. server/graph/common/goSocialClient.js:382 382: .then((result: any): Array<T> => { ^^^^^^^^ array type. This type is incompatible with 12: onFulfill?: (value: R) => KewPromise<U> | U, ^^^^^^^^^^^^^^^^^ union: type application of identifier `KewPromise` | type parameter `U` of call of method `then`. See lib: style-guide/flow-interfaces.js: 12 Member 1: 12: onFulfill?: (value: R) => KewPromise<U> | U, ^^^^^^^^^^^^^ type application of identifier `KewPromise`. See lib: style-guide/flow-interfaces.js:12 Error: 382: .then((result: any): Array<T> => { ^^^^^^^^ array type. This type is incompatible with 12: onFulfill?: (value: R) => KewPromise<U> | U, ^^^^^^^^^^^^^ KewPromise. See lib: style-guide/flow- interfaces.js:12 Member 2: 381: return this.request(requestData) ^ type parameter `U` of call of method `then` Error: 382: .then((result: any): Array<T> => { ^^^^^^^^ array type. This type is incompatible with 373: fetchObjects<T: RpcSchema>(Ctor: Class<T>, options: GoSocialRequestOptions): Promise<T> { ^ some incompatible instantiation of `T` When flow complains …
  • 32. Lint Flow Test Do the unit and functional tests still pass? Manual poking
  • 33. type GoSocialRequestOptions = {
 method?: string,
 requestType?: string,
 type?: string,
 path?: string,
 udpEvent?: Object,
 shape?: string,
 query?: Object,
 properties?: Object,
 } {} as a parameter
  • 34. What makes this project challenging?
  • 35. We needed to figure out the dependency graph.
  • 36. LoginService AuthManager UserManager Depends on … Depends on … Why the dependency graph matters
  • 37. Our graph 😭 😭 😭
  • 38. The method docs are out of date.
  • 39. Remember this? /**
 * @return {Promise<Array<Post>>} A sample of 
 * the user's posts 
 */
 getLatestPosts(userId) {
 if (!userId) {
 return Q.resolve(null)
 }
 // More code here to fetch posts and
 // return a Promise
 }
  • 40. We have to define interfaces for third party libraries
  • 41. // Source: https://github.com/moment/moment/blob/develop/moment.d.ts
 declare class Moment {
 subtract(val: number, unit: string): this;
 add(amount: number, unitOfTime: string): this;
 diff(b: Moment, unitOfTime?: string, round?: boolean): number;
 startOf(unit: string): this;
 valueOf(): number;
 }
 
 declare module 'moment' {
 declare function utc(): Moment;
 declare function exports(): Moment;
 }
  • 42. Mistakes are buried layers deep - changes need to be made that don’t result in unintended consequences
  • 44. One more thing: nulls.
  • 45. create(tag: Tag): Promise<?Tag> This value is NOT allowed to be null
  • 46. create(tag: Tag): Promise<?Tag> This value is allowed to be null!
  • 47. create(tag: Tag): Promise<Tag> {
 let tag = null;
 
 if (someCondition) {
 tag = new Tag()
 }
 
 return Q.resolve(tag)
 }
 No ‘?’ to show that return type is nullable ERROR
  • 48. create(tag: Tag): Promise<?Tag> {
 let tag = null;
 
 if (someCondition) {
 tag = new Tag()
 }
 
 return Q.resolve(tag)
 }
 ‘?’ shows that return type is nullable Correct!
  • 49. Now everything this touches • Has to null check (via a conditional) - subtle changes in how code works • Or accept a type ?Tag, which means “nullable type” • Adding to existing code can be tricky without introducing subtle changes
  • 50. But this is also good • Makes us really think about which methods will always have a valid object, which will not • Will lead to better API design long term
  • 51. But it can be confusing function myNullableString(input: ?string): number {
 return input.length;
 }
 
 function callerOfMyNullableString(input: string): number {
 return myNullableString(input);
 }

  • 52. function myNullableString(input: ?string): number {
 return input.length;
 }
 
 function callerOfMyNullableString(input: string): number {
 return myNullableString(input);
 }
 input: ?string input: string This is valid
  • 53. This is not valid function myArray(input: Array<?string>): number {
 return input.length;
 }
 
 
 
 
 function callerOfMyArray(input: Array<string>): number {
 return myArrayWithNullableObjects(input);
 }
  • 54. Here’s why function myArray(input: Array<?string>): number {
 input.append(null);
 return input.length;
 } function callerOfMyArray(input: Array<string>): number {
 return myArrayWithNullableObjects(input);
 } No longer valid!
  • 55. Organizational challenges • General organization buy-in • Flow is almost an entirely new language that people need to learn • Working against a moving target • Funny things that happen after merges
  • 56. To recap • We are finding it hard to scale development without types • Adding types to untyped code has a number of challenges, technical and otherwise • We believe the payoff will be totally worth it.
  • 57. Thank you! • Thanks to Kelly, Nick, Gianni for being great teammates and teaching me everything • Thanks to Madeline for organizing this great event! • Image source: unsplash.com
  • 58. Q&A • If you come up with someone, just ask one of us! • Twitter/Medium: @ikai