DEV Community

Cover image for Code Smell 217 - Empty Implementation
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

3

Code Smell 217 - Empty Implementation

You create empty methods instead of failing

TL;DR: Don't fill in methods to comply

Problems

  • Fail Fast Principle Violation

Solutions

  1. Throw an error indicating implementation is not complete

Context

Creating an empty implementation might seem fine to jump to more interesting problems.

The code left won't fail fast so debugging it will be a bigger problem

Sample Code

Wrong

class MerchantProcessor {
  processPayment(amount) {
    // no default implementation
  }
}

class MockMerchantProcessor extends MerchantProcessor {
  processPayment(amount) {
     // Empty implementation to comply with the compiler
     // Won't do anything
  }
}
Enter fullscreen mode Exit fullscreen mode

Right

class MerchantProcessor {
  processPayment(amount) {
    throw new Error('Should be overriden');
  }
}

class MockMerchantProcessor extends MerchantProcessor {
  processPayment(amount) {
     throw new Error('Will be implemented when needed');
  }
}

// or better...

class MockMerchantProcessor extends MerchantProcessor {
  processPayment(amount) {
    console.log('Mock payment processed: $${amount}');
  }
}


Enter fullscreen mode Exit fullscreen mode

Detection

[X] Manual

Since empty code is valid sometimes only a good peer review will find these problems.

Tags

  • Hierarchies

Conclusion

Being lazy and deferring certain decisions is acceptable, but it's crucial to be explicit about it.

Relations

More Info

Disclaimer

Code Smells are my opinion.

Credits

Photo by Joey Kyber on Unsplash


There is an art to knowing where things should be checked and making sure that the program fails fast if you make a mistake. That kind of choosing is part of the art of simplification.

Ward Cunningham


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 (1)

Collapse
 
devtronic profile image
Julian Finkler

This is also a good indicator of the disregard of the interface segregation principle 🙂

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