Apply Functional Programming Principles
|Line 14:||Line 14:|
 - Sutter, H. 2005. "The
 - Sutter, H. 2005. "The : A in "Dr. Dobb's Journal, http://www.gotw.ca/publications/concurrency-ddj.htm
 - Freeman S, Pryce N, Mackinnon T, Walnes J. "Mock Roles, not Objects"
 - Freeman S, Pryce N, Mackinnon T, Walnes J. "Mock Roles, not Objects"http://www.jmock.org/oopsla2004.pdf
By [[Edward Garson]]
By [[Edward Garson]]
Revision as of 04:25, 27 August 2009
Functional programming has recently enjoyed renewed interest from the mainstream programming community. Part of the reason is because emergent properties of the functional paradigm foster a pre-disposition for it to better address the challenges posed by our industry's shift toward multi-core. However, while that is certainly an important application, it is not the reason this piece admonishes you to know thy functional programming.
Mastery of the functional programming paradigm can greatly augment the quality of the code you write in other contexts, because if you deeply understand and apply the functional paradigm, your designs will exhibit a much greater degree of referential transparency.
Referential transparency is a very desirable property: it implies that functions consistently yield the same results, given the same input, irrespective of where and when they are invoked. That is, side effects from mutable state have a diminished impact on function evaluation (ideally none!).
A leading cause of defects in imperative code is attributable to mutable variables: everyone reading this (except perhaps my mom) will have investigated why some value is not as expected in a particular circumstance. Visibility semantics can help to mitigate these insidious defects, or at least to drastically narrow down their location, but their true culprit may in fact be the providence of designs that employ inordinate mutability.
And we certainly don't get much help from industry in this regard: introductions to object-orientation (for one) tacitly promote such design, because they often show examples composed of graphs of relatively long-lived objects that happily call mutators on each other, which can be dangerous. However, with good test-driven design (particularly when astutely employing a mock object framework), inordinate mutability can be designed away: object graphs become composed largely of immutable objects, i.e. having readonly/final instance variables (in C#/Java-speak), and constructors become a means with which to perform one-time object initialization. (An interesting emergent property is that instance variables can be made public - gasp! - as the protection afforded by accessors becomes moot: the member is immutable!). The net result is a design that typically yields more numerous, smaller classes (thus better cohesion) and more numerous, smaller functions that act on arguments passed into them, rather than to reference mutable member variables; all of which adds up to a much higher degree of referential transparency. And, positively nothing will get these ideas as deeply into your bones as to learn a functional programming language, where this model of computation is the norm.
Of course, there are caveats to this approach in some contexts; for one, many virtual machine implementations become inefficient when required to manage multitudinous small objects: pragmatism still applies. In object oriented systems, this style often yields better results with domain models (i.e. collaborations and complex business rules) than with server-side processing.
Nevertheless, you should master the functional programming paradigm and apply these principles in other domains: you will find that your object systems (for one) are much closer to their functional counterparts than many would have you believe. In fact, some experts assert that the apex of functional programming and object orientatation are merely a reflection of each other, a form of computational yin and yang.
 - Sutter, H. 2005. "The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software", Dr. Dobb's Journal, http://www.gotw.ca/publications/concurrency-ddj.htm
 - Freeman S, Pryce N, Mackinnon T, Walnes J. 2004. "Mock Roles, not Objects", OOPSLA 2004, http://www.jmock.org/oopsla2004.pdf
This work is licensed under a Creative Commons Attribution 3
Back to the 97 Things Every Programmer Should Know home page