thinking reactive
TRANSCRIPT
Imperative
Write correct sequence of codes in the correct order. Step by step. (HOW)
Declarative
Write code to describes WHAT you want to do.
Example 1
Search API as you type but:
- Don't spam API server every seconds- Only make request if keyword changed- Cancel previous requests
Example 2
When the user starts simultaneously panning and rotating an object, start a countdown from 3.
Stop the timer either when the countdown ends or when the user stops the gestures.
Imperative
- When the user touches the screen - Check if a user is panning an object, store that information - Check if both gestures are running simultaneously - Start a timer, from 3, counting down. - Check if the user is rotating an object, store that information - Check if both gestures are running simultaneously - Start a timer, from 3, counting down. - Check if the user stopped panning - Stop the timer if needed - Check if the user stopped rotating - Stop the timer if needed
- When the timer ticks, decrease the number of seconds left - If the number of seconds left is zero, stop the timer*
Declarative
Define condition “simultaneously `panning` and `rotating`”Define condition “start a countdown from `3`”Define condition “when the user stops the gestures”Define what a timer is
When the user starts simultaneously `panning` and `rotating` an object, start a countdown from `3`.Stop the timer either when the countdown ends or when the user stops the gestures.”
Advantages• Code more concise, easy to read, easy to understand
without context
• Make asynchronous easier
• Concurrency, thread safety
• Eliminate state from your code
• Has standard mechanism for error recovery
• Support Data Binding
• Maintainability, Extensibility, Testability...
Denial• FRP is hard, imperative is more easy
• FRP just is trend!
• RxSwift is just another TOOL
• Unnecessarily complex