×

Smok Code's video: What are pure functions

@What are pure functions?
They can speed up development and increase productivity, but at what cost? Smok gives you a . Clean Code (R. Martin) - US: https://amzn.to/3jVUEDU | UK: https://amzn.to/2Iij4Jz Best Practices for Programmers - US: https://amzn.to/2SPRSEg | UK: https://amzn.to/2Fr2h6e What are pure functions? Let’s start by acknowledging there are pure functions and pure virtual functions. I want to clear that these are unrelated. Pure virtual function is used in C++ to name an abstract virtual function. However when a programmer says that a function is pure - they mean two things: 1. function doesn’t have any side effect, and 2. returns the same value for the same input. Let’s unpack this. Side effect of a function is a process that changes things in an outside context of the function. For example this could be: consuming input, modifying an outside object or variable, writing to a file, network or console, starting a process, and simply calling a non-pure function. We could possibly list many other examples of how a function can affect the outer scope of our program, but I think you get the idea: no side effects. Now why is that important? Masters of the craft like Robert Martin say that the ”ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code”. You can take from it that we actually should care more about programs that are easier to read and understand. Pure functions reduce the overhead of thinking how everything else is affected by this function. Using pure functions enhances productivity and makes onboarding with the code easier. Pure function has to meet another condition: produce the same output for the same input, no matter how many times you call it. Seems simple enough - we know plenty of those: max, min, find, sum, average and so on. This makes our function predictable and cacheable. Caching can be important for some more costly functions, and it’s good to know that once computed value won’t change. There is an ongoing discussion if pure functions are idempotent. Which means almost exactly the same thing as producing the same output for the same input. However you need to know that we also use the term idempotent to label functions, or API endpoints. Such an endpoint will produce the same result for the same input, but there is no requirement that idempotent functions have no side effects. You see: a function that deletes a record by ID can be idempotent, as the deleted record stays deleted no matter how many times you call the function, but - as you surely notice it has the side effect. So you may hear that some people say: that pure functions have no side effects and are idempotent, but I prefer to avoid this term, as it is used in this other meaning. Pure functions can help you to avoid most of the bugs that can happen because of state modification, which is often the case with data races in parallel systems. Having that said - avoiding mutation of the state is not always possible, and is viewed as less performant, as you have to copy new state instead of changing data in place. All in all - use the pure functions where you can, and others where you need to, but try to clearly separate one from another. Subscribe, and I’ll see you in the next one, cheers!

33

4
Smok Code
Subscribers
15K
Total Post
87
Total Views
317.6K
Avg. Views
5.7K
View Profile
This video was published on 2020-10-19 19:30:05 GMT by @Smok-Code on Youtube. Smok Code has total 15K subscribers on Youtube and has a total of 87 video.This video has received 33 Likes which are lower than the average likes that Smok Code gets . @Smok-Code receives an average views of 5.7K per video on Youtube.This video has received 4 comments which are lower than the average comments that Smok Code gets . Overall the views for this video was lower than the average for the profile.Smok Code #QuickAnswer. #programming #tech #softwaredevelopment #developer #sde Clean has been used frequently in this Post.

Other post by @Smok Code