Science Knowings: JavaScript Course For Social Media

Pure Functions

Pure Functions: The Key to Predictable and Maintainable Code

Welcome! In this session, we'll dive into pure functions, a fundamental concept in functional programming that helps write clean, predictable, and maintainable code.

What are Pure Functions?

Pure functions are functions that:

  • Always return the same output for the same input (deterministic)
  • Have no side effects (don't modify any external state)
  • Are referentially transparent (can be replaced with their return value without changing the program's behavior)

Benefits of Using Pure Functions

Pure functions offer numerous benefits:

  • Predictable behavior: Always produce the same output, making it easier to reason about your code.
  • Testability: Easier to test since they don't rely on external state.
  • Concurrency: Can be safely executed in parallel, as they don't have side effects.
  • Immutability: Help preserve the integrity of your data by not modifying it.

Writing Pure Functions in JavaScript

To write pure functions in JavaScript, avoid using:

  • Global variables (use local variables instead)
  • Input/Output (use function parameters or return values)
  • Mutation of arguments (use ... spread operator to create copies)

Writing Pure Functions in Java

Similarly, in Java, avoid:

  • Instance variables (use method parameters instead)
  • Static variables (use local variables within methods)
  • System.out (use return values or logging frameworks)

Refactoring Impure Functions to Pure

Impure functions can be refactored to pure by:

  • Extracting side effects: Move side effects (e.g., logging) to separate functions.
  • Passing state as parameters: Make the state of the function explicit by passing it as parameters.
  • Using immutability: Avoid mutating arguments; instead, create new instances with updated state.

Examples of Pure Functions


// JavaScript
const sum = (a, b) => a + b;

// Java
public static int sum(int a, int b) {
  return a + b;

Side Effects of Impure Functions

Impure functions can lead to:

  • Unpredictable behavior: Output depends on external state, making it difficult to debug.
  • Concurrency issues: Can't be safely executed in parallel, as they may modify shared state.
  • Data corruption: May modify data in unintended ways, leading to errors.

How to Avoid Side Effects in Pure Functions?


  • Use immutable data structures (e.g., arrays, objects)
  • Return new instances instead of mutating existing ones
  • Use functional programming techniques (e.g., map, filter, reduce)

Testing Pure Functions

Pure functions are easier to test:

  • Unit tests: Test individual functions with different inputs and verify the expected outputs.
  • Property-based testing: Generate random inputs and check if the function's behavior matches your expectations.

Best Practices for Pure Functions

Best practices:

  • Favor pure functions over impure ones: Use pure functions whenever possible.
  • Name functions clearly: Indicate whether a function is pure or impure in its name.
  • Document side effects: Clearly document any side effects in the function's documentation.

Next Topic: Immutability

In the next session, we'll explore immutability, a powerful technique that complements pure functions and helps you write more robust and reliable code. Follow us to learn more!