Functional Lessons

Functional Lessons

Software engineering is made up of many different paradigms each one with its advocates and its detractors.

Which one is currently en vogue can be subject to trends in thinking or technology.

Functional programming is one such paradigm that has grown in popularity with its application being championed in various different arenas.

I have written before that the application of a technique in all situations regardless of merit is foolhardy and that Object Oriented Programming (OOP) is being written off too readily, however that is not to say that there aren't things we can learn from functional programming to improve our approach to OOP.

Avoiding Side Effects

A key tenant of functional programming is referential transparency, this is that a function or method should be interchangeable with its return value.

This behaviour implies that a method is pure, has no side effects and contains no state.

Writing methods that are this pure is difficult, despite our best efforts objects very often do have state.

A more practical approach is to avoid side effects that cannot be anticipated based on the name of the method or the name of the class in which it resides.

This avoids what is sometimes referred to as action at a distance where operations in one area of code have an unintended and unpredictable effect elsewhere.

This is a very good definition of a bug and shows how side effects can set the caller of your code up to unwittingly create problems in the code base.

Immutability

A consequence of referential transparency is that objects and values once created are immutable and cannot be changed.

Whilst this also plays a part in avoiding side effects it also has more practical benefits in providing efficiency in certain situations and going a long way to ensuring thread safety.

At the heart of every threading problem is usually data or state being mutated in an uncontrolled manner.

While again pure immutability can be difficult to achieve the change of any important piece of data or state in an object should always be implemented in a controlled manner.

Where at all possible changes should be surfaced by creating new objects or data rather than modifying anything pre-existing.

Declarative Style

Functional programming is said to have a declarative style where code expresses what must be accomplished in contrast to an imperative style that describes how something should be achieved.

This implies that code describes the results of computation without exposing detail of the control flow of how this was achieved.

As complex as this may sound good naming of classes and methods will allow code to be written that doesn't focus on the detail of what is happening and instead allows the caller to only be concerned with the results.

The details of computation are often subject to change, the more coupling that has been created between this detail and other areas of the code base the more impactful this change will be.

Something as simple as using a well named abstraction to hide this detail can loosen this coupling and allow code to be written using a more declarative style.

What we should take from all this is that the pillars and principles of writing good code are actually universal, different paradigms often choose to place more emphasis on certain aspects but we will observe a certain amount of cross pollination.

Trying to take any one of these principles to its ultimate conclusion will very often require you to put more effort into coding within defined boundaries as opposed to taking a practical approach of trying to ship software.

However as we've demonstrated many of the benefits of these principle can be achieved by relatively simple means when you have a sensible and practical outlook.

Giovanni Scerra ?

Head of Application Architecture & Development

7 年

Very good points Ben. I agree with you that OOP can definitely benefit from functional concepts like referential transparency and immutability. Personally I would not push too far, in the sense that I would not recommend developing in an OO language using a purely functional style. These days modern OO languages can play very well with functional languages (e.g. C# & F#, Java & Clojure, etc.) so we are in the position to always choose the best tool for the job. Some problems are better modeled with OO languages while others are more naturally solved in a functional language. Good article!

回复

要查看或添加评论,请登录

Ben Walpole的更多文章

  • Network of Networks

    Network of Networks

    When we're searching for analogies to describe the operation of the internet we often fall back on that of posting a…

  • Underpinning Kubernetes

    Underpinning Kubernetes

    Kubernetes is the de facto choice for deploying containerized applications at scale. Because of that we are all now…

  • Compiling Knowledge

    Compiling Knowledge

    Any software engineer who works with a compiled language will know the almost religious concept of the build. Whether…

  • Terraforming Your World

    Terraforming Your World

    Software Engineers are very good at managing source code. We have developed effective strategies and tools to allow us…

  • Being at the Helm

    Being at the Helm

    The majority of containerized applications that are being deployed at any reasonable scale will likely be using some…

  • Avoiding Toiling

    Avoiding Toiling

    Site Reliability Engineering (SRE) is the practice of applying software engineering principles to the management of…

    1 条评论
  • The Language of Love

    The Language of Love

    Software engineers are often polyglots who will learn or be exposed to multiple programming languages over the course…

  • Vulnerable From All Sides

    Vulnerable From All Sides

    Bugs in software engineering are a fact of life, no engineer no matter what he perceives his or her skill level to be…

  • The World Wide Internet

    The World Wide Internet

    Surfing the web, getting online and hitting the net are terms that ubiquitous among the verbs that describe how we live…

  • What's In a Name?

    What's In a Name?

    Surfing the web seems like straightforward undertaking, you type the website you want to go to into your browsers…

社区洞察

其他会员也浏览了