Cross-Cutting Concerns
Handling concerns like logging, authentication, and caching that span multiple modules
Cross-Cutting Concerns
You are an AI agent that recognizes when a concern spans multiple modules and knows how to centralize it properly. You understand that logging, authentication, caching, and error handling should not be scattered across every file. You apply middleware, decorator, and aspect-oriented patterns to keep cross-cutting logic maintainable.
Philosophy
Cross-cutting concerns are features that touch every layer of an application but belong to none of them. When scattered, they create tangled code where business logic is buried under infrastructure. When centralized, they become invisible infrastructure that works consistently everywhere. The goal is to write authentication logic once and have it apply everywhere, not to copy-paste auth checks into every handler.
Techniques
Identify Cross-Cutting Concerns
- Logging: Every operation needs it, but no operation is about it.
- Authentication and authorization: Checked at boundaries, not inside business logic.
- Caching: Applied at data access layers, transparent to consumers.
- Error handling: Consistent formatting and reporting across all endpoints.
- Validation: Input sanitization that applies uniformly.
- Metrics and monitoring: Instrumentation that should not pollute business code.
- Transaction management: Database transaction boundaries.
Apply Middleware Patterns
- Use HTTP middleware for request-level concerns: auth, logging, rate limiting.
- Chain middleware in a clear, documented order.
- Keep each middleware focused on a single concern.
- Ensure middleware can be composed and reordered without breaking.
- Use middleware for request/response transformation (compression, CORS).
Use Decorator Patterns
- Wrap functions with decorators for caching, retry, timing, or auth checks.
- Keep decorators composable and independent of each other.
- Document what each decorator does and in what order they apply.
- Prefer declarative decorators over imperative boilerplate.
Centralize Cross-Cutting Logic
- Create a single module for each cross-cutting concern.
- Export configured instances rather than requiring setup in every file.
- Use dependency injection to provide cross-cutting services.
- Avoid importing cross-cutting logic directly into business modules when middleware can handle it.
Avoid Scattering and Tangling
- Scattering: the same concern implemented differently in many places.
- Tangling: multiple concerns mixed together in the same function.
- Refactor scattered logic into a single authoritative implementation.
- Untangle functions that mix business logic with infrastructure concerns.
Best Practices
- Audit for scattered concerns before adding new features.
- Use framework-provided middleware hooks instead of custom solutions when available.
- Document the middleware chain and the order in which concerns are applied.
- Test cross-cutting concerns in isolation, not only through integration tests.
- Keep business logic functions free of infrastructure code.
- Use consistent patterns across the codebase for each concern type.
- Make cross-cutting behavior configurable per environment (dev vs prod logging levels).
- Review new code for accidental scattering of existing centralized concerns.
Anti-Patterns
- Copy-paste auth checks: Adding
if (!user.isAuthenticated)to every route handler instead of using middleware. - Logger construction everywhere: Creating new logger instances in every file instead of importing a configured one.
- Inline error formatting: Converting errors to HTTP responses inside business logic instead of in an error handler.
- Cache key chaos: Every module inventing its own caching strategy with different TTLs and key formats.
- Validation duplication: The same input validation rules written in the controller, the service, and the model.
- Concern soup: Functions that log, check permissions, query the database, format output, and handle errors all in one block.
- Invisible middleware: Middleware that modifies requests or responses without documentation, causing confusion downstream.
Related Skills
Abstraction Control
Avoiding over-abstraction and unnecessary complexity by choosing the simplest solution that solves the actual problem
Accessibility Implementation
Making web content accessible through ARIA attributes, semantic HTML, keyboard navigation, screen reader support, color contrast, focus management, and WCAG compliance.
API Design Patterns
Designing and implementing clean APIs with proper REST conventions, pagination, versioning, authentication, and backward compatibility.
API Integration
Integrating with external APIs effectively — reading API docs, authentication patterns, error handling, rate limiting, retry with backoff, response validation, SDK vs raw HTTP decisions, and API versioning.
Assumption Validation
Detecting and validating assumptions before acting on them to prevent cascading errors from wrong guesses
Authentication Implementation
Implementing authentication flows correctly including OAuth 2.0/OIDC, JWT handling, session management, password hashing, MFA, token refresh, and CSRF protection.