Minimizing Shared State

Vladimir Khorikov has an example of removing shared state and making application code more “honest” as a result:

Note how we’ve removed the private fields. Getting rid of the shared state automatically decoupled the three methods and made the workflow explicit. Without the shared state, the only way we can carry data around is by using the methods’ arguments and return values. And that is exactly what we did: all three members now explicitly state required inputs and possible outputs in their signatures.

This is the essence of functional programming. With honest method signatures, it’s extremely easy to reason about the code as we don’t need to keep in mind hidden relationships between its different parts. It’s also impossible to mess up with the invocation order. If we try, for example, to put the second line above the first one, the code simply wouldn’t compile:

This is one of many reasons why I’m fond of functional programming.

Related Posts

Static Site Generation With Hugo

Steph Locke explains how to build a simple site using Hugo: This site uses Hugo. Hugo is a “static site generator” which means you write a bunch of markdown and it generates html. This is great for building simple sites like company leafletware or blogs. You can get Hugo across platforms and on Windows it’s […]

Read More

Using Azure Cloud Shell

Jeffrey Verheul shows off a bit of Azure Cloud Shell: Connecting to a database Now that your Cloud Shell is ready to go, you can start using Bash. This means you can also use sqlcmd from within Bash. You can connect to a database with sqlcmd, by using the following command: sqlcmd -S servername.database.windows.net -U […]

Read More

Categories

April 2017
MTWTFSS
« Mar May »
 12
3456789
10111213141516
17181920212223
24252627282930