conventions

Swift: guard vs if let

ⅰ亾dé卋堺 提交于 2019-11-26 01:13:23
问题 I have been reading about Optionals in Swift, and I have seen examples where if let is used to check if an Optional holds a value, and in case it does – do something with the unwrapped value. However, I have seen that in Swift 2.0 the keyword guard is used mostly. I wonder whether if let has been removed from Swift 2.0 or if it still possible to be used. Should I change my programs that contain if let to guard ? 回答1: if let and guard let serve similar, but distinct purposes. The "else" case

When to use double or single quotes in JavaScript?

a 夏天 提交于 2019-11-25 22:15:09
问题 console.log(\"double\"); vs console.log(\'single\'); I see more and more JavaScript libraries out there using single quotes when handling strings. What are the reasons to use one over the other? I thought they\'re pretty much interchangeable. 回答1: The most likely reason for use of single vs double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string. Using the other type of quote as a literal: alert('Say

Is it a good idea to typedef pointers?

白昼怎懂夜的黑 提交于 2019-11-25 22:14:05
问题 I looked through some code and noticed that the convention was to turn pointer types like SomeStruct* into typedef SomeStruct* pSomeStruct; Is there any merit to this? 回答1: This can be appropriate when the pointer itself can be regarded as a "black box", that is, a piece of data whose internal representation should be irrelevant to the code. Essentially, if your code will never dereference the pointer, and you just pass it around API functions (sometimes by reference), then not only does the

Is there a standardized method to swap two variables in Python?

南楼画角 提交于 2019-11-25 22:07:31
问题 In Python, I\'ve seen two variable values swapped using this syntax: left, right = right, left Is this considered the standard way to swap two variable values or is there some other means by which two variables are by convention most usually swapped? 回答1: Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side. http://docs.python.org/3/reference/expressions.html#evaluation-order That means the