Reconciling OOP vs. FP

OOP
animal.move(12, 15)
FP
move(animal, 12, 15)
tony := tiger{}
tony.move(1, 2) // OOP syntax
tiger.move(tony, 1, 2) // FP syntax

Polymorphism

type animal interface {
move(int, int)
}
func moveAnimal(a animal) {
a.move(42, 13)
move(a, 42, 13) // This doesn't work.
}
moveAnimal(tiger{})
fun foo(age: Int, name: String) {
println("${name} is ABOUT ${age} years old.");
}
fun foo(age: Double, name: String) {
println("${name} is EXACTLY ${age} years old.");
}
val name = "Alice"
val roundedAge = 42
val exactAge = 42.2

foo(roundedAge, name); // prints "Alice is ABOUT 42 years old."
foo(exactAge, name); // prints "Alice is EXACTLY 42.2 years old."

Composition vs. Inheritance

Mutability

Unnecessary Conditionals

Law of Demeter

Closures vs. Objects

func foo(age int, name)
println("${name} is ABOUT ${age} years old.")
func foo(age double, name)
println("${name} is EXACTLY ${age} years old.")
type person struct
age int
name string
func foo(someone) # Type "inference" rather than interfaces
foo(someone.age, someone.name)
func main()
# A constant
name = "Alice"
# May be reassigned due to := initialization
unusedName := "Bob"
# All args passed by reference and immutable outside their
# creation scope.
foo(42, name)
foo(42.2, name)
foo(person{age: 42, name: "Alice"})

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Clay Shentrup

Clay Shentrup

advocate of score voting and approval voting. software engineer. father. husband. american.