Why expressions over statements matters in Kotlin - Performance Analysis
We want to see how choosing expressions instead of statements affects how long code takes to run.
Does using expressions change how the program grows with bigger inputs?
Analyze the time complexity of the following code snippet.
fun maxOfTwo(a: Int, b: Int): Int {
return if (a > b) a else b
}
fun maxOfTwoStatement(a: Int, b: Int): Int {
var max: Int
if (a > b) {
max = a
} else {
max = b
}
return max
}
This code compares two numbers and returns the bigger one, once using an expression and once using statements.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single comparison between two numbers.
- How many times: Exactly once per function call.
Since the functions only compare two numbers once, the work stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 2 | 1 comparison |
| 10 | 1 comparison |
| 1000 | 1 comparison |
Pattern observation: The number of operations does not grow with input size here.
Time Complexity: O(1)
This means the time to run the code stays the same no matter how big the input is.
[X] Wrong: "Using expressions always makes code faster than statements."
[OK] Correct: Both expressions and statements can run in the same time; the difference is often about style and clarity, not speed.
Understanding how expressions and statements affect code helps you write clear and efficient programs, a skill valued in many coding challenges and real projects.
"What if we compared three numbers instead of two? How would the time complexity change?"
Practice
Solution
Step 1: Understand the difference between expressions and statements
Expressions produce values and can be used wherever a value is expected, while statements perform actions but do not return values.Step 2: Identify why expressions are preferred
Using expressions allows writing clearer and more concise code by directly returning values without extra variables or statements.Final Answer:
Because expressions return values and can be used anywhere a value is needed -> Option AQuick Check:
Expressions return values = C [OK]
- Confusing speed with value return
- Thinking statements always cause errors
- Believing expressions don't need variables
if statement?Solution
Step 1: Review Kotlin's expression syntax for
Kotlin allowsififto be used as an expression that returns a value, using parentheses and else branch.Step 2: Identify the correct syntax
val result = if (x > 0) "Positive" else "Non-positive" correctly assigns the result of theifexpression to a variable. Other options have syntax errors or incorrect usage.Final Answer:
val result = if (x > 0) "Positive" else "Non-positive" -> Option CQuick Check:
Correctifexpression syntax = B [OK]
if expressions [OK]- Omitting parentheses around condition
- Using incorrect keywords like 'then'
- Trying to assign without else branch
val max = if (10 > 5) 10 else 5 println(max)
Solution
Step 1: Evaluate the
The conditionifexpression condition10 > 5is true, so the expression returns 10.Step 2: Assign and print the value
The variablemaxis assigned 10, andprintln(max)outputs 10.Final Answer:
10 -> Option BQuick Check:
Condition true returns 10 = A [OK]
- Confusing output with false branch
- Expecting boolean output
- Thinking code causes error
val result
if (x > 0) {
result = "Positive"
} else {
result = "Non-positive"
}
println(result)Solution
Step 1: Check variable declaration and initialization
The variableresultis declared without initialization, which is not allowed forvalin Kotlin.Step 2: Understand Kotlin's val initialization rules
valmust be initialized at declaration or in constructor; here it is uninitialized and assigned later, causing error.Final Answer:
Variable 'result' must be initialized before use -> Option DQuick Check:
Uninitialized val causes error = D [OK]
- Thinking val can be assigned later
- Ignoring initialization errors
- Assuming if syntax is wrong
var status: String
if (score >= 60) {
status = "Pass"
} else {
status = "Fail"
}
println(status)Solution
Step 1: Identify the current code structure
The code uses a statement-basedifto assignstatusvariable based onscore.Step 2: Use
Kotlin allowsifas an expression for assignmentifto return a value, so we can assignstatusdirectly with the expression, making code shorter and clearer.Final Answer:
val status = if (score >= 60) "Pass" else "Fail" println(status) -> Option AQuick Check:
Useifexpression for assignment = A [OK]
if result directly to variable [OK]- Using invalid syntax for
ifexpression - Trying to assign without else branch
- Using statements instead of expressions
Ready to prove it?
Pass the test and this pattern moves unknown
