• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Coding Standards For Objective C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Coding Standards For Objective C


  • Subject: Re: Coding Standards For Objective C
  • From: Ricky Sharp <email@hidden>
  • Date: Wed, 24 Nov 2004 15:41:10 -0600


On Nov 24, 2004, at 2:32 PM, Scott Stevenson wrote:

On Nov 24, 2004, at 11:58 AM, Ricky Sharp wrote:

e.g.:    float mShadowOffset;
e.g.:    int foo (int inAddend, int inAugend, int& outSum)

(3) Because of the prefix rules above, all local variables would not have any prefix. Thus, locals var names never collided with ivars or parameters.
[...]
Is anyone else doing something similar? Is this to be frowned upon? I have no problem in changing my practices to adhere to the guidelines.

I rarely see it in Code, and Apple's guidelines don't mention it at all. I think the main problem with something like this from Cocoa's persepective is that something like "m" is pretty terse.

I agree. I'll be adopting the guidelines for ivars and not prefix them.

I think I read a recommendation in Apple's docs somewhere that local vars can have the prefix "my" if the name is otherwise the same as an ivar. You can also "the". The point is to make it readable, and "m" isn't ideal for that. Same with "pz" and such.

Thanks for pointing out that recommendation; I think I'll adopt it.

Also, for foo, should I really be calling the accessor everywhere rather than having a local?

- (void)foo
{
    [self someMethod:[self someAttribute]];
    [self anotherMethod:[self someAttribute]];
}

I personally think this is wasteful, but it really depends on how how often -foo is called and the context. Sometimes someAttribute won't be the same from one line to the next.

Agreed.


On Nov 24, 2004, at 3:27 PM, Sherm Pendley wrote:

On Nov 24, 2004, at 2:58 PM, Ricky Sharp wrote:

Also, for foo, should I really be calling the accessor everywhere rather than having a local?

In general, it's a good idea to use accessors any time you need to do more than a simple assignment.


One example when you have an ivar that's an object type. Generally, when you assign it a new value, you retain the new value and (auto)release the old value. Consistently using an accessor in that case will isolate your memory management into one place, making it less likely to have retain/release bugs, and easier to debug if it does.

Another example is threading. If you're using mutexes to coordinate access to an ivar among multiple threads, it's not a bad idea to isolate all of that mutex-related code into accessor methods.

Good points.

Thanks Sherm and Scott for your replies; definitely helps me pick a direction to take.

___________________________________________________________
Ricky A. Sharp         mailto:email@hidden
Instant Interactive(tm)   http://www.instantinteractive.com

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Coding Standards For Objective C (From: "Sanoop" <email@hidden>)
 >Re: Coding Standards For Objective C (From: j o a r <email@hidden>)
 >Re: Coding Standards For Objective C (From: Shawn Erickson <email@hidden>)
 >Re: Coding Standards For Objective C (From: Ricky Sharp <email@hidden>)
 >Re: Coding Standards For Objective C (From: Scott Stevenson <email@hidden>)

  • Prev by Date: Re: Coding Standards For Objective C
  • Next by Date: NSButton w/ Image
  • Previous by thread: Re: Coding Standards For Objective C
  • Next by thread: Re: Coding Standards For Objective C
  • Index(es):
    • Date
    • Thread