関数型プログラミングを学ぶことの重要性 – プログラマが知るべき97のこと



Kevlin Henney(編)、和田 卓人(監修)、夏目 大(訳)

Functional programming has recently enjoyed renewed interest from the mainstream programming community.


Part of the reason is because emergent properties of the functional paradigm are well positioned to 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 improve the quality of the code you write in other contexts.


If you deeply understand and apply the functional paradigm, your designs will exhibit a much higher degree of referential transparency.

重要なのは、参照透過性(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, function evaluation depends less — ideally, not at all — on the side effects of mutable state.


A leading cause of defects in imperative code is attributable to mutable variables.


Everyone reading this will have investigated why some value is not as expected in a particular situation.


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.

「可視性セマンティクス(visibility semantics)」などが、そうした目に見えにくい不具合を減らす上で役立つことはありますし、少なくとも、不具合の箇所の絞り込みにかなり役立つのは間違いないでしょう。しかし、ここで本当に問題なのは、そもそも、変数の値が変わってしまうような設計をしてしまったことです。

And we certainly don’t get much help from industry in this regard.


Introductions to object orientation tacitly promote such design, because they often show examples composed of graphs of relatively long-lived objects that happily call mutator methods on each other, which can be dangerous.

オブジェクト指向言語の入門書などには、変数の値が変わる設計を、暗黙のうちに助長してしまっているものもあります。比較的「長生き」するオブジェクトたちが、互いの値や状態を変えるメソッド(mutator methods)を次々に呼び出す、というようなプログラム例が図解入りで載っていたりするからです。これはとても危険です。

However, with astute test-driven design, particularly when being sure to “Mock Roles, not Objects”, unnecessary mutability can be designed away.


The net result is a design that typically has better responsibility allocation with more numerous, smaller functions that act on arguments passed into them, rather than referencing mutable member variables.


There will be fewer defects, and furthermore they will often be simpler to debug, because it is easier to locate where a rogue value is introduced in these designs than to otherwise deduce the particular context that results in an erroneous assignment.


This adds up to a much higher degree of referential transparency, and positively nothing will get these ideas as deeply into your bones as learning a functional programming language, where this model of computation is the norm.


Of course, this approach is not optimal in all situations.


For example, in object-oriented systems this style often yields better results with domain model development (i.e., where collaborations serve to break down the complexity of business rules) than with user-interface development.


Master the functional programming paradigm so you are able to judiciously apply the lessons learned to other domains.


Your object systems (for one) will resonate with referential transparency goodness and be much closer to their functional counterparts than many would have you believe.


In fact, some would even assert that the apex of functional programming and object orientation are merely a reflection of each other, a form of computational yin and yang.


By Edward Garson