Skip to content

Instantly share code, notes, and snippets.

@raheelahmad
Last active August 29, 2015 14:25
Show Gist options
  • Save raheelahmad/001aa888a91a9e4c2c78 to your computer and use it in GitHub Desktop.
Save raheelahmad/001aa888a91a9e4c2c78 to your computer and use it in GitHub Desktop.
Swift vs. Objective-C

autoscale: true build-lists: true

Swift vs. Objective-C


Why Swift is the future


Objective-C

  • C-superset
  • Dynamic type system
  • Object-Oriented
  • influences: C, Smalltalk

Swift

  • Strongly typed
  • (mostly) functional
  • correctness, conciseness (S v N)
  • influences: Scala, Rust, Haskell

Strong typing

  • correctness at compile time (besides behavior)
  • types as proofs (... illegal is impossible)
  • vs. testing: TDD vs. TDD
  • e.g.: Optional (Maybe), typed collections, generics
  • (most) type inference
  • steep ramp-up, but compiles == runs well

Simpler APIs

  • blocks/closures
  • initializers
  • string interpolation
  • (basic) operator overloading
  • literals
  • class declaration
  • easier unit tests

NSInteger (^SumOfTwo)(NSInteger, NSInteger) = ^BOOL(NSInteger first, NSInteger second) {
    return first + second
};

let sumOfTwo = { (first: NSInteger, second: NSInteger) in
    return first + second
}

let sumOfTwo = { first, second in
    return first + second
}

let sumOfTwo = { return $0 + $1 }

let sumOfTwo = { $0 + $1 }

@property (nonatomic, copy) BOOL (^comparator)(NSString *, NSString*);

let comparator: (String, String) -> Bool

- (void)compare:(BOOL (^)(NSString *, NSString *))comparator {
    
}

func compare(comparator: (NSString, NSString) -> Bool) {

}

Person.h

@interface Person: NSObject
@property (strong) NSString *name;
@property (strong) NSNumber *age;
@property (nonatomic, copy) BOOL (^onChange)(NSString *context);
@end

Person.m

@interface Person() 
@property (strong) NSString *firstName;
@property (strong) NSString *lastName;
@end

@implementation Person
...
@end

class Person {
	var name: String
	var age: Int
	var onChange: (String) -> Void
	
	private firstName: String
	private lastName: String
}

Functional

  • first-class primitives
  • stdlib includes map, flatMap, reduce
  • currying
  • (somewhat) lazy

Value Types

  • enums, structs
  • a great complement for functional programming
  • avoid state (functional core, imperative shell)
  • Immutable by default, single owner
  • thread safety

Faster


Other benefits

  • namespaces
  • property observers
  • access modifiers
  • generics
  • tuples
  • protocols

Even more benefits

  • pattern matching (switch-case on steroids)
  • operator overloading + subscripting
  • error handling (control flow + type)
  • dynamic frameworks
    • versioned; reduced app size; can include assets etc.

Few more benefits

  • playgrounds
  • well designed from scratch (rather than a legacy system)
  • open source
  • developer mindshare
  • Apple is betting on it

😱 Cons

  • May have rough edges (very few post-2.0)
  • Some tasks are more difficult with the strong type system (parsing JSON, building a plugin system)
  • Interacting with C (lots better in 2.0)
  • Familiar vs. complex
  • Solutions can be too Creative ✨
  • "you can do it all in ObjC"

Who Else


Once more: Why Swift?

  • Faster iteration for an implementation
  • Safer
  • Cleaner abstractions in complex contexts
    • less bugs
    • scalable
    • modifiable...
  • Safer bet for the future

Plan


Path to Swift

  • All new code
  • Break out modules into Swift frameworks
    • continue & refine LVUser, LVBankConnection, LVNetworking
    • easier to grok/develop/reuse
    • ability to add unit tests for smaller modules,
    • possibly open source modules
  • Convert legacy code incrementally

Refactor / rewrite: Goal

  • clean architecture & grasp
  • fewer bugs
  • faster iterations

Plan

  • (re)write Persistence layer ♦️
  • rewrite Model layer ♦️
  • move to JSON
  • rewrite API layer ♦️
  • rearchitect MVC pattern + information flow ♦️
    • factored-out, reusable code
    • clearer dependencies
  • Others
    • Analytics
♦️  →   adds 1.5 - 2 months to development time
all →   adds 2 - 2.5 months
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment