There is the nature of constructor functions and how they are used as a way to create functions that are called off of an instance of that constructor by way of a prototype object. In contrast to that of a constructor function there is what many might call a pure function, or at least a pure like function if I can call it that, which might be a function that has at least some of the features of a pure function. In pure functions one of the features is that a pure function will always return the same result for the same arguments, this is not always the case when calling the prototype method of a constructor instance as the value can change from one call to the next with the same arguments depending on the value of the state.
With the instances of constructor functions there are prototype methods like that of the array splice method for example that will mutate an array in place, and then there are other methods in the array prototype such as slice that will not mutate the source array, but instead return a new array without mutating the source array off of while array slice was called. However even with array slice it can still end up giving different results depending on the state of the array that is is called off of, so although it is maybe a pure like function it is not a true pure function going by a strict definition of what one is.
So in this post I will be going over some pure functions, or at least functions that demonstrate some of the features of pure functions. I would say that using pure functions helps to keep things a little more organized, easy to read, and debug, but it is not enough for me to just say that. It would be best to see for ones self, not just by reading content such as this, but by taking the time to work out ones own pure function examples. Still it is nice to have some kind of guide as a starting point so lets get to it.
So one of the features of a pure function is that a pure function will return the same value every time for the same given arguments. So when calling the pure function no global variables, or state object outside of the function is mutated. Also now given object as an argument is mutate in place inside the function, but a new object can be returned that is based off the given source object. So only a new return value is returned, and then it is a question as to what is done with that return value outside of the call of the function.
So say I have two distance formula methods, one of which makes use of some global variables, and one that just works with what is passed via arguments.
The first dist formula only takes two argument that are of course the x and y values of one point, but not the other. The other point is stored outside the scope of the dist function, so depending on what the values are for those global variables the distance can change each time it is called depending on what the values of the globals are.
In this section I will be going over a simple Box constructor function that has a single distance prototype method, and a pure function style alternative that does the same thing without the use of a constructor.
So lets say I have a Box Class that is a constructor function, and a Prototype object with methods that make use of the this keyword in the body of code. In these prototype methods the this keyword refers to the instance of the Box Class when it is called off of such an instance.
So in place of using a constructor function to create a Box Class instance why not just have a pure function that will always return the same box object for the same arguments, and in addition it returns just a plain old object? I can then use the plain old object that has box properties with a stand alone distance formula that is also very much pure function like.
As of late I find myself ditching the use of constructors in favor of writing modules like this.
So pure functions are generally a better way of going about doing things I would say over all. I do not think that I would care to always try to make everything in a project fully functional by trying to make everything a pure function though. To at least some extent it might not always be possible to make all functions a pure function in a project anyway, but still I think they help to keep things a little more sane.
There are many more posts on pure functions on the open web of course, and as time goes by I am sure that there will only be yet even more on this topic. However it would make sense to read more of them on the topic, as well as work out some unique code that will help one get a better sense as to why pure functions are helpful. I have to admit that I like them, and I like the whole idea behind them where I am keeping everything that I am working with close off into a body of just one little function isolated from everything that is going on outside of it.