RSS Telegram YouTube Apple Яндекс Spotify Amazon Почта

79. Side Effects

24.11.2024

Скачать

К списку выпусков

Side effects are a core concept in functional programming (FP), but they’re relevant in every programming paradigm. So, what exactly is a side effect, and why should we care about them?

What Is a Side Effect?

In simple terms, a side effect is any impact a function has on the outside world beyond returning a value. Think of a function as a black box: you pass in some data and get a result. If the function also writes to a file, modifies a global variable, or sends data over the network, that’s a side effect.

This doesn’t mean side effects are inherently bad. In fact, in web development, side effects like saving data to a database or sending a response over the network are often the main purpose of the code! But understanding and managing side effects is key to writing maintainable and predictable software.

Why Side Effects Matter

Guy Steele, a well-known computer scientist and co-author of Java and JavaScript specifications, highlights three programming breakthroughs that simplified our work by managing complexity. These are:

Structured Programming: Before structured programming, goto statements let you jump to any line of code, making programs hard to follow. By limiting flow control to a few patterns (like loops and conditionals), code became easier to read and understand.

Object-Oriented Programming (OOP): In the pre-OOP era, global variables were everywhere, accessible from any part of the program. OOP introduced the idea of encapsulation, restricting access to data. Even without classes, you can apply this principle by using local variables to limit a function's scope.

Functional Programming (FP): FP emphasizes separating code with side effects from code without them. This makes programs easier to reason about and debug.

FP’s Approach to Side Effects

Functional programming doesn’t eliminate side effects but organizes them better. For example, imagine you have a list of email addresses, and you need to generate personalized messages and send them.

In an imperative approach, you might loop through the list, generate a message, and send it in one go. FP suggests splitting this into two steps:

This separation clarifies what the code is doing at each stage and isolates the part that interacts with external systems.

Recognizing Side Effects

A function has side effects if it:

Why the Name “Side Effect”?

The term “side effect” might sound negative, as if it’s an unintended consequence. But in many cases, especially in web development, side effects like saving data or sending responses are exactly the point of our code. Maybe “external effect” would have been a better name, but here we are.

Final Thoughts

Managing side effects is about making your code clearer and more predictable. By isolating side effects and structuring code thoughtfully, you reduce bugs and make future changes easier. Whether you’re writing FP, OOP, or procedural code, keeping an eye on those “extra wires” connected to your functions is always a good idea.