Популярное

Музыка Кино и Анимация Автомобили Животные Спорт Путешествия Игры Юмор

Интересные видео

2025 Сериалы Трейлеры Новости Как сделать Видеоуроки Diy своими руками

Топ запросов

смотреть а4 schoolboy runaway турецкий сериал смотреть мультфильмы эдисон
dTub
Скачать

What are pure functions?

Автор: Smok Code

Загружено: 2020-10-19

Просмотров: 1937

Описание:

They can speed up development and increase productivity, but at what cost? Smok gives you a #QuickAnswer.

#programming #tech #softwaredevelopment #developer #sde

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!

What are pure functions?

Поделиться в:

Доступные форматы для скачивания:

Скачать видео mp4

  • Информация по загрузке:

Скачать аудио mp3

Похожие видео

How to code good arguments for your function?

How to code good arguments for your function?

Функции и классы: когда использовать какие и почему?

Функции и классы: когда использовать какие и почему?

Idempotency in APIs: you should be aware of this!

Idempotency in APIs: you should be aware of this!

Master the Design of Functional Types in C#

Master the Design of Functional Types in C#

Learn Pure Functions In 10 Minutes

Learn Pure Functions In 10 Minutes

Избегайте наследования при разработке ООП-программного обеспечения?

Избегайте наследования при разработке ООП-программного обеспечения?

Чистейший стиль кодирования, в котором ошибки практически невозможны

Чистейший стиль кодирования, в котором ошибки практически невозможны

Pure Functions Javascript - A Way to Avoid Side Effects in Javascript

Pure Functions Javascript - A Way to Avoid Side Effects in Javascript

Дорогие функциональные братья

Дорогие функциональные братья

5 Sure Signs You're No Longer Junior Programmer

5 Sure Signs You're No Longer Junior Programmer

Functional Programming & Haskell - Computerphile

Functional Programming & Haskell - Computerphile

JavaScript Pure Functions - Tutorial for beginners

JavaScript Pure Functions - Tutorial for beginners

Как взломать любое программное обеспечение

Как взломать любое программное обеспечение

Все возможности C# за 10 минут

Все возможности C# за 10 минут

Что такое «чистые функции» и «побочные эффекты»?

Что такое «чистые функции» и «побочные эффекты»?

КОНЕЦ АПГРЕЙДА: Почему твой следующий ПК будет последним?

КОНЕЦ АПГРЕЙДА: Почему твой следующий ПК будет последним?

Мы что, миллионеры? Откуда деньги на землю и стройку

Мы что, миллионеры? Откуда деньги на землю и стройку

ООП На Простых Примерах | Объектно-Ориентированное Программирование

ООП На Простых Примерах | Объектно-Ориентированное Программирование

КАК НЕЛЬЗЯ ХРАНИТЬ ПАРОЛИ (и как нужно) за 11 минут

КАК НЕЛЬЗЯ ХРАНИТЬ ПАРОЛИ (и как нужно) за 11 минут

Pure Functions And Side Effects

Pure Functions And Side Effects

© 2025 dtub. Все права защищены.



  • Контакты
  • О нас
  • Политика конфиденциальности



Контакты для правообладателей: [email protected]