Code Smell 235 - Console Side Effects

Code Smell 235 - Console Side Effects

You compute and log in the same place

TL;DR: Avoid side effects

Problems

  • Coupling
  • Testability
  • Reusability
  • Function Composition

Solutions

  1. Decouple the code and avoid side-effects
  2. Inject the output destination

Context

Outputting to the console within an internal function generates coupling and side effects

Sample Code

Wrong

function drawChristmasTree(height) {
  let tree = '';
  let currentFloor = 1;

  while (currentFloor <= height) { 
      tree += ' '.repeat(height - currentFloor) + '??'.repeat(currentFloor)
        + '\n';
      currentFloor++;
  }

  // This function has side effects
  // You cannot test it
  console.log(tree);
}

drawChristmasTree(7);        

Right

function createChristmasTree(height) {
  let tree = '';
  let currentFloor = 1;

  while (currentFloor <= height) { 
      tree += ' '.repeat(height - currentFloor) + '??'.repeat(currentFloor)
        + '\n';
      currentFloor++;
  }

  return tree;
}

// The side effects are OUTSIDE the function
console.log(createChristmasTree(7));

// You can also test it 

const createChristmasTree = createChristmasTree(7);

describe('createChristmasTree', () => {
  it('should create a Christmas tree of the specified height', () => {
    const expectedTree = 
      '      ??\n' +
      '     ????\n' +
      '    ??????\n' +
      '   ????????\n' +
      '  ??????????\n' +
      ' ????????????\n' +
      '??????????????\n';

    const result = createChristmasTree(7);
    expect(result).toEqual(expectedTree);
  });

});        

Detection

[X] Automatic

Several linters warn for this usage

Tags

  • Globals

Conclusion

Instead of logging directly within internal functions, a more modular and flexible approach is to have functions return values or throw exceptions when errors occur.

The calling code can then decide how to handle and log these results based on the application's logging strategy.

Relations

Code Smell 17 - Global Functions

Disclaimer

Code Smells are my opinion.

Credits

Image generated by Midjourney


Memory is like an orgasm. It's a lot better if you don't have to fake it.

Seymour Cray


This article is part of the CodeSmell Series.


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

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…

社区洞察

其他会员也浏览了