Skip to main content

Scattered Concerns

Description

What does it look like?

  • Logging logic and configuration access are written inconsistently across the codebase
  • Each part of the system logs messages or loads config values in its own way, lacking consistency
  • Implementation of logs and configs tends to vary depending on the developer

Why is it a problem?

  • Low maintainability and poor reusability
  • When log format or destination needs to change, the impact is widespread
  • Incorrect config access or repeated loading may lead to performance issues and bugs

Bad Example of the Anti-pattern

// あちこちで Logger や設定を直接扱っている
class OrderService {
createOrder(orderId: string) {
console.log(`[OrderService] 注文作成: ${orderId}`); // 直書きログ
const debugMode = process.env.DEBUG_MODE === "true"; // 設定の直接参照
if (debugMode) {
console.log(`[DEBUG] 注文詳細: ${orderId}`);
}
}
}

class UserService {
createUser(name: string) {
console.log(`[UserService] ユーザー作成: ${name}`);
}
}

Issues:

  • Direct use of console.log()Must update every log when the format changes
  • Direct access to process.envNo centralized configuration management
  • Responsibility is scattered and reuse is difficult

Refactoring by Pattern

Design patterns that can address this

PatternOverviewMain Refactoring Approach
SingletonRestrict instance to one across the applicationManage logs and configs as shared state
FacadeProvide a simplified API for logging and config accessCreate a unified interface for use