[pitch] rename 'guard' to 'ensure'


(Marco Pace) #1

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jay) #2

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 …*”.

I see, yes I hadn't considered this interpretation...

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.

I actually like this better than ensure - it makes a lot of sense. It would
also force the second interpretation as you mentioned above. If 'guard' was
simply renamed to 'guard:' (or depending on how you look at it, required to
have a colon after it), how would it complicate the syntax?