Skip to content

Instantly share code, notes, and snippets.

View philosopherdog's full-sized avatar

Steve Thompson philosopherdog

View GitHub Profile
@philosopherdog
philosopherdog / working_effectively_with_legacy_code.md
Created January 15, 2025 14:11 — forked from jonnyjava/working_effectively_with_legacy_code.md
Working effectively with legacy code summary

WORKING EFFECTIVELY WITH LEGACY CODE

To me, legacy code is simply code without tests. I’ve gotten some grief for this definition. What do tests have to do with whether code is bad? To me, the answer is straightforward, and it is a point that I elaborate throughout the book: Code without tests is bad code. It doesn’t matter how well written it is; it doesn’t matter how pretty or object-oriented or well-encapsulated it is. With tests, we can change the behavior of our code quickly and verifiably. Without them, we really don’t know if our code is getting better or worse.

Chapter 1 Changing Software

Four Reasons to Change Software: For simplicity’s sake, let’s look at four primary reasons to change software.

@philosopherdog
philosopherdog / AutoRetry.swift
Created November 2, 2024 15:47 — forked from kean/AutoRetry.swift
Smart Auto Retry using RxSwift
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
import RxSwift
import RxCocoa
extension ObservableType {

Declaring models

class Dog: Object {

  dynamic var id = ""

  dynamic var name = ""

 dynamic var tempVariable = ""
@philosopherdog
philosopherdog / StringDimensionProvider.swift
Last active December 10, 2021 20:47
String Dimension Provider
//
// StringDimensionProvider.swift
//
// Created by steve on 2019-07-11.
// Copyright © 2019 Steven Thompson. All rights reserved.
//
//
// StringHeightProvider.swift
//
@philosopherdog
philosopherdog / UnwrapEnum.swift
Last active December 8, 2021 17:34
Swift Unwrap Enum Associated Type Without Switch #enum
enum CommunityProfileSection {
case FacilityName(String?)
case ContactInfo(ProfileContactViewModel)
case LocationInfo([ExpandableProfileModel])
}
extension CommunityProfileSection {
var facilityName: String? {
if case let .FacilityName(name) = self {
import UIKit
extension UICollectionReusableView {
/**
Used for creating dynamic height cells
- Call from overridden UICollectionReusableView (Cell) method `preferredLayoutAttributesFitting(_:)` which the system calls
- Setup the cell width in `sizeForItem` as usual & pass a temp height
- Setup Autolayout on cells to ensure a height can be computed
@philosopherdog
philosopherdog / ProtocolWitness.swift
Last active December 5, 2021 21:34
Simple Protocol Witness Example
import UIKit
struct ViewModel1 {
let title = "Hello"
}
struct ViewModel2 {
let message = "some message"
}
@philosopherdog
philosopherdog / Equating.swift
Last active October 13, 2021 16:28
Equatable Type Erasure
protocol Equating {
func isEqualTo(_ other: Equating)-> Bool
}
extension Equating where Self: Equatable {
func isEqualTo(_ other: Equating) -> Bool {
guard let other = other as? Self else { return false }
return other == self
}
}
public struct AnyEquatable: Equatable {
private let value: Any
private let equals: Any -> Bool
public init<E: Equatable>(_ value: E) {
self.value = value
self.equals = { ($0 as? E == value) ?? false }
}
}
@philosopherdog
philosopherdog / CountdownTests.swift
Created August 16, 2021 23:48 — forked from JonnyBeeGod/CountdownTests.swift
This code allows for testing UNNotificationCenter and UNNotificationSettings
func testNotifications() {
// map all authorizationStatus with expected Result
let authorizationStatusMap: [UNAuthorizationStatus: Int] = [.authorized: 1, .denied: 0, .notDetermined: 0, .provisional: 1]
UNNotificationSettings.swizzleAuthorizationStatus()
authorizationStatusMap.forEach { (key: UNAuthorizationStatus, value: Int) in
UNNotificationSettings.fakeAuthorizationStatus = key
let mockCenter = UserNotificationCenterMock()
let mockCoder = MockNSCoder()