Joshua Smith
Joshua Smith

Reputation: 1004

What is the purpose of this line of code?

Consider this snippet of Objective-C:

NSString *elementString = nil;

if(elementText != NULL) {
    elementString = [NSString stringWithCString:(char*)elementText encoding:NSUTF8StringEncoding];
    [elementString self]; 
    xmlFree(elementText);
}

I understand that [elementString self] returns a reference to elementString. My question is, since that return value isn't being used here, why would [elementString self] be called at all?

This pattern is repeated many times throughout the XML parsing routines in an application I'm maintaining. If it only happened once I would just suspect somebody did something silly, but given its prevalence I'm wondering what I'm missing.

Edit: The answer given by Benedict Cohen is correct. The elementString variable isn't ever used. Most instances of this pattern are buried in multi-hundred line methods and I didn't see that it was never used.

Upvotes: 3

Views: 115

Answers (1)

Benedict Cohen
Benedict Cohen

Reputation: 11920

Without the compiler would issue a warning that elementString is not used.

Edit: Actually, due to the way that elementString is declared and then set I don't think this would raise a warning....

Edit: Just tested it. [elementString self] isn't suppressing a warning. My only guess is that it was once included to suppress a warning but the code has been refactored and it's no longer necessary.

Upvotes: 4

Related Questions