menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

Code Smell...
source image

Dev

1w

read

285

img
dot

Image Credit: Dev

Code Smell 301 - Database as Parameter

  • Passing databases as parameters creates accidental coupling between domain logic and data persistence mechanisms, violating separation of concerns and making code harder to test, maintain, and evolve.
  • To address this code smell, use dependency injection, avoid using the Repository Pattern, separate business logic from data access concerns, and design for decoupling.
  • Examples of problems caused by passing databases as parameters include tight coupling, mixed responsibilities, violation of the Single Responsibility Principle, and blurred layers.
  • By refactoring to separate business logic and data access, such as in the 'Right' sample code provided, you can improve maintainability and testability of your code.
  • Detection of this code smell can be done semi-automatically by identifying database connections or ORM objects passed as parameters to business methods.
  • AI generation tools often introduce this code smell by directly coupling database access with business logic, prioritizing working code over clean architecture.
  • AI detection tools can identify this code smell by focusing on the separation of concerns and proper use of dependency injection patterns.
  • In conclusion, avoid passing databases as parameters to business objects to keep business logic clean, facilitate testing, and maintain a clear separation between domain and infrastructure concerns.
  • Related topics include articles on object keys, mocking business, accidental methods on business objects, and inappropriate intimacy in code design.
  • Code smells are subjective opinions, and the article emphasizes breaking down large applications into smaller, testable components for better maintainability.

Read Full Article

like

17 Likes

For uninterrupted reading, download the app