Unit 2 Quizzies¶
Unit 2 Pathway 1 Quizzies¶
When using lambda expressions with one parameter, what are some considerations when deciding whether to use the implicit
itparameter, or to provide a descriptive parameter name?#solulu
If it’s very obvious what the parameter means, e.g.,
repeat(5) { println("Count: $it") }, it can keep the code concise. However, if the parameter’s meaning is not obvious, naming it something else can improve readability.If the lambda body is longer or more involved body, a descriptive name can improve readability.
If the
itis nested, it can be harder to understand what it refers to. Example:@Composable fun WoofApp() { Scaffold { it -> LazyColumn(contentPadding = it) { items(dogs) { DogItem( dog = it, modifier = Modifier.padding(dimensionResource(R.dimen.padding_small)) ) } } } }
What are some strengths and weaknesses of using trailing lambda syntax?
#solulu
Strengths:
Improves readability by reducing parentheses clutter.
Feels more like HTML.
Weaknesses:
Confuses Kotlin beginners with C/Java background because it looks a lot like a function definition.
May be more difficult to read when functions have multiple lambda parameters. Example:
fun handleEvents(onClick: () -> Unit, onLongClick: () -> Unit) { onClick() onLongClick() } fun main() { // Which lambda is for onClick and which for onLongClick can be unclear. handleEvents({ println("Clicked") }) { println("Long clicked") } }
Unit 2 Pathway 2 Quizzies¶
The codelab places the dice-rolling logic
(1..6).random()inline in the Button’sonClicklambda, not in a separate function. What are some considerations that might influence the choice of placing code inline vs in a separate function?#solulu
Code length. Shorter lambda body âžś more suitable to be inline.
Reuse. More times used in the code âžś more suitable to be separate function.
Searchability. Inline lambda âžś instantly see the code that runs when the button is clicked, no need to scroll to a different part of the code.
Clutter. Inline lambda âžś reduces total number of function declarations.
Unit 2 Pathway 3 Quizzies¶
What are the strengths and weaknesses of using state hoisting?
#solulu
Strengths
Separation of concerns: composables mainly responsible for displaying UI elements, other code is responsible for the app logic.
Better code overview: easier to see all state variables at a glance, since they are in a central location. Example:
@Composable fun ParentComponent() { // State is hoisted to the parent component, all state variables visible at a glance. var count by remember { mutableStateOf(0) } var isEnabled by remember { mutableStateOf(true) } var userName by remember { mutableStateOf("") } Column { ChildComponent( count = count, isEnabled = isEnabled, userName = userName, onCountChange = { count++ }, onEnableChange = { isEnabled = !isEnabled }, onUserNameChange = { userName = it } ) } } @Composable fun ChildComponent( count: Int, isEnabled: Boolean, userName: String, onCountChange: () -> Unit, onEnableChange: () -> Unit, onUserNameChange: (String) -> Unit ) { Column { Button(onClick = onCountChange, enabled = isEnabled) { Text("Increment Count") } Button(onClick = onEnableChange) { Text(if (isEnabled) "Disable" else "Enable") } Button(onClick = { onUserNameChange("New User") }) { Text("Change User") } } }
Weaknesses:
May lead to parameters being passed down multiple levels of the hierarchy, even if some levels don’t use them. In React, this is called “prop drilling”. Example:
@Composable fun ParentComponent(data: String) { ChildComponentA(data) } @Composable fun ChildComponentA(data: String) { // Doesn't use data, passes it down to ChildComponentB ChildComponentB(data) } @Composable fun ChildComponentB(data: String) { // Doesn't use data, passes it down to ChildComponentC ChildComponentC(data) } @Composable fun ChildComponentC(data: String) { Text(text = data) }
What are some scenarios when it may be better for a composable to be stateful, not stateless?
#solulu
When state is localized to that composable only, not shared with other composables.
For small components (like toggle buttons) - may make them simpler and more self-contained. Example:
@Composable fun ToggleButton() { var isOn by remember { mutableStateOf(false) } Button(onClick = { isOn = !isOn }) { Text(if (isOn) "ON" else "OFF") } }
What are some considerations when deciding whether to use a local test or an instrumentation test?
#solulu
Local Tests:
Run on the computer, fast, ideal for testing business logic or utility functions.
No dependencies on the Android OS, making them more isolated from Android OS issues.
Isolated from other apps.
Instrumentation Tests:
Run on an actual device or emulator, testing UI and integration with Android components.
Useful for verifying behaviors that depend on the Android framework or system interactions.
Affected by other apps on the device or emulator.