- vừa được xem lúc

Blog#88: What are Pure Functions and Why You Should Care

0 0 24

Người đăng: NGUYỄN ANH TUẤN

Theo Viblo Asia

Hi, I'm Tuan, a Full-stack Web Developer from Tokyo 😊. Follow my blog to not miss out on useful and interesting articles in the future.

You may have heard the term "pure function" thrown around in the world of programming, but what exactly does it mean? In short, a pure function is a function that has no side effects and always returns the same output for a given input. This may not sound like a big deal, but pure functions can actually be extremely useful in a variety of contexts. In this article, we'll take a closer look at what pure functions are, why they're important, and how you can use them in your own code.

What is a Pure Function?

A pure function is a function that meets the following criteria:

  1. It only depends on its input parameters and not on any external state.
  2. It does not modify any external state.
  3. It always returns the same output for a given input.

Let's break down each of these criteria a bit further:

  1. A pure function only depends on its input parameters and not on any external state. This means that the function does not rely on any external variables or resources in order to do its job. It simply takes in some input, performs some operations on that input, and returns a result. This can make it much easier to understand and predict how the function will behave, as there are no external factors that could potentially impact its output.
  2. A pure function does not modify any external state. This means that the function does not change the value of any variables outside of itself, nor does it interact with any external resources (such as a database or API). This is an important aspect of pure functions, as it ensures that the function is completely self-contained and does not have any unintended consequences on the rest of your program.
  3. A pure function always returns the same output for a given input. This means that if you call the function with the same input multiple times, you will always get the same output. This can be extremely useful when it comes to testing and debugging your code, as it allows you to easily verify that your function is working correctly.

Why Pure Functions Matter

So now that we know what pure functions are, you might be wondering why they're so important. There are actually several good reasons to use pure functions in your code:

  1. Easier to test and debug Because pure functions always return the same output for a given input, it's much easier to write test cases for them and verify that they're working correctly. This can save you a lot of time and frustration when it comes to debugging your code.
  2. Easier to reason about Because pure functions are self-contained and do not rely on any external state, it's often much easier to understand how they work and predict their behavior. This can make your code more maintainable and easier to work with in the long run.
  3. Easier to optimize Because pure functions do not modify any external state, it's often possible to optimize them in ways that would not be possible with impure functions. For example, if you have a pure function that is called many times in your program, you could potentially cache the result of the function to avoid recalculating it each time. This can lead to significant performance improvements in some cases.
  4. Easier to parallelize Because pure functions do not rely on any external state, it's often much easier to parallelize them and take advantage of multiple CPU cores. This can be especially useful for tasks that involve large amounts of data processing.

Example

Now that we've looked at some of the benefits of pure functions, let's look at some real-world examples of how you might use them in your own code.

Data transformation

One common use case for pure functions is data transformation. For example, suppose you have a list of user objects and you want to extract just the email addresses from each object. You could write a pure function that takes in a list of user objects and returns a list of email addresses like this:

function extractEmails(users) { return users.map(user => user.email);
}

This function is pure because it only depends on its input (the list of users) and does not modify any external state. It also always returns the same output for a given input, making it easy to test and debug.

Sanitizing user input

Another common use case for pure functions is sanitizing user input. For example, suppose you have a form on your website that allows users to submit comments. You might want to write a pure function to strip out any HTML tags from the comments to prevent cross-site scripting attacks. You could do this with a function like this:

function sanitizeComment(comment) { return comment.replace(/<[^>]*>/g, '');
}

This function is pure because it only depends on its input (the comment string) and does not modify any external state. It also always returns the same output for a given input, making it easy to test and debug.

Generating unique IDs

Pure functions can also be useful for generating unique IDs. For example, you might want to write a pure function that generates a unique ID for each object in a list. You could do this with a function like this:

let nextId = 0; function generateId() { return nextId++;
}

This function is pure because it only depends on its input (none) and does not modify any external state. It also always returns the same output for a given input (though the output will be different each time the function is called).

Calculating values

Pure functions can also be useful for calculating values. For example, suppose you want to write a function that calculates the total cost of an order, including taxes and shipping. You could write a pure function to do this like this:

function calculateTotalCost(order) { const taxRate = 0.08; const shippingCost = 5.99; return ( order.items.reduce((total, item) => total + item.price, 0) * (1 + taxRate) + shippingCost );
}

This function is pure because it only depends on its input (the order object) and does not modify any external state. It also always returns the same output for a given input, making it easy to test and debug.

Validating data

Pure functions can also be useful for validating data. For example, suppose you want to write a function that checks whether a given string is a valid email address. You could write a pure function to do this like this:

function isValidEmail(email) { const emailRegex = /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/; return emailRegex.test(email);
}

This function is pure because it only depends on its input (the email string) and does not modify any external state. It also always returns the same output for a given input, making it easy to test and debug.

Conclusion

Pure functions are a powerful tool that can make your code easier to test, debug, and maintain. By following the principles of pure functions, you can write code that is more predictable and easier to reason about, which can ultimately save you time and effort in the long run. Whether you're working on a small personal project or a large enterprise application, pure functions can be a valuable asset to have in your toolkit.

As always, I hope you enjoyed this article and learned something new. Thank you and see you in the next articles!

If you liked this article, please give me a like and subscribe to support me. Thank you. 😊

Ref

Bình luận

Bài viết tương tự

- vừa được xem lúc

Giới thiệu Typescript - Sự khác nhau giữa Typescript và Javascript

Typescript là gì. TypeScript là một ngôn ngữ giúp cung cấp quy mô lớn hơn so với JavaScript.

0 0 528

- vừa được xem lúc

Bạn đã biết các tips này khi làm việc với chuỗi trong JavaScript chưa ?

Hi xin chào các bạn, tiếp tục chuỗi chủ đề về cái thằng JavaScript này, hôm nay mình sẽ giới thiệu cho các bạn một số thủ thuật hay ho khi làm việc với chuỗi trong JavaScript có thể bạn đã hoặc chưa từng dùng. Cụ thể như nào thì hãy cùng mình tìm hiểu trong bài viết này nhé (go).

0 0 436

- vừa được xem lúc

Một số phương thức với object trong Javascript

Trong Javascript có hỗ trợ các loại dữ liệu cơ bản là giống với hầu hết những ngôn ngữ lập trình khác. Bài viết này mình sẽ giới thiệu về Object và một số phương thức thường dùng với nó.

0 0 158

- vừa được xem lúc

Tìm hiểu về thư viện axios

Giới thiệu. Axios là gì? Axios là một thư viện HTTP Client dựa trên Promise.

0 0 149

- vừa được xem lúc

Imports và Exports trong JavaScript ES6

. Giới thiệu. ES6 cung cấp cho chúng ta import (nhập), export (xuất) các functions, biến từ module này sang module khác và sử dụng nó trong các file khác.

0 0 113

- vừa được xem lúc

Bài toán đọc số thành chữ (phần 2) - Hoàn chỉnh chương trình dưới 100 dòng code

Tiếp tục bài viết còn dang dở ở phần trước Phân tích bài toán đọc số thành chữ (phần 1) - Phân tích đề và những mảnh ghép đầu tiên. Bạn nào chưa đọc thì có thể xem ở link trên trước nhé.

0 0 249