5 ms·
I agree that you can split validation parts into separate functions but this is conflating two concerns, for example the rewriting of the guard statement from
by alexjarvis 11y ago
I agree that you can split validation parts into separate functions but this is conflating two concerns, for example the rewriting of the guard statement from
guard item.count > 0 else {
throw VendingMachineError.OutOfStock
}
to
if count == 0 {
throw VendingMachineError.OutOfStock
}
only proves that you can refactor the code to not use guard whereas the article starts by talking about small functions, where you could in fact still use guard in this case.
- rimantas 11y agoYes. And if all your code is composed of three-lines methods, then ok. Otherwise guard is extremely usefull, because unlike 'if' it leaves your constant/variable accessible outside the guard scope.