Wanted for abuse of operators

2006-12-02 13:49:19 -08:00

From the stock app delegate in Apple’s Core Data application template:

[managedObjectContext release], managedObjectContext = nil;
[persistentStoreCoordinator release], persistentStoreCoordinator = nil;
[managedObjectModel release], managedObjectModel = nil;

And in case you were wondering how Apple could possibly ship code that doesn’t even compile in the app templates…

… that’s legal code.

Yes, there is a comma operator, and that’s what’s at work there. There is a warning (-release returns void, whereas assignment of nil returns id, so the types are mismatched, which GCC doesn’t like), but the code is legal.

Legal doesn’t always mean correct, however. The comma operator is supposed to be used for things like this:

printf(“%s\n”, (-‍-argc, *++argv));

Though in modern usage it is out of favor, replaced on sight with less obfuscative code:

printf(“%s\n”, *++argv);
-‍-argc;

printf(“%s\n”, argv[idx++]);

It is not for performing a void function call followed by an assignment. A void expression should never appear in a comma expression. The code would be greatly improved by replacing the comma with a semicolon, dividing each statement into the two statements that it should be.

8 Responses to “Wanted for abuse of operators”

  1. Scott Stevenson Says:

    This is completely off-topic but I can’t get Vienna to read your feed. It says “Error parsing XML data in feed.” Yours is one of only two I found that do this. The feed validator suggests there’s perhaps a minor issue?

  2. Peter Hosey Says:

    Guess it didn’t like my declaration at the top of the file. I find nothing in the XML spec to suggest that it’s illegal, but xmllint doesn’t like it, and apparently neither does Vienna.

    I’ve commented it out, although the feed still doesn’t validate because entity references outside of the standard five are not declared.

  3. Peter Hosey Says:

    Yargh!

    my <!ENTITY> declaration.

  4. Scott Stevenson Says:

    Well, the feed is read by Vienna now, but the excerpts are empty. Just article titles.

  5. Scott Stevenson Says:

    I take that back. The feed is parsed enough for Vienna to get a list of articles, but the excerpts are empty — just titles.

  6. Peter Hosey Says:

    Works now. I had some borken XML in the previous post (unmatched “” in attribute values).

  7. Blake C. Says:

    You didn’t mention how cool the comma operator is when used in a #define to specify a return value. I just learned about that recently. Sorry to post something on-topic :P

  8. Peter Hosey Says:

    Yes, the comma operator does have cool and correct uses. This wasn’t one of them. ;)

Leave a Reply

Do not delete the second sentence.


Warning: Undefined array key "ntt_saved_comment_text" in /home/public/blog/wp-content/plugins/negative-turing-test/negative-turing-test.php on line 143