Code Formatting Guidelines
This document outlines the code formatting standards for Swift and Objective-C code in the SideStore project. Consistent formatting makes the codebase more readable and maintainable.
General Principles
- Consistency: Follow the existing code style in the project
- Readability: Code should be easy to read and understand
- Simplicity: Prefer simple, clear solutions over clever ones
- Apple conventions: Follow Apple's official Swift and Objective-C conventions where possible
Swift Formatting
Naming Conventions
Variables and Functions
- Use camelCase for variables, functions, and methods
- Use descriptive names that clearly indicate purpose
// ✅ Good
let downloadProgress: Float
func validateUserCredentials() -> Bool
var isAppInstalling: Bool
// ❌ Bad
let dp: Float
func validate() -> Bool
var installing: Bool
Classes, Structs, Enums, and Protocols
- Use PascalCase for types
- Use descriptive, noun-based names
// ✅ Good
class AppInstaller
struct InstallationProgress
enum AppState
protocol AppManaging
// ❌ Bad
class installer
struct progress
enum state
protocol managing
Constants
- Use camelCase for constants
- Consider using SCREAMING_SNAKE_CASE for global constants
// ✅ Good
let maxRetryAttempts = 3
private let defaultTimeout: TimeInterval = 30.0
// Global constants
let MAX_CONCURRENT_DOWNLOADS = 5
// ❌ Bad
let MaxRetryAttempts = 3
let max_retry_attempts = 3
Indentation and Spacing
Indentation
- Use 4 spaces for indentation (not tabs)
- Align continuation lines with the opening delimiter
// ✅ Good
func installApp(withIdentifier identifier: String,
sourceURL: URL,
completion: @escaping (Result<Void, Error>) -> Void) {
// Implementation
}
// ❌ Bad
func installApp(withIdentifier identifier: String,
sourceURL: URL,
completion: @escaping (Result<Void, Error>) -> Void) {
// Implementation
}
Line Length
- Keep lines under 120 characters when possible
- Break long lines at logical points
// ✅ Good
let longVariableName = SomeClass.createInstanceWithVeryLongMethodName(
parameter1: value1,
parameter2: value2
)
// ❌ Bad
let longVariableName = SomeClass.createInstanceWithVeryLongMethodName(parameter1: value1, parameter2: value2, parameter3: value3)
Spacing
- Use single spaces around operators
- No trailing whitespace
- Single empty line between functions and major code sections
// ✅ Good
let result = value1 + value2
if condition && anotherCondition {
// Code
}
func firstFunction() {
// Implementation
}
func secondFunction() {
// Implementation
}
// ❌ Bad
let result=value1+value2
if condition&&anotherCondition{
// Code
}
func firstFunction(){
// Implementation
}
func secondFunction(){
// Implementation
}
Braces and Control Flow
Brace Style
- Opening brace on the same line as the statement
- Closing brace on its own line, aligned with the opening statement
// ✅ Good
if condition {
doSomething()
} else {
doSomethingElse()
}
class MyClass {
func myMethod() {
// Implementation
}
}
// ❌ Bad
if condition
{
doSomething()
}
else
{
doSomethingElse()
}
Guard Statements
- Use guard statements for early returns
- Keep guard conditions simple and readable
// ✅ Good
guard let url = URL(string: urlString) else {
completion(.failure(ValidationError.invalidURL))
return
}
guard !apps.isEmpty else {
return
}
// ❌ Bad
if let url = URL(string: urlString) {
// Long nested code block
} else {
completion(.failure(ValidationError.invalidURL))
return
}