Pure fun add(a: Int, b: Int): Int { return a + b } fun add(a: Int, b: Int): Int { val result = a + b println(result) return result } Not Pure What’s a Pure function?
Action) = when(action){ is Action.CheckFields-> { if (!action.username.isBlank()) stateMachine.nextState(ValidState(stateMachine)) else stateMachine.nextState(DefaultState(stateMachine)) } is Action.AttemptLogin -> { stateMachine.nexState(LoadingState(stateMachine), Action.AttemptLogin)) } } } class DefaultState(private val stateMachine: StateMachine) { fun onAction(action: Action) = if (action is Action.CheckFields) if (!action.username.isBlank()) stateMachine.nextState(ValidState()) }
Action) { if (action is Action.CheckFields) { if (!action.username.isBlank()) { stateMachine.nextState(ValidState()) } } } } class LoadingState(private val stateMachine: StateMachine) { fun onAction(action: Action) = if(action is Action.AttemptLogin){ async(UI) { val account = bg { authService.signIn(action.credentials}.await() stateMachine.nextState(AuthenticatedState()) } } } class ValidState(private val stateMachine: StateMachine) { fun onAction(action: Action) = when(action){ is Action.CheckFields-> { if (!action.username.isBlank()) stateMachine.nextState(ValidState(stateMachine)) else stateMachine.nextState(DefaultState(stateMachine)) } is Action.AttemptLogin -> { stateMachine.nexState(LoadingState(stateMachine), Action.AttemptLogin)) } } }
find bugs Easy to maintain Easy to add new actions Global Picture of possible actions Logic business is isolated in pure functions Side Effects are isolated ViewController need to figure out the actual state Actions can be dispatched from anywhere No Global Picture of the flow
find bugs Easy to maintain Easy to add new actions Global Picture of possible actions Logic business is isolated in pure functions Side Effects are isolated ViewController need to figure out the actual state Actions can be dispatched from anywhere No Global Picture of the flow State Machine Easy to find bugs Easy to maintain Actions are carried by the state ViewController receive a state Hard to add new states No Global Picture of the flow Business Logic and Side effects are inside the state State are responsible for going to the next state
to find bugs Easy to maintain Easy to add new actions Global Picture of possible actions Logic business is isolated in pure functions Side Effects are isolated Easy to find bugs Easy to maintain Actions are carried by the state ViewController receive a state Cons ViewController need to figure out the actual state Actions can be dispatched from anywhere No Global Picture of the flow Hard to add new states No Global Picture of the flow Business Logic and Side effects are inside the state State are responsible for going to the next state
String inline fun body(f: ()-> Body) { ... } } inline fun html(content: HTML.() -> Unit): HTML { val html = HTML() // create the receiver object html.content() // pass the receiver object to the lambda return html } html { title = "Droidcon Berlin" body { ... } }