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.
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:].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With