Controlled Functions

Controllable function (Controllable Function), refers to a class that may be generated by changing the function to determine parameters of the function result or a simple modification function of external dependencies during execution, this is my out to promote the concept of pure function a new term (probably new, right?).

Why do we need a controlled function

Pure function in functional programming world is very common, but recently it has also gradually show more and more attractive in the object-oriented world, you can even see the document author to document some framework or technology respected pure function. In my opinion, pure function to attract more developers to focus on one important reason is that it is easier to test, because we can be very sure determines the output in the case of a given input of pure function. But in the object-oriented world, the function because of the existence of the class, so we often write (or method) becomes difficult to design a pure function - after all the other members the way we will always be in class access the class.

The function is controlled to limit access to the external environment as well as to modify more relaxed. I wonder if you remember the "Control Variable" PEP high school biology mentioned in the book, in the design of experiments, we need to do to change only a single variable affecting the test results as possible. Write unit tests, too, if we can all rely on very simple control of a function (including function parameters and external dependencies, for example, other members of the class) changes, then we can easily design method we use to control variables test cases, and simplify the process into equivalence classes, which for the preparation of white-box testing, the benefits of self-evident.

What makes your function uncontrollable

If the functional dependence "current time" can not use this code to control external variables, then this function is often become less controllable. As in most programming languages or technology framework, modify the current system time or Mock global variables with the system time is often very difficult. Like JS can very easily Mock window.Dateclass, but in C #, Mock DateTimewill be very difficult.

Another function is to let the situation become a runaway horse is likely to be that it relies overly complex external objects, such as EF Core of DbContext. DbContextDifficult to be perfect Mock, whether it is InMemoryor SQLite InMemoryhas its limit lies. Not to mention the inquiry posture (whether or not AsNoTracking, whether to use a third party to expand EF) will affect the real execution of the generated. It had not so complicated logic, but because of the complexity of the introduction of an external object becomes difficult to predict.

To regain control of your function

Here I would like to introduce a programming language - Elmin Elm foundation class library, it is not unusual to provide direct access to the system function of time, on the contrary, Elm acquisition system designers believe that time is not a pure function because we can not at different times so that the function returns the same value. To describe this phenomenon, Elm this function in order to achieve a side effect, in short, developers need to use a mechanism similar Callback system to get the current time, so that forced us to lift the strong dependence on the current system time, because we can only get a real time system parameters through the Callback.

Elm imitate the idea of using external variables that we can not use the code to control the time, we can use the "packaging layer" approach to solve the problem of the strong dependency. For example, in the object-oriented world, it is possible by using one ISystemTimeProviderto get the interface to the current system time, it's like the Elm Callback, like hiding behind a layer of abstraction, unknown to the current system to provide accurate time for our code.

In addition we can not rely on external control mentioned above, there are some external dependency is easy to control, they tend to come from a third-party library that we used and their compositions are also usually more complex, e.g. the AspNetCore HttpContextwell DbContext. Imagine, if a function receives this type of argument, how to create a desired input becomes a difficult thing. This time we can try the Keep IT and the Simple Stupid . For example, when we only need to use DbContextthe time of the query results, query function return types directly from the BCL and not IQueryable<T>so dependent on the results of the query function can get rid of the DbContextdependence of.

Guess you like

Origin www.cnblogs.com/JacZhu/p/11006719.html