Code Smell 209 - Side Effects

Code Smell 209 - Side Effects

Global scope is easy or a nightmare, or both

TL;DR: Avoid side effects on your code.

Problems

  • Coupling
  • Least Astonishment Principle violation

Solutions

  1. Favor referential transparency

Context

Referential transparency always produces the same output for a given input and does not have any side effects, such as modifying global variables or performing I/O operations.

A function or expression is referentially transparent if it can be replaced with its evaluated result without changing the behavior of the program.

This property allows for easier reasoning about the behavior of a program and enables optimizations such as caching and memorization.

Functions are treated as mathematical expressions that map inputs to outputs.

Sample Code

Wrong

let counter = 0;

function incrementCounter(value: number): void {
  // Two side effects

  counter += value; 
  // it modifies the global variable counter 

  console.log(`Counter is now ${counter}`); 
  // it logs a message to the console.
}        

Right

function incrementCounter(counter: number, value: number): number {  
  return counter + value; 
  // Not too efficient  
}
        

Detection

[X] Automatic

Most linterns can warn you when accessing the global state or?Functions?and create side effects.

Tags

  • Global

Conclusion

Functional Programming is amazing and can teach us a lot about how to write clean code.

We need to understand its pillars.

Relations

Code Smell 17 - Global Functions

Disclaimer

Code Smells are my?opinion.

Credits

Photo by?Daan Mooij?on?Unsplash


The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.

Brian W. Kernighan

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code

要查看或添加评论,请登录

Maximiliano Contieri的更多文章

  • Refactoring 001 - Remove Setters

    Refactoring 001 - Remove Setters

    Setters violate immutability and add accidental coupling TL;DR: Make your attributes private to favor mutability…

  • Code Smell 292 - Missing Return

    Code Smell 292 - Missing Return

    When your code loses its way TL;DR: Missing return statements cause unexpected behavior. Problems ?? Silent failures…

  • Code Smell 291 - Mutable Keys

    Code Smell 291 - Mutable Keys

    Changing Keys, Losing Values TL;DR: When you use mutable objects as keys in hashed collections, changing them breaks…

    2 条评论
  • Refactoring 023 - Replace Inheritance with Delegation

    Refactoring 023 - Replace Inheritance with Delegation

    Transform your rigid inheritance into flexible delegations TL;DR: Replace restrictive inheritance hierarchies with…

  • 16 Simple Tips to Boost Your Productivity x10

    16 Simple Tips to Boost Your Productivity x10

    TL;DR: More productivity tips These easy-to-follow tips will improve your output dramatically. 1.

  • Code Smell 290 - Refused Bequest

    Code Smell 290 - Refused Bequest

    When you ignore your inheritance, you will have trouble with your parents TL;DR: Subclasses should honor ALL their…

  • Code Smell 289 - Shy Visitor

    Code Smell 289 - Shy Visitor

    Don't knock. You are accepted TL;DR: Avoid combining the Visitor pattern with instanceof checks.

    1 条评论
  • Code Smell 288 - Unthrown Exceptions

    Code Smell 288 - Unthrown Exceptions

    TL;DR: Creating a new exception without throwing it leads to silent failures. When You Forget to Throw, Your Code Will…

    1 条评论
  • Code Smell 287 - Unused Local Assignment

    Code Smell 287 - Unused Local Assignment

    Are you using the returned value? TL;DR: Avoid assigning values you never use. Problems Dead code Unclear intent…

  • Refactoring 022 - Extract Common Ancestor

    Refactoring 022 - Extract Common Ancestor

    Make your class hierarchy clear and flexible TL;DR: Extract a common abstract class to mimic real-world structure…

社区洞察

其他会员也浏览了