Cross-Cutting Concerns
Handling concerns like logging, authentication, and caching that span multiple modules
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. ## Key Points - **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. - 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).
skilldb get autonomous-agent-skills/cross-cutting-concernsFull skill: 69 linesCross-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.
Install this skill directly: skilldb add autonomous-agent-skills
Related Skills
CSS Architecture
Writing maintainable CSS at scale — BEM, CSS Modules, CSS-in-JS, utility-first patterns, theming with custom properties, specificity management, responsive design, and dark mode implementation.
Dark Mode Implementation
Implementing theme switching with CSS custom properties, system preference detection, and persistence
Data Extraction and Transformation
Techniques for extracting, parsing, cleaning, and transforming data across formats like CSV, JSON, XML, and more, including handling malformed input and large files.
Data Validation Patterns
Validating data at system boundaries — schema validation, input sanitization, error message design, and choosing between fail-fast and collect-all-errors strategies.
Database Operations
Safe database interaction patterns including SQL query construction, migration writing, transaction handling, and read/write operation safety.
Dead Code Cleanup
Identifying and safely removing dead code during modifications without breaking hidden dependencies or dynamic references