kodecocodes / objective-c-style-guide

A style guide that outlines the coding conventions for Kodeco
http://www.raywenderlich.com/62570/objective-c-style-guide
3.1k stars 628 forks source link

Move `const` after the type for consistency. #30

Closed gregheo closed 10 years ago

gregheo commented 10 years ago

For pointers to things like NSString, the const needs to be after the *. Reading the declaration from right to left, static const NSString *foo means "foo is a pointer to an NSString that is constant". i.e. the NSString data is constant, but NSStrings are already immutable. That means the pointer foo can be reassigned to some other string.

What we want is static NSString * const foo which means "foo is a constant, unchangeable pointer to an NSString".

This next point is debatable, but for consistency, I suggest always putting the const after the type for primitive types too. Technically, const is supposed to modify what precedes it.

Reference: http://stackoverflow.com/questions/7526152/easy-rule-to-read-complicated-const-declarations