Skip to content

Instantly share code, notes, and snippets.

View rcharlton's full-sized avatar

Robin Charlton rcharlton

View GitHub Profile
@rcharlton
rcharlton / clean_code.md
Created April 5, 2019 08:18 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

@rcharlton
rcharlton / Thottle.swift
Last active May 17, 2017 08:03
A super-simple invocation rate throttler
/**
Manages the rate at which a closure is repeatedly invoked.
*/
public class Throttle {
/// The minimum time interval between executions.
private let period: TimeInterval
/// The closure to execute.
private let closure: () -> Void
@rcharlton
rcharlton / observable.swift
Last active May 17, 2017 08:04
A super-simple Observer pattern.
import Foundation
public final class Observer<T> {
public weak var subject: Observable<T>?
fileprivate var identifier: UInt
fileprivate var isValid: Bool {
return subject != nil
@rcharlton
rcharlton / KeyValueObserver.playground
Last active May 5, 2017 12:33
KeyValue Observing in Swift
import Foundation
public final class Observer: NSObject {
public let subject: NSObject
public let keyPath: String
public let didChange: ([NSKeyValueChangeKey : Any]) -> Void
@rcharlton
rcharlton / Xcode .gitignore
Created February 21, 2017 14:26
Xcode Swift Gitignore
.DS_Store
# Xcode
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
@rcharlton
rcharlton / enum.swift
Last active July 12, 2024 14:31
Swift enums, Equatable and Hashable
// Enums with associated values are not equatable...
enum MyEnum {
case a(number: Int)
case b
case c(text: String)
}
// We can define a hash based on the case or the case plus associated value.
extension MyEnum: Hashable {
@rcharlton
rcharlton / UIColor+Random.swift
Created February 11, 2017 17:17
Random UIColor values
extension UIColor {
class func random() -> UIColor {
return UIColor(
hue: CGFloat.random(min: 0.0, max: 1.0),
saturation: CGFloat.random(min: 0.3, max: 0.6),
brightness: CGFloat.random(min: 0.7, max: 1.0),
alpha: 1.0)
}
}
@rcharlton
rcharlton / TimeInterval.swift
Last active February 11, 2017 15:58
TimeInterval Convenience Methods
import Foundation
public func TimeInterval(seconds: Double) -> Foundation.TimeInterval {
return Foundation.TimeInterval(seconds)
}
public func TimeInterval(minutes: Double) -> Foundation.TimeInterval {
return Foundation.TimeInterval(minutes * 60.0)
}