Sobes.tech
Middle

What is an antipattern and which antipatterns are you familiar with?

sobes.tech AI

Answer from AI

An antipattern is a common but ineffective or harmful solution to a typical problem that leads to negative consequences.

Some well-known antipatterns in Java development:

  • God Object: A class that performs too many functions, has too many dependencies, and is too large. Violates the Single Responsibility Principle (SRP).
  • Magic Strings/Numbers: Using string or numeric constants directly in code instead of named constants or enumerations. Complicates maintenance and refactoring.
  • Hardcoding: Embedding configuration parameters, file paths, or other values directly in code instead of using external sources (configuration files, environment variables).
  • Spaghetti Code: Code with tangled and unstructured logic, with many jumps (e.g., goto - rarely used in modern Java, but metaphorically applicable to complex conditions and calls), making it difficult to understand and modify.
  • Copy-Paste Programming: Repeating the same or similar code in multiple places instead of creating reusable functions or classes. Violates the DRY (Don't Repeat Yourself) principle.
  • Large Class: A class that has too many lines of code, methods, and fields. Often a consequence of the God Object, but can also arise from insufficient separation of responsibilities.
  • Feature Envy: A method of class A that too often accesses data or behavior of class B, instead of being part of class B. Indicates incorrect placement of functionality.
  • Primitive Obsession: Excessive use of primitive types (e.g., int, String) to represent domain concepts instead of creating specialized value object classes. For example, representing a monetary amount as double instead of a Money class.
  • Dead Code: Code that is never executed (not called, unreachable). Clutters the codebase and complicates understanding.
  • Excessive Coupling: Classes or modules that depend too heavily on each other. Changing one component requires changes in many others.
  • Laid-Back Framework: Creating a custom, incomplete framework or library to solve a problem for which mature and widely accepted solutions already exist.

Understanding and avoiding these antipatterns improves code quality, maintainability, and extensibility.