[swift-evolution] [pitch] rename 'guard' to 'ensure'

Marco Pace drive at marcopace.it
Tue Oct 25 12:02:40 CDT 2016


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."

That’s a good point, but I think it depends on the way you read it. I’m a non-native English speakers and the way a read the guard is different. Using a simple example:

guard x > 0 else {
    return
}

I don’t read “Guard that x is higher than 0 else … ”, but instead I read it like “Guard! x must be higher than 0 else …”.

If you read it like the first sentence I agree that it sounds strange and that another word, like ensure, sounds better. 
If you read it like that second sentence the word guard makes more sense.

Probably adding a colon could make it more clear to the reader:

guard: x > 0 else {
    return
}

But then we risk to add complexity to the syntax.

Marco


On 25 October 2016 at 18:38:50, Jay Abbott via swift-evolution (swift-evolution at swift.org) wrote:

I mentioned this in passing on a different thread. 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?

_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161025/419778e5/attachment.html>


More information about the swift-evolution mailing list