Skip to content

[gardening][(JSON|Plist)Encoder] Prefer !isEmpty over count > 0 #15335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2018

Conversation

ikesyo
Copy link
Member

@ikesyo ikesyo commented Mar 19, 2018

No description provided.

@ikesyo
Copy link
Member Author

ikesyo commented Mar 19, 2018

@swift-ci Please smoke test

@ikesyo
Copy link
Member Author

ikesyo commented Mar 19, 2018

@swift-ci smoke test

@ikesyo
Copy link
Member Author

ikesyo commented Mar 19, 2018

The failure looks unrelated.

@swift-ci Please clean smoke test Linux platform

@jrose-apple jrose-apple requested a review from itaiferber March 19, 2018 18:43
Copy link
Contributor

@itaiferber itaiferber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to put this together; the change itself looks okay, but is there a concrete reason for the code churn?

@ikesyo
Copy link
Member Author

ikesyo commented Mar 20, 2018

@itaiferber I'm a fan of using isEmpty over checking count as described in the doc comments of Collection.count:

  /// The number of elements in the collection.
  ///
  /// To check whether a collection is empty, use its `isEmpty` property
  /// instead of comparing `count` to zero. Unless the collection guarantees
  /// random-access performance, calculating `count` can be an O(*n*)
  /// operation.
  ///
  /// - Complexity: O(1) if the collection conforms to
  ///   `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
  ///   of the collection.
  var count: Int { get }

Or the doc comments is not right or is misleading? Is that only about count == 0 case and not about count > 0?

For the cases in this PR the collections are random-access ones so there should be no differences actually, but using isEmpty consistently would be a good habit I think.

@ikesyo
Copy link
Member Author

ikesyo commented Mar 20, 2018

Ref: #15326

@ikesyo
Copy link
Member Author

ikesyo commented Mar 20, 2018

See also #12504 (comment) and #12638.

@ikesyo
Copy link
Member Author

ikesyo commented Mar 20, 2018

Please let me know if I have overlooked something 😃

@harlanhaskins
Copy link
Contributor

@itaiferber At the very least, changing the instance of stringKey.count > 0 in _convertToSnakeCase is taking it from an O(n) operation to an O(1) operation.

@itaiferber
Copy link
Contributor

From a correctness perspective, I don't disagree — it's just that our collections offer O(1) count and JSON keys are rarely long (and overwhelmingly often ASCII), so O(n) vs. O(1) won't make much of a difference there. Just trying to find a balance between correctness and code churn.

@itaiferber itaiferber merged commit feae8bd into swiftlang:master Mar 20, 2018
@itaiferber
Copy link
Contributor

Please make a similar change over on https://github.com/apple/swift-corelibs-foundation, if you don't mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants