The following worries are reduced and debugging efficiency is improved.
"This flag should be true, but it's false!" "I don't know where the cause of the bug is." "If you fix it, you may have a problem somewhere again."
Design the following two functions exactly separately, and increase the pure area to reduce the dirty area.
A function that always returns the same result for the same argument and does not affect the scope outside the function.
A function that returns different results with the same arguments or affects the scope outside the function
The following is an example. Anything is fine as long as you can easily and clearly distinguish between pure and dirty, and make the pure area relatively thick, such as separating files by package or by naming.
class Pure:
"""
A class of pure functions
Make this fat
"""
def plus(self,a,b):
return a + b
def minus(self,a,b):
return a - b
class Dirty:
"""
A class of dirty functions
=A class with a function that changes the state
Reduce this
"""
def __init__(self):
self.my_number = 0
def plus(self,a):
self.my_number = self.my_number + a
return self.my_number
def minus(self,b):
self.my_number = self.my_number - 1
return self.my_number
There are four reasons.
A language that is easy to do functional programming. It is suitable for writing with abundant languages that handle functions or with pure functions. Functional programming itself can be VB or whatever.
The most specific thing in common is to minimize the impact of one area on another. Object orientation seeks to achieve this through encapsulation, polymorphism, and inheritance. Functional programming is trying to fatten a pure function like the one above to minimize the problem of state changes.
The following may be biased in a very personal view Both are not contradictory concepts, but coexisting concepts, but object-orientation tends to be built on the premise of changing the state of many objects, so the above purpose is "a certain area". Minimizing the impact of has on other areas is difficult to achieve. I also feel that object-oriented programming is less likely to produce results than learning and design costs compared to functional programming. So, while keeping in mind the trade-off with time and classifying to some extent, I mostly pay attention to Pure or Dirty.
The heart of functional programming is to fatten the pure realm. Functional programming is a delicious and versatile technology that is highly effective for its low learning costs.
Recommended Posts