Discover the secret to clean and efficient Objective-C code with Stripe's comprehensive Coding Style Guide. This guide is designed to help developers maintain consistency and readability in their code, ensuring a smooth and seamless experience for both team members and users. With a focus on ground rules, variable naming, control flow, documentation, literals, constants, and design patterns, this guide covers every aspect of Objective-C programming to help you write better code, faster.
Stripe's Coding Style Guide emphasizes the importance of clarity, organization, and consistency in code. From proper indentation and spacing to the use of literals and constants, the guide provides detailed instructions and examples to help developers create clean, efficient, and easy-to-understand code. By following these guidelines, developers can ensure that their code is not only functional but also maintainable and scalable.
Objective-C Style Guide
NSString *text
unless it's NSString * const Text
idx
/ jdx
instead of i
/ j
in for loops.url
or urlString
). Otherwise, they should be all caps when occurring elsewhere in the method name, or as a class name (ex: handleStripeURLCallbackWithURL
or STPAPIClient
)_
, e.g. - (void)_doPrivateStuff
and id _internalVariable
. This is not required for private properties which should not include an underscore (this is to distinguish them from their underlying variable which automatically has an _
prefix).else if
and else
on the same line as the preceding closing curly brace:if (condition) return YES;
switch
statements for enums should contain an entry for each value and avoid using default
NSString
, NSDictionary
, NSArray
, NSNumber
:STP
:stripe-ios/Stripe/
stripe-ios/Tests/Tests/
stripe-ios/Example/Basic Integration/Basic Integration/
stripe-ios/Example/Non-Card Payment Examples/Non-Card Payment Examples/
stripe-ios/Stripe/PublicHeaders/
for Cocoapods and Swift Package Manager compatibility@class
for everything elseNS_ASSUME_NON_NULL_BEGIN
/ NS_ASSUME_NON_NULL_END
in headers. NS_ASSUME_NON_NULL_BEGIN
/ NS_ASSUME_NON_NULL_END
should also be used in implementation (.m
) filesstp_
to avoid collision:STPSource.m
.+Private.h
file. Ex: STPSource+Private.h
.assign
, readwrite
, strong
)copy
for classes with mutable counterparts such as NSString
, NSArray
, NSDictionary
@synthesize
and @dynamic
on separate lines for shorter diffsself.foo
) instead of their corresponding instance variables (_foo
). Instance variables should only be accessed directly in initializer methods (init
, initWithCoder:
, etc…), dealloc
methods, and within custom getters and setters. For more information, see Apple’s docs on using accessor methods in initializer methods and dealloc..#define
to define a block of code -- #define
code is very difficult to debug#pragma mark - <text>
and #pragma mark <text>
to group methods In large implementation files:Discover the secret to clean and efficient Objective-C code with Stripe's comprehensive Coding Style Guide. This guide is designed to help developers maintain consistency and readability in their code, ensuring a smooth and seamless experience for both team members and users. With a focus on ground rules, variable naming, control flow, documentation, literals, constants, and design patterns, this guide covers every aspect of Objective-C programming to help you write better code, faster.
Stripe's Coding Style Guide emphasizes the importance of clarity, organization, and consistency in code. From proper indentation and spacing to the use of literals and constants, the guide provides detailed instructions and examples to help developers create clean, efficient, and easy-to-understand code. By following these guidelines, developers can ensure that their code is not only functional but also maintainable and scalable.