Day 13: Write Functions in a Functional Style

Mathematical functions are a simple transformation from inputs to outputs. Given a set of parameters, a function performs a transformation based on these parameters, and returns the desired results. Because of their simple character, mathematical functions are a good analogy for what can be done by a computer. Since they are independent entities, functions can be used anywhere, whatever the context is. For example, one can use the sine function to calculate the sine of any angle, no matter where the angle occurs.

Functions in programming languages are usually not so nice. They are frequently defined not only by their parameters but also by the environment where they occur. For example, if we define a function to return the amount of memory used by the application, it will necessarily have to search for information that is particular to the programming environment.

A more insidious problem with functions in programming languages is that they may change the environment where they run. For example, the previous function could very well change internal data structures in order to get the required memory information. The next invocation can, for example, use internal data instead of accessing the disk for this information. Whenever this happens, the function is said to have side-effects.

Using functions with side effects may lead to several problems. Many of these problems are debated by researchers on functional programming theory. But the main practical problem is that it is hard to understand such a function in isolation, because it may change other parts of the program. As a result, one needs to consider the program as a whole when checking the validity of a function with side-effects, instead of just looking at its definition.

Object oriented programs make this process a little simpler, because, if properly used, methods can only modify data that is part of the object. But still the issue remains, especially when programmers are not careful about how objects are manipulated.

Functional Programming to the Rescue

Functions that follow the mathematical model are said to be pure functions. There are whole programming languages, such as Haskel and F#, that have been developed in order to explore programming with pure functions. While most people don’t work with functional languages, we can still benefit by using a functional-oriented style.

One of the rules used to approximate the functional style is to avoid side effects in programs as much as possible. One can not always do this, but the general strategy consists of dividing functions into two classes: the ones with side effect and the ones with no side effects. The principle is that each method or function should have only one of these purposes.

The simplest example are getters and setters. A getter is a function without side effects because it just returns the value of something inside the first object. On the other hand, setters are free to make lasting changes to the environment. From this point of view, we see that making this separation is good not only for style reasons. It helps a lot when we known that our functions are classified by their use.

The next step is to minimize the number of functions with side-effects. By doing so, it becomes easier to understand how a program works: one can look only at the current function or method to have an idea of what is going on. It is also easier to test functions that have no side effects, because they can be tested in isolation from the rest of the program.

Finally, a program written in this style is easier to change. Functions and methods in such programs have only local effects. Thus, if something needs to be changed, we need to look only at the inputs and outputs of each function, not to the whole program.

Summary

By understanding the difference between functions with side effects and pure functions we can develop a more systematic way to create software that is easier to test and change.

Although there are whole languages that use functional programming as its main style, we don’t need to use functional languages to benefit from the idea. Separating functions between pure function and function with side-effects we can capture many of the advantages of functional programming, while keeping with whatever language we are using for the current project.

Further Reading

There are several books about functional programming the ones I like most are:

Similar Posts:

Similar Posts

One Comment

  1. He made a mockery of Labour’s early prison release scheme by triumphantly posing in front of a £150,000 Bentley and shouting ‘Big
    up Keir Starmer!’ as he strolled to freedom.

    But armed kidnapper Daniel Dowling-Brooks is back behind bars after
    breaching his licensing conditions, The Mail On Sunday can reveal. 

    What’s more, he’s now flouting rules by documenting his life inside on social media.

    Dowling-Brooks, 29, was among the first of 1,200 prisoners to be freed early last October.
    He promised he had changed his ways as he was let out of
    HMP Swaleside in Kent. 

    But it can be revealed he has been recalled
    for violating the conditions of his release, though
    the details are unclear.

    When an offender is released early from prison, they are subject to
    license conditions for the remainder of their sentence,
    such as receiving visits from a supervising officer and staying in the UK.

    Since his recall, Dowling-Brooks has been breaking
    strict jail rules by posting on Instagram. He boasts about rapping in prison and complains about life behind bars. 

    The Ministry of Justice confirmed last night it was investigating after the MoS brought Dowling-Brooks’s posts to its
    attention.

    Armed kidnapper Daniel Dowling-Brooks (pictured) made a
    mockery of Labour’s early prison release scheme by triumphantly posing in front of a £150,000 Bentley and shouting ‘Big up
    Keir Starmer!’ as he strolled to freedom in October 2024

    However, the 29-year-old (pictured here with his mother Sarah upon his release last year) is now back behind bars after
    breaching his licensing conditions

    It is now threatening to increase his sentence and has contacted Instagram to get his posts removed.

    The father-of-two uploaded a photo from inside his cell which showed his bed and prison items.

    He wrote: ‘£60 for full duvet/pillow set. But you only get £19.80 a week.
    They want you to starve for three weeks just to be
    comfy. Don’t come jail it’s not as good as it might
    look trust me.’

    This week he posted a picture of stereo equipment with the caption: ‘Rappin ain’t dead!’
    Dowling-Brooks also pictured his cell window with bars and the caption: ‘Looks like it’s an 8th
    summer in this bitch.’

    He used the same social media account to post from prison during his original sentence.

    One source said: ‘This sort of behaviour will not go down well.’

    Former Met detective Peter Bleksley said: ‘This makes a complete mockery of the early release scheme.
    The criminals were laughing at the authorities at the time.

    Now this individual has shown how utterly ridiculous the whole thing was.

    Daniel Dowling-Brooks poses in front of a white Bentley as he celebrates being released outside HM Prison Swaleside

    Dowling-Brooks poses in car in a white tank
    top shortly after his release from HMP Swaleside in Kent

    ‘The issue of phones in prisons is huge. It’s time there were proper punishments as a result.’

    A Prison Service spokesman said: ‘We have launched an investigation into these videos
    and have requested their removal.

    ‘Any prisoner found with contraband, such as a mobile
    phone or recording equipment, is breaking the law
    and could face extra time in prison.’

    When Dowling-Brooks was released last October, he declared
    ‘I love my life’ as he posed leaning on the Bentley. 

    He said: ‘I’m a dad of two so I’m out now and I’m very
    happy. I’m going to be good.’ Talking about his crimes, he revealed:
    ‘I kidnapped someone who owed my friend money. I tied him up and had him at gunpoint.
    I beat him up.’

    His mother, Sarah, who was waiting for him, said: ‘He’s going to be good now.
    He has done his time. I’m so happy he is home.’

    He had previously been given a seven-year sentence for kidnapping and grievous bodily harm.

    He was one of hundreds of prisoners to be greeted by family members with high-end super cars as they
    walked out.

    LabourKeir StarmerInstagram

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.