DEV Community

Cover image for Code Smell 235 - Console Side Effects
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

5

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);
Enter fullscreen mode Exit fullscreen mode

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);
  });

});

Enter fullscreen mode Exit fullscreen mode

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

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.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay