Steve Loar

Web developer, horse rider and maker of small tables.

Ponies at Lost Valley Ranch

OO vs. Functional Programming

Object Oriented Programming (OO) and Functional programming are two very different approaches to writing programs. OO approaches programming with classes as the main construct to create programs. Functional programming uses functions as the main construct.

OO uses classes to model real world objects. It has properties that are mutable, methods that are publicly accessable or are limited to private access within the class. This can be an easier paradigm for program development as classes can be easier to understand. Ruby is considered an OO language.

Functional programming works with functions as the main construct and functions can be passed to them and returned from them. The properties and "things" are few in number and rarely change. This can seem complex to developers and harder to work with. But this can lead to powerful solutions where functions take functions as parameters which are then executed inside them. Haskel is an example of a functional programming language.

Which one you choose depends on the future you expect for your program, or the limitations of the language you work in. If you can select your language, you would consider OO for projects where you expect to add more objects and fewer methods in the future. You would consider Functional programming when you have a fixed set of properties and expect to add more functions on those over time. Some languages only allow one approach or the other, while some languages allow some intermingling of both approaches. For example, in Ruby, you can pass a block of code that will get executed when the yield keyword is encountered.