Faux Pas

Avoiding Brent’s Coding Nits

Ali Rantakari
July 21, 2014

Brent Simmons wrote about some issues he found in an unnamed open-source Cocoa project. He lists 12 things, and while reading them I realized that you can use the Clang compiler and Faux Pas to avoid most of them:

“Accessors shouldn’t be of the form getSomething. Drop the get.”

Faux Pas has a rule for this: Unidiomatic accessor naming.

#define SOME_CONSTANT 10 isn’t Cocoa-like. Better is static const NSUInteger PBSSomeConstant = 10; …”

Faux Pas has a rule for this: Macro definition for literal value.

“All of your class names should have a prefix. All of them.”

Faux Pas can warn you about classes with no prefix at all (Unprefixed Objective-C class), or about prefixes that are too short or collide with Apple’s framework prefixes: Reserved symbol prefix.

“Header files should expose only what’s needed by the outside world. Use class extensions to keep private things private.”

The Clang compiler has the -Wobjc-interface-ivars warning for this: “declaration of instance variables in the interface is deprecated”.

Faux Pas has a related rule: Category used for “private” declarations.

“The init method should return instancetype and not anything else.”

Faux Pas has a rule for this: Constructor return type. It can check both convenience constructors and init methods.

“You shouldn’t use self.whatever accessors in init, except when unavoidable …”

Faux Pas has a rule for this: Setter invocation in init or dealloc method.

“Turn on more errors and warnings. (Even consider treat-warnings-as-errors.) …”

Faux Pas has a rule for this: Recommended compiler warning options. The default configuration also recommends treating warnings as errors (-Werror).

“If your code is meant for iOS 7 or Mac OS 10.9, use @import.”

The Clang compiler has the -Wauto-import warning for this.

“If you register for notifications, and there’s any code path where you might not call removeObserver, fix it.”

Faux Pas has a couple of rules that help you find issues related to this: