Code Grammer

Discussion in 'Mac Programming' started by larswik, Aug 23, 2010.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I have a question about a part of a book I am learning from.

    The line is
    Code:
    -(void) setWidth: (int) w andHeight: (int) h;
    I understand that 'h' is the argument of type '(int), then the ':' allows 'Height', the variable, to except that argument. That is the code that I write in my @implementation section. My question is with the words SET and AND, Are they just in the code to make it more readable or are they needed to execute the code correctly. Could it also be written like this?

    Code:
    -(void) Width: (int) w Height: (int) h;
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    All of the text except the '-', the types, and the ':'s is "ornamental" in the sense that it's only there for readability and to provide a unique identifier.

    Code:
    - (void) set:(int)w :(int)h;
    would be valid, but horrible
    Code:
    - (void) :(int)w :(int)h;
    might even be valid, I can't remember.
    Code:
    - (void) width:(int)w height:(int)h;
    would also be valid, but wouldn't be idiomatic.

    fwiw I would probably declare this method as

    Code:
    - (void) setWidth:(int)w height:(int)h;
    since the 'and' is usually omitted.

    or, best of all:

    Code:
    - (void) setSize:(NSSize)size;
    Apple has guidelines for naming things here:

    http://developer.apple.com/mac/libr...l/CodingGuidelines/Articles/NamingBasics.html

    http://developer.apple.com/mac/libr.../CodingGuidelines/Articles/NamingMethods.html
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    Thanks catfish...

    I will look over those links. So is the word SET over looked by the compiler then, or what I have been calling an 'ignore' word.

    I could also write it like this then using 2 SET words.

    Code:
    -(void) setWidth: (int) w setHeight: (int) h;
     
  4. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #4
    The word "set" is not a separate token in that. The compiler doesn't divide things at capitalization changes. What the compiler sees is roughly (using [] to separate chunks):

    [ - ] [ (type) ] [ name ] [ : ] [ (type) ] [ argName ] [ moreName ] [ : ] [ (type) ] [ argName ]

    It doesn't make decisions on compiling your code based on partial pieces of those chunks like "set".

    There *are* a few things that do look at the actual naming of things. Key-Value Coding will look for a correctly named setter function to do its work, and the clang static analyzer understands how Cocoa idioms apply to memory management.

    Finally, understanding the pieces of an Objective-C method name is important when you need to refer to them via selectors. To get a selector for your original method you would do:

    Code:
    @selector(setWidth:andHeight:);
     
  5. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
  6. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #6
  7. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #7
    In the context of this thread, robvas is correct. This thread has been about syntax, not grammars. A grammar is a formal set of rules used to parse the syntax of a given language, which one would rarely discuss unless they were writing a compiler (or a parser to be exact).
     
  8. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    There are a few things that I don't understand, English not being my first language. What do you mean by "to except that argument"? I didn't know "except" was a verb. Do you mean "to exclude the argument" or "to mark it as exceptional"? This doesn't make sense.

    And that should answer your question: "set" and "and" are not _just_ in the code to make it more readable. They are there to make it _more readable_, and that is very, very important. In my company they would be essential to pass a code review, which means without them you'd have to rewrite the code to get it accepted, and your collegues would be wondering what is wrong with you to think that code that doesn't clearly express its intention might be acceptable.

    Anyway, could you try to repost your question in correct and precise English, because I have a vague idea that there are a few things that you don't understand correctly about Objective-C, but I won't start making wild guesses.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    "except" should be "accept", to make sense.
     
  10. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #10
    Please correct me on this:

    If you use @property with @synthesize, the symbolic language stops being entirely ornamental. If you synthesize the above property "width" (not as read-only), the compiler will generate a method equivalent to
    Code:
    -(void)setWidth:(int)w
    {
    width = w;
    }
    ( This, of course, would not in any way affect the method -setWidth:andHeight: )

    I have not used properties, so I am not clear on all the subtleties. If you synthesize a property but still write a setter method, I believe your setter method would override the synthesized method as long as the method is declared in the .h file - if you write an undeclared setter method, I would expect a compiler error, but doing so would not make much sense anyway.

    And "Grammer" is an actor who played Frasier Crane for many many years.
     

Share This Page