Skip to content

Latest commit

 

History

History
44 lines (27 loc) · 1.19 KB

StaticMethodAbuse.mdwn

File metadata and controls

44 lines (27 loc) · 1.19 KB

[[!taglink AntiPattern]]

Static Method Abuse

Context

  • methods operate on groups of objects
  • only one instance of an object would ever exist

Forces

  • procedural code is frequently easier to write
  • object-oriented code is more difficult to understand
  • operations on primitives are more efficient
  • global methods are easy to invoke
  • object instantiation is expensive for a single method call

Supposed Solution

  • make all methods static (i.e., global)

Resulting Context

When all methods are static, unit testing is difficult, since all classes are tightly coupled. There is no seam for dependency injection, so all validations must test the system as a whole.

Polymorphism is impossible, since static methods cannot be overridden. This makes it difficult to handle duplication and special cases cleanly.

Code reuse is more trouble than it's worth, because all code reuse is [[PasteDrivenDevelopment]].

Related AntiPatterns

  • [[MonolithicServiceClass]]
  • [[MonolithicTestFactory]]

Applicable Positive Patterns

  • [[CommandPattern]]

Suggested [[Refactoring]]s

  • [[EncapsulateCollection]]
  • [[ExtractClass]]
  • [[IntroduceParameterObject]]
  • [[MoveMethod]]