3 Distinguishing actions, calculations, and data

 
Image

In this chapter

  • Learn the differences between actions, calculations, and data.
  • Distinguish between actions, calculations, and data when thinking about a problem, coding, and reading existing code.
  • Track actions as they spread throughout your code.
  • Be able to spot actions in existing code.

We’ve already seen the categories of actions, calculations, and data at a glance. In this chapter, we’re going to learn how to identify these three categories in real life and in code. As we’ve talked about before, it’s the first step in doing functional programming. By identifying these categories, you’ll see how calculations are often overlooked and how infectious actions can be.

Actions, calculations, and data

Functional programmers distinguish between actions, calculations, and data (ACD).

Actions

Depend on how many times or when it is run

Also called functions with side-effects, side-effecting functions, impure functions

Examples: Send an email, read from a database

Calculations

Computations from input to output

Also called pure functions, mathematical functions

Examples: Find the maximum number, check if an email address is valid

Data

Facts about events

Examples: The email address a user gave us, the dollar amount read from a bank’s API

We apply this distinction throughout the development process. For instance, you might find functional programmers using these concepts in the following situations:

1. Thinking about a problem

Actions, calculations, and data apply to any situation

Lessons from our shopping process

Applying functional thinking to new code

Drawing the coupon email process

Implementing the coupon email process

Applying functional thinking to existing code

Actions spread through code

Actions can take many forms

Conclusion

Summary

Up next…

sitemap