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
Standardize Property Declaration Style in Core Classes #870
Conversation
@Adlai-Holler thanks for taking the initiative to propose and fix up the project! I'm curious if others have any style feedback; I haven't given it too much thought, but honestly am happy with anything consistent. So, you have my vote on this if others agree with the style (we should discuss any small tweaks if folks think of them). There is one thing, though, which is entirely about semantics and not style: how we can achieve consistency and safety in handling transactionalized / locked properties. As of now, this is locking, since atomic is insufficient for correct behavior in our primary use cases. We agree that atomic properties are fast (per your research / proof of this), which means we can use them anywhere they are useful. However, we need to carefully consider how to safely use them. It has become clear in a number of cases that using atomics is insufficient for even fairly trivial usages, where any other locked data (at all) is involved. In such cases, not only the usage of the atomic needs to be locked anyway, but also the setter method for the atomic needs the lock... For this reason, I'd suggest we do atomic in a separate pass at the same time as a quick writeup / documentation of the threading rules (which we already have a number of established). We'd need to equip engineers with some limited / brief guidance on how atomics can be safely used. In particular though, I don't think there is value in atomic-by-default, because all meaningful objects (including ASDisplayNode, ASTextNode, and ASImageNode) need to continue locking the setters, getters, and usages of all properties (atomic or not). Now, if the properties are implemented with custom getters and setters in order to put in the locking, I suppose the nonatomic vs atomic makes no functional difference? If that's correct, then it doesn't really matter — I hesitate a bit because atomic won't indicate whether it is object-locked or just property-locked. I wish we could solve that ambiguity somehow, but using atomic seems fine as long as we are not relying on the property-level atomicity unless we're certain it's safe. Thanks again for working on this kind of stuff. It's makes a real difference for the project. |
That's exactly the case. Atomic is necessary but not sufficient, and it only affects compilation if you use synthesized accessors. We should document any extra requirements or guarantees about a property, but it's simply incorrect to declare it For instance, CALayer's properties are all declared |
Actually as I go through all these, I think I want to reverse course and leave out There is a warning available for using the implicit Since virtually all of our properties are going to be atomic, having the extra word all over the place is noise. And it's great to call out the nonatomic properties by having longer declarations IMO. So it would be like:
Less is more. |
Thanks for taking this on, @Adlai-Holler. Consistency is indeed crucial. On atomicity, I'd agree to leave out
With this new convention, I think it's important to weed out all And we should update our Coding Guidelines accordingly. |
I'm also in on all of these changes. As @appleguy says they bring up questions about how best to indicate what kind of thread safety mechanisms are being used internally, but all your changes should simply move us closer to this goal. Thanks @Adlai-Holler ! |
As for how to document threading requirements/guarantees beyond atomic/nonatomic, I think we could make empty macros like:
Takes? @garrettmoon @nguyenhuy @appleguy |
The remaining license header warnings are false positives. We use a different license header for YYText. |
I like it, though it doesn't help when reading calls to these methods… I wish there was a way to do that too without pollutting the name of the method too much. |
Generated by 🚫 Danger |
EDIT I've proposed getting rid of explicit
atomic
further down in the comments and given my rationale.OK folks this one won't be fun but let's bang it out.
I propose this as the property declaration style for Texture:
class
, if applicablenullable
onweak
properties even though it's implied. Nullability and storage class are separate concerns.strong
for objects,copy
for blocks, andassign
for primitives.readwrite
.So
(nullable, atomic, readonly)
is good.(atomic, readwrite, assign)
is bad.Other good examples:
✅
(nullable, atomic) id
✅
(atomic) NSInteger
✅
(nonatomic, copy) NSString *
✅
(atomic, readonly) NSInteger
Nonexamples:
🚫
(atomic, nullable) id
🚫
id
🚫
(nonatomic, assign, readwrite) NSInteger
Thoughts from folks? @garrettmoon @nguyenhuy @maicki @appleguy ?