Problem overview
The method legalAge takes an integer age and returns true if the age is legal, meaning at least 18, and false otherwise. The lesson asks for it in a single line, with no if or else.
The long way
The obvious version tests the condition and returns a Boolean for each outcome:
def legalAge(age: Int): Boolean = {
if (age >= 18) {
return true
} else {
return false
}
}
It works, but look at what it does: it checks whether a Boolean is true and then returns that very Boolean. The if adds five lines and no information.
The idea
A comparison such as age >= 18 is an expression whose value is already true or false. That value is exactly what the method should return, so there is nothing for an if to decide. Whenever you see if (cond) true else false, you can replace it with cond. The reverse pattern, if (cond) false else true, is !cond.
Step by step
- Write the comparison. "At least 18" is
age >= 18. Using>would exclude an age of exactly 18. - Return it directly. The whole body is one line.
The solution in Scala
class Solution {
def legalAge(age: Int): Boolean = {
return age >= 18
}
}
The Scala details:
- Precedence. The comparison is evaluated first, and
returnsends back its result. No parentheses are needed. - Dropping
return. The last expression of a method is its value, sodef legalAge(age: Int): Boolean = age >= 18is equivalent and is the usual Scala style. The lesson keepsreturnand the braces, which also compile. - The type is
Boolean. The result of>=on two integers is aBoolean, which matches the declared return type.
Combining conditions
The same trick works with &&, || and !. A method that decides whether someone is a teenager can return the combined condition directly:
def isTeen(age: Int): Boolean = age >= 13 && age <= 19
It returns true for 13, 16 and 19, and false for 12 and 20.
Flipping a result with !
When the long form returns false first, negate the condition. This method answers the opposite question, whether someone is a minor:
def isMinor(age: Int): Boolean = !(age >= 18)
It returns true for 17 and false for 18. Often you can simplify further by flipping the operator, as in age < 18, which gives the same answer for every integer.
Complexity
The method does a single comparison, so it takes constant time and constant extra space. Removing the if doesn't change the speed; it makes the code shorter and easier to read.
Why it matters
Each branch you remove is one less place for a mistake. In the long version, someone could return true in both branches by accident and the code would still compile. With age >= 18 there is one expression to read, and it states the rule directly. The shorter form also reads like the rule itself: "legal age is at least 18".
Edge cases and common mistakes
- Exactly 18.
legalAge(18)istrue. With>it would befalse. - Zero or negative ages.
legalAge(0)andlegalAge(-5)returnfalse. The method doesn't validate input; it only answers the comparison. - Comparing the result with
true. Writing(age >= 18) == trueis the same redundancy in another form. - Wrong direction.
age <= 18is true for the children and wrong for the adults. Check one value on each side of the boundary.
Testing
legalAge(17)givesfalse.legalAge(18)givestrue.legalAge(19)givestrue.legalAge(0)givesfalse.
