Skip to content

Instantly share code, notes, and snippets.

View SURYAKANTSHARMA's full-sized avatar
🎯
Focusing

Suryakant SURYAKANTSHARMA

🎯
Focusing
View GitHub Profile
@SURYAKANTSHARMA
SURYAKANTSHARMA / AddItemToCache.swift
Last active April 20, 2019 10:49
Example of Unified Unit/Output
class OnDiskCache {
struct Item {
let path: String
let age: TimeInterval
let size: Int
}
var currentItems: Set<Item> { /* .returning current items.. */ }
@SURYAKANTSHARMA
SURYAKANTSHARMA / CleanUpPolicy.swift
Created April 20, 2019 11:39
Input/OutPut Refactoring
protocol CleanUpPolicy {
func itemsToRemove(from items: Set<OnDiskCache.Item>) -> Set<OnDiskCache.Item>
}
struct MaxSizeCachePolicy: CleanUpPolicy {
let maxSize: Int
func itemsToRemove(from items: Set<OnDiskCache.Item>) -> Set<OnDiskCache.Item> {
var itemToRemove = Set<OnDiskCache.Item>()
let sortedItems = items.sorted { $0.age < $1.age }
@SURYAKANTSHARMA
SURYAKANTSHARMA / CleanUpPolicy.swift
Created April 20, 2019 11:39
Input/Output refactoring
protocol CleanUpPolicy {
func itemsToRemove(from items: Set<OnDiskCache.Item>) -> Set<OnDiskCache.Item>
}
struct MaxSizeCachePolicy: CleanUpPolicy {
let maxSize: Int
func itemsToRemove(from items: Set<OnDiskCache.Item>) -> Set<OnDiskCache.Item> {
var itemToRemove = Set<OnDiskCache.Item>()
let sortedItems = items.sorted { $0.age < $1.age }
@SURYAKANTSHARMA
SURYAKANTSHARMA / AvoidSingleton.swift
Last active May 3, 2019 17:25
Save User Balance
struct User {
let balance: Double
}
func saveUserBalance(user: User, buyingAmount: Double) {
let remainingBalance = user.balance - buyingAmount
// save user balance for persistance
UserDefaults.standard.set(remainingBalance, forKey: "userBalance")
}
@SURYAKANTSHARMA
SURYAKANTSHARMA / localScope.swift
Last active May 3, 2019 17:25
Avoid Singleton
func saveUserBalance(user: User, buyingAmount: Double, defaults: UserDefaults = UserDefaults.standard) {
let remainingBalance = user.balance - buyingAmount
// save user balance for persistance
defaults.set(remainingBalance, forKey: "userBalance")
}
class MockUserDefault: UserDefaults {
var balance = Double()
override func set(_ value: Double, forKey defaultName: String) {
balance = value
}
}
func testUserBalanceCalculation() {
let user = User(balance: 100)
let mockUserDefaults = MockUserDefault()
class ProfileViewController: UIViewController {
var user: User
convenience init(user: User) {
self.init(nibName:nil, bundle:nil)
self.user = user
}
}
let vc = ProfileViewController()
vc.user = user
navigationViewController.push(vc, animated: true)
class DateFormatter {
static func convertToUTC(localDate: Date) -> Date {
// conversion logic
return utcDate
}
}
typealias Amount = Float
struct User {
let name: String
let salary: Amount
}
enum PerformanceRating {
enum Result {
case warning(letter: String)
case bonus(amount: Amount)