[swift-evolution] [pitch] rename 'guard' to 'ensure'
Jay Abbott
jay at abbott.me.uk
Tue Oct 25 11:38:50 CDT 2016
I mentioned this in passing on a different thread
<https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20161024/028371.html>.
Although it caused some slight confusion when I first learned the 'guard'
keyword, it was easy to overcome and I didn't think the issue was strong
enough to warrant a change. However, after reading the other discussion on
that thread (around the possibility of a 'matches' keyword and how code is
"read out" or "internally verbalised"), and seeing how passionate everyone
here is about Swift, I'm thinking that maybe it should be changed.
The issue is that in English, 'guard' has an inverted logical connotation
to how it is used in Swift. For example, take this sentence:
"In some functions it's a good idea is to put some checks in place, in
order to guard against invalid input values."
Swift's 'guard' keyword is to *guard* against *invalid* values.
Or to put it another way, to *ensure* that we have *valid* values.
However, the way it reads in Swift, we *guard* that we have *valid* values.
I fully understand and agree that it's better to state the *true* condition
for *valid* values here, using *else* to perform a contingency. And I
understand why the word 'guard' was picked (given the above example
sentence). I just think that 'ensure' would be better.
ensure x > 0 else {
return
}
ensure camp.isSafe else {
runForTheHills()
}
ensure let thingy = object as? Thingy else {
return ExamineThingyResult.NotAThingy
}
As I said, I thought it was too small a niggle to worry about, but seeing
how people on here care so much about making Swift better, and the
willingness to change things, I thought I'd suggest it. Although it would
be a breaking change, the code changes are easy.
So what do people think? In particular it would be interesting to hear from
non-native English speakers on this, did 'guard' confuse you or did 'guard'
cause your understanding of the English word to be confused?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161025/211aafcd/attachment.html>
More information about the swift-evolution
mailing list