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
@propertyto 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:
There’s also a side-case for some types of booleans properties, where “is” is used as a prefix for readability, e.g.
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:].