Use when organizing code structure, creating new features, or reviewing file placement to keep related code together by scope and reduce cognitive load.
Keep related code together by scope. Scoped colocation organizes files by feature or domain rather than technical layer, reducing navigation overhead and making the codebase self-documenting.
Principle: Code that changes together should live together. When you modify a feature, you should not need to navigate across multiple unrelated directories.
REQUIRED BACKGROUND: superpowers:verification-before-completion
Always triggered for:
Organize by feature/domain, not by technical concern:
# Anti-pattern: Technical layers
src/
controllers/
OrderController.cs
CustomerController.cs
services/
OrderService.cs
CustomerService.cs
models/
Order.cs
Customer.cs
# Preferred: Feature-scoped
src/
Orders/
OrderController.cs
OrderService.cs
Order.cs
OrderTests.cs
Customers/
CustomerController.cs
CustomerService.cs
Customer.cs
CustomerTests.cs
Tests live next to the code they test:
# Anti-pattern: Separate test tree
src/
Components/
Button.tsx
tests/
Components/
Button.test.tsx
# Preferred: Colocated tests
src/
Components/
Button.tsx
Button.test.tsx
Related assets (styles, types, fixtures) stay with their component:
src/
Features/
Dashboard/
Dashboard.tsx
Dashboard.module.css
Dashboard.types.ts
Dashboard.test.tsx
dashboard-fixtures.json
Extract only when genuinely shared (used in 3+ places):
src/
Features/
Orders/
... (feature-specific code)
Customers/
... (feature-specific code)
Shared/
Components/
Button.tsx # Used by Orders, Customers, Reports
Utils/
formatDate.ts # Used across multiple features
Rule: Do not prematurely extract. Keep code in features until sharing becomes necessary.
1. Same file (functions, types)
2. Same directory (component + test + styles)
3. Feature directory (all feature-related code)
4. Domain directory (related features grouped)
5. Shared (cross-cutting, stable, reusable)
utils/ or helpers/ file for single-feature code__tests__/ tree far from implementationAll mean: Consider if the code belongs in a feature directory instead.
| Benefit | Description |
|---|---|
| Reduced navigation | Related files in same directory |
| Self-documenting | Directory structure reveals feature boundaries |
| Easier deletion | Remove feature by deleting one directory |
| Clear ownership | Obvious which team owns which code |
| Simpler imports | Shorter, more intuitive import paths |
| Better encapsulation | Feature internals hidden from other features |
src/
features/
auth/
LoginForm.tsx
LoginForm.test.tsx
LoginForm.module.css
useAuth.ts
useAuth.test.ts
auth.types.ts
dashboard/
Dashboard.tsx
Dashboard.test.tsx
DashboardWidget.tsx
useDashboardData.ts
src/
Features/
Orders/
CreateOrderCommand.cs
CreateOrderCommandHandler.cs
CreateOrderCommandTests.cs
OrderRepository.cs
OrderDto.cs
Customers/
GetCustomerQuery.cs
GetCustomerQueryHandler.cs
CustomerService.cs
src/
features/
orders/
__init__.py
routes.py
service.py
models.py
test_orders.py
customers/
__init__.py
routes.py
service.py
models.py
test_customers.py
Certain code legitimately belongs in shared/common locations:
These exceptions should be documented in the project's architecture documentation.
Before declaring structure complete:
project/
āāā src/
ā āāā features/
ā ā āāā auth/
ā ā ā āāā components/
ā ā ā ā āāā LoginForm.tsx
ā ā ā ā āāā LoginForm.test.tsx
ā ā ā ā āāā LoginForm.module.css
ā ā ā āāā hooks/
ā ā ā ā āāā useAuth.ts
ā ā ā ā āāā useAuth.test.ts
ā ā ā āāā api/
ā ā ā ā āāā authApi.ts
ā ā ā ā āāā authApi.test.ts
ā ā ā āāā index.ts # Public exports
ā ā ā
ā ā āāā orders/
ā ā ā āāā components/
ā ā ā ā āāā OrderList.tsx
ā ā ā ā āāā OrderList.test.tsx
ā ā ā ā āāā OrderDetail.tsx
ā ā ā ā āāā OrderDetail.test.tsx
ā ā ā āāā hooks/
ā ā ā ā āāā useOrders.ts
ā ā ā ā āāā useOrders.test.ts
ā ā ā āāā types/
ā ā ā ā āāā order.types.ts
ā ā ā āāā index.ts
ā ā ā
ā ā āāā dashboard/
ā ā āāā Dashboard.tsx
ā ā āāā Dashboard.test.tsx
ā ā āāā widgets/
ā ā āāā SalesWidget.tsx
ā ā āāā SalesWidget.test.tsx
ā ā
ā āāā shared/ # Only truly shared (3+ consumers)
ā ā āāā components/
ā ā ā āāā Button.tsx
ā ā ā āāā Button.test.tsx
ā ā ā āāā Modal.tsx
ā ā āāā utils/
ā ā āāā formatDate.ts
ā ā āāā formatDate.test.ts
ā ā
ā āāā infrastructure/ # Framework/cross-cutting
ā āāā http/
ā ā āāā apiClient.ts
ā āāā store/
ā āāā configureStore.ts
ā
āāā package.json
src/
āāā Domain/ # Core business logic
ā āāā Orders/
ā ā āāā Order.cs
ā ā āāā OrderItem.cs
ā ā āāā IOrderRepository.cs
ā āāā Customers/
ā āāā Customer.cs
ā āāā ICustomerRepository.cs
ā
āāā Application/ # Use cases
ā āāā Orders/
ā ā āāā Commands/
ā ā ā āāā CreateOrder/
ā ā ā ā āāā CreateOrderCommand.cs
ā ā ā ā āāā CreateOrderHandler.cs
ā ā ā ā āāā CreateOrderValidator.cs
ā ā ā āāā CancelOrder/
ā ā ā āāā CancelOrderCommand.cs
ā ā ā āāā CancelOrderHandler.cs
ā ā āāā Queries/
ā ā āāā GetOrder/
ā ā āāā GetOrderQuery.cs
ā ā āāā GetOrderHandler.cs
ā āāā Common/
ā āāā Behaviors/ # Cross-cutting (logging, validation)
ā
āāā Infrastructure/ # External concerns
ā āāā Persistence/
ā ā āāā Orders/
ā ā ā āāā OrderRepository.cs
ā ā āāā AppDbContext.cs
ā āāā ExternalServices/
ā
āāā WebApi/ # Presentation
āāā Controllers/
ā āāā OrdersController.cs
ā āāā CustomersController.cs
āāā Program.cs
tests/
āāā Domain.Tests/
ā āāā Orders/
ā āāā OrderTests.cs
āāā Application.Tests/
ā āāā Orders/
ā āāā CreateOrderHandlerTests.cs
āāā WebApi.Tests/
āāā Orders/
āāā OrdersControllerTests.cs
src/
āāā features/
ā āāā auth/
ā ā āāā __init__.py
ā ā āāā router.py
ā ā āāā service.py
ā ā āāā models.py
ā ā āāā schemas.py
ā ā āāā test_auth.py
ā ā
ā āāā orders/
ā ā āāā __init__.py
ā ā āāā router.py
ā ā āāā service.py
ā ā āāā models.py
ā ā āāā schemas.py
ā ā āāā test_orders.py
ā ā
ā āāā customers/
ā āāā __init__.py
ā āāā router.py
ā āāā test_customers.py
ā
āāā shared/
ā āāā __init__.py
ā āāā database.py
ā āāā utils/
ā āāā __init__.py
ā āāā date_utils.py
ā
āāā main.py
āāā conftest.py # Shared test fixtures
Use this checklist to audit existing codebases:
For each feature identified:
| Feature | Code Location | Test Location | Colocated? | Action |
|---|---|---|---|---|
| Auth | src/features/auth | tests/auth | ā Yes | None |
| Orders | src/services/ | tests/services/ | ā No | Move |
| Utils | src/utils/ | tests/utils/ | ā Check | Review usage |
For each file in shared/, common/, utils/:
| File | Used By | Count | Action |
|---|---|---|---|
| formatDate.ts | auth, orders, reports | 3 | Keep in shared |
| orderHelpers.ts | orders only | 1 | Move to orders/ |
| constants.ts | auth, orders | 2 | Keep but watch |
Based on analysis, prioritize:
#!/bin/bash
# colocation-audit.sh
echo "=== Colocation Audit ==="
echo ""
# Check for common anti-patterns
echo "## Anti-pattern Detection"
# Separate test directories
if [ -d "tests" ] || [ -d "__tests__" ]; then
TEST_COUNT=$(find . -name "*.test.*" -o -name "*_test.*" | wc -l)
COLOCATED=$(find src -name "*.test.*" -o -name "*_test.*" 2>/dev/null | wc -l)
echo "Total tests: $TEST_COUNT"
echo "Colocated tests: $COLOCATED"
if [ "$COLOCATED" -lt "$TEST_COUNT" ]; then
echo "ā Some tests not colocated with source"
fi
fi
# Check utils/helpers directories
for dir in utils helpers common; do
if [ -d "src/$dir" ]; then
FILE_COUNT=$(find "src/$dir" -type f -name "*.ts" -o -name "*.js" -o -name "*.py" | wc -l)
echo "ā Found src/$dir with $FILE_COUNT files - review for single-feature code"
fi
done
# Check for feature directories
echo ""
echo "## Feature Directory Detection"
if [ -d "src/features" ]; then
echo "ā Feature-based structure detected"
ls -d src/features/*/ 2>/dev/null | while read dir; do
FEATURE=$(basename "$dir")
TEST_FILES=$(find "$dir" -name "*.test.*" -o -name "*_test.*" | wc -l)
echo " $FEATURE: $TEST_FILES colocated tests"
done
else
echo "ā No features/ directory - consider restructuring"
fi
echo ""
echo "=== Audit Complete ==="
## Colocation Audit Results
**Repository**: [repo-name]
**Date**: YYYY-MM-DD
**Auditor**: [name]
### Current Structure
[Describe current organization pattern]
### Findings
| Category | Status | Details |
| -------------------- | ------ | ------------------------- |
| Feature directories | ā/ā | [describe] |
| Test colocation | ā/ā | [X]% colocated |
| Shared code validity | ā/ā | [X] files, [Y] single-use |
### Recommended Actions
1. [ ] Move [X] to feature directory [Y]
2. [ ] Colocate tests for [feature]
3. [ ] Review shared/utils for single-use code
### Verification
After refactoring:
- [ ] All tests pass
- [ ] Import paths updated
- [ ] No orphaned files