Pure functions are often hyped up in the Javascript world, probably because of the abundance of state in front end applications. While pure functions have their downsides (i.e. inconvenience, potentially large argument lists), I believe they should be used as much as reasonably possible, and I want to focus on pure functions in Go. What is a Pure Function? According to , a Pure function has the following properties: Wikipedia Its is the same for the same (no variation with local , , mutable or input streams from ). return value arguments static variables non-local variables reference arguments I/O devices Its evaluation has no (no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams). side effects Which means that as a developer I know two important things: When I call a pure function I will get the same exact result every time After calling a pure function my program will be in the same state it was before calling it (other than time will pass and I will have assigned the result of the function) Because of these properties, pure functions keep applications simple. As we know, simple applications tend to be faster, are easier to test and debug, and are less error prone in general. Example in Go (golang) totalCounted := [ ] {} { total := name = getNameFromDatabase() _, word := strings.Split(text, ) { word == mention { total++ } } totalCounted[name] = total } map string int func countNamesInText (text ) string 0 const for range " " if This function is impure for a couple reasons. Let's examine each one. 1. Program state is mutated by calling countNamesInText() Instead of mutating a global variable as a means of "returning" data to the caller, we should return the data via a return statement. We can assume this was done because elsewhere in the program it was a good idea to store of map of counts, but it would be better if that state management was handled outside of our counting function: { totalCounted := name = getNameFromDatabase() _, word := strings.Split(text, ) { word == mention { totalCounted++ } } totalCounted } func countNamesInText (text ) string int 0 const for range " " if return Much better, this function is more "pure" because it will not change the application's state. 2. Database Argument Our function is still impure because the "name" value, which affects the result of the function call, is retrieved from a database. In order for our function to be deterministic, that value should instead be passed as a parameter. Currently, if we wrote the test: { assert.Equal(t, , countNamesInText( )) } func TestCountNamesInText (t *testing.T) 2 "this word here" It wouldn't work consistently. If the database isn't set up, or if the database was tampered with, our tests will fail. That makes this a bad test, and we wrote the bad test because we have an impure function. Let's purify a bit more: { totalCounted := _, word := strings.Split(text, ) { word == mention { totalCounted++ } } totalCounted } func countNamesInText (text, name ) string int 0 for range " " if return Our function is pure! Now we can have a good test: { assert.Equal(t, , countNamesInText( , )) } func TestCountNamesInText (t *testing.T) 1 "this word here" "this" And if we include how our application will likely look now including state management and the database call: totalCounted := [ ] {} name := getNameFromDatabase() totalCounted[name] = countNamesInText( , name) map string int "some name in here" Thanks for reading! By Lane Wagner @wagslane Learn at Qvault: https://classroom.qvault.io Previously published at https://qvault.io/2019/10/30/purity-in-my-programming-please/