Looking through the code apple generates for core data I found this bit of code
[__persistentStoreCoordinator release], __persistentStoreCoordinator = nil;
I understand the purpose for it but is there any purpose for writing the code like this instead of just like this
[__persistentStoreCoordinator release];
__persistentStoreCoordinator = nil;
or is it just being clever?
It’s purely a stylistic choice, but one that expresses intent. In this case, it’s expressing the intent that the release and the nil assignment are one single “operation” and should be kept together. As two separate lines, you are free to insert lines between them, but as a single statement using the comma operator it takes far more work to split them up.
It’s also helpful to the reader in that they don’t have to parse two separate lines, figure out what they’re doing, and understand that it’s just trying to clear out that ivar. Instead as one statement, the reader can quickly recognize this pattern (once they’ve seen it a few times) and it produces less cognitive load on the user. For example, here’s a
-deallocusing separate statementsHere’s the equivalent:
Not only is this fewer lines of code, but the pattern is immediately obvious to the reader. It also looks more aesthetically pleasing, with every line being a method call followed by a nil assignment.