DEV Community

loading...
Cover image for Code Smell 18 — Static Functions

Code Smell 18 — Static Functions

mcsee profile image Maxi Contieri Originally published at Medium Updated on ・1 min read

Yet another global access coupled with laziness.

Problems

  • Coupling

  • Testability

  • Protocol Overloading

  • Cohesion

Solutions

Examples

  • Static class methods

  • Static attributes

Sample Code

Wrong

Right

Detection

We can enforce a policy to avoid static methods (all class methods but constructors).

Tags

  • Global

  • Libraries

Conclusion

Class are globals disguised. Polluting their protocol with "library methods" breaks cohesion and generates coupling. We should extract static with refactorings.

In most languages we cannot manipulate classes and use them polymorphically, so we can't mock them or plug them on tests.

Therefore, we have a global reference too difficult too decouple.

Relations

More info

Credits

Photo by Alex Azabache on Unsplash


There is no programming problem that can't be solved with one more level of indirection.

John McCarthy

Discussion (0)

pic
Editor guide