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

Tim Vermeulen tvermeulen at me.com
Tue Oct 25 12:02:54 CDT 2016


I don’t really mind the “guard” keyword after having used it hundreds of times, but it didn’t really make a lot of sense to me to start with (I’m not a native speaker). “ensure” definitely conveys the meaning better, in my opinion. I’d be in favor of this change.

I bet most people won’t think it’s worth it to to rename it now. It’s kind of a pain for people that come across articles mentioning guard, though Xcode could assist with this change. If “ensure” was current day syntax and someone would now propose to change it to “guard”, I’m almost certain nobody would really take them seriously (but that may just be my unfamiliarity with the English language).

> I mentioned this in passing on adifferent 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 toguardagainstinvalidvalues.
> Or to put it another way, toensurethat we havevalidvalues.
> 
> However, the way it reads in Swift, weguardthat we havevalidvalues.
> 
> I fully understand and agree that it's better to state thetruecondition forvalidvalues here, usingelseto 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?
> 
> 
> 
> 


More information about the swift-evolution mailing list