Why function becomes not pure

Pure function is absolute.  It gives definite outcome on certain input. Otherwise the function is not pure.

A function becomes not pure when its calculation depends on context other than parameters.

Then the question rises:  why not just pass the context in as parameters?

Yes you can, at the level of modelling and core business logic.  However at some stage of building software you can not do it anymore.  A controller in MVC handles API end point is definitely not pure. input might be a URL from user, it needs to use data stored to return a html page. Although the comptroller is often written in the form of an function, it is strictly speaking a procedure. In general lower level functions are more likely to be pure than high level ones.

What is the proper boundary where function stop being pure?

It depends. Ideally function should not have more than 3 parameters or more than 100 lines. A chuck easier for human to understand is probably no more than 10.  10 files in a fold, 10 functions in a class.  Overall they are just a way of organizing.

Following this approach we end up with well scoped chuck of code. However we break the whole product into so many pieces. Understanding the referencing relations becomes a nightmare. File naming and fold structure need to be really solid to compensate it.

OOP Vs functional programming is not the right question.  OOP tries to map real world object into software and is set on a layer above functional programming.

Leave a Reply