Skip to content

Instantly share code, notes, and snippets.

import Foundation
// Inspired by https://gist.github.com/mbuchetics/c9bc6c22033014aa0c550d3b4324411a
struct JSONCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
public class Throttler {
private let queue: DispatchQueue = DispatchQueue.global(qos: .background)
private var job: DispatchWorkItem = DispatchWorkItem(block: {})
private var previousRun: Date = Date.distantPast
private var maxInterval: TimeInterval
public init(maxInterval: TimeInterval) {
self.maxInterval = maxInterval
@XDKostia
XDKostia / clean_code.md
Created March 1, 2021 18:00 — 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

import Foundation
typealias EmptyClosure = ()->Void
typealias GenericClosure<T> = (T)->Void
class SafeClosure {
private let closure: EmptyClosure
init<WeakObject: AnyObject>(weak object: WeakObject, _ closure: @escaping GenericClosure<WeakObject>) {
self.closure = { [weak object] in
import Foundation
class QueuedStorage<Value> {
// MARK: - Properties
static var defaultQueue: DispatchQueue {
return DispatchQueue(label: "QueuedStorage.default", qos: .default)
}
// MARK: Private
private var value: Value {
import Foundation
class QTimer {
let timeInterval: TimeInterval
var repeating: Bool = false
private var flags: DispatchSource.TimerFlags
private var queue: DispatchQueue?
private lazy var timer: DispatchSourceTimer = {
import Foundation
class DeferDispatch {
var interval: TimeInterval
var overridePrevious: Bool
var executionQueue: DispatchQueue?
var timer: QTimer?
var deferedClosure: (EmptyClosure)?