Mit Codex oder Claude installieren Kopieren Sie diesen Prompt, fügen Sie ihn in Codex, Claude oder einen anderen Assistant ein und lassen Sie die Skill-Seite prüfen und installieren.
Use this skill when restructuring existing code in the Smart Farming application. Refactoring must preserve behavior while improving code structure, readability, or maintainability.
Safety Protocol
Before ANY refactoring:
Run tests: pytest — Establish the baseline (all tests must pass)
Understand scope: Identify all files affected by the change
Make incremental changes: One logical change at a time
Re-run tests after each change: Catch regressions immediately
Commit after each safe step: Easy to revert if needed
Common Refactoring Patterns
Extract Shared Logic to Commons
When: Two or more use cases contain duplicate logic.
Where: src/app/core/commons/
# Before: duplicated in multiple use casesclassCreateSensorRecord:
defexecute(self, data):
# validation logic here...classEditSensorRecord:
defexecute(self, data):
# same validation logic here...# After: extracted to commons# core/commons/validate_sensor_data.pydefvalidate_sensor_data(data):
# shared validation logic# Use in both use casesfrom core.commons.validate_sensor_data import validate_sensor_data
Split Large Use Case
When: A use case has too many responsibilities (> 100 lines or multiple distinct operations).
Steps:
Identify distinct operations within the use case
Create new use case classes for each operation
Update the factory function to wire new dependencies
Update the view to use the appropriate use case
Move/create tests for each new use case
Consolidate Repository Methods
When: Multiple repository methods have similar SQL with minor variations.
# Before: separate methods with similar queriesdeffind_by_plant_id(self, plant_id): ...
deffind_by_date_range(self, start, end): ...
deffind_by_plant_and_date(self, plant_id, start, end): ...
# After: flexible query methoddeffind(self, filters: dict): ...
Rename for Clarity
When: Names don't clearly communicate intent.
Checklist:
Update class/function name
Update all imports
Update factory functions
Update test references
Update mock class names if interface changed
Move to Correct Layer
When: Code is in the wrong architectural layer.
Code Type
Correct Location
Business rules
core/use_cases/
Data validation
core/commons/ or infra/forms/
SQL queries
infra/repositories/
HTTP handling
infra/views/
Configuration
infra/constants/
Refactoring Checklist
All tests pass BEFORE starting
Change is isolated to one concern
No architectural boundary violations introduced
Interfaces updated if contracts change
Factory functions updated if dependencies change
Mock repositories updated if interfaces change
All tests pass AFTER the change
No unused imports or dead code left behind
Commit with :recycle: refactor(scope): description
Risky Refactors (Extra Caution)
Changing entity structure — Affects repositories, use cases, views, templates, and tests
Changing database schema — Requires SQL migration and may affect all layers
Renaming blueprints — Affects URL routing and template links
Modifying init_database() — Can destroy development data