Reputation: 24750
I want all my instance variables private and accessors/mutators in public methods. I am wondering if there is a naming convention that I should be aware of when building large classes, a convention both for readability among other developers, but also a convention that prevents method name clashing with other classes.
For example, if I want a class to access and change a "quality" (not necessarily a specific variable) called "supports musicians," would [myInstance setSupportsMusicians:YES]
and [myInstance returnSupportsMusicians]
be acceptable names, using set
and return
as method name prefixes for all other mutators and accessors?
Obviously I know I can name them anything I want, but I wanted to get some opinions since I know naming conventions are a significant component of organized development.
Upvotes: 3
Views: 2334
Reputation: 2018
You'd be best off using @property
to declare things like this. That way you can get all its implementation benefits (like atominicity, automatic ivar generation, etc) along with convenient dot syntax (e.g. myInstance.supportsMusicians = YES
), all without having to worry about the underlying method names at all.
But in case you do want to declare your methods manually, or just want to know what the auto-generated ones are, the naming convention is:
- (void)setSupportsMusicians:(BOOL)supportsMusicians;
- (BOOL)supportsMusicians;
There's also a side-case for some types of booleans properties, where "is" is used as a prefix for readability, e.g.
- (BOOL)isVisible;
This is not followed universally, however, and might be considered a legacy convention.
Note that "get" as a prefix shouldn't be used randomly as it has a specific meaning: it's used in a context where the caller provides a buffer to be filled in, e.g. -[NSString getBytes:length:]
.
Upvotes: 4
Reputation: 46543
set and get are Java & few other language style.
But cocoa style is as (Pseudocode, for names):
setName() in cocoa is setName:
getName() in cocoa is name:
EDIT: this is only for naming convention, dont take it as a syntax for methods.
Few helpful links :
http://cocoadevcentral.com/articles/000082.php
Upvotes: 2