- Complexity Check: Count conditions/branches - if >5, plan to split the function
- Length Check: If logic seems like >15 lines, design smaller helper functions first
- Parameter Check: If >4 parameters needed, consider using a data class/dict
- Duplication Check: If similar logic exists elsewhere, extract to shared utility first
- Constants First: Define any magic numbers/strings as named constants immediately
- Specific Exceptions: Never use bare
except Exception:- always specify expected exceptions - Single Responsibility: Each function should do ONE clear thing
- Early Returns: Use guard clauses to reduce nesting depth
- Self-Review: Apply full SonarQube checklist from
sonarqube.md - Complexity Verification: No function >10 cyclomatic complexity
- Duplication Scan: No repeated code blocks >3 lines
- Error Handling: All database operations have proper rollback patterns
- STOP and PLAN if a function is getting complex
- EXTRACT immediately when I see duplication
- REFACTOR in real-time rather than "fix later"
- Apply SonarQube rules as I write, not after
- Function approaching 15 lines → Extract helper methods
- Seeing similar try/except blocks → Create error handling utility
- Multiple if/elif chains → Consider strategy pattern or lookup tables
- Magic numbers → Define as named constants
- Generic exception handling → Specify expected exceptions
- No function >15 lines without documented justification
- No cyclomatic complexity >10
- No code duplication >3 lines
- No magic numbers (except 0, 1, -1 in obvious contexts)
- Always sort imports using standard Python import order:
- Standard library imports
- Third-party imports
- Local application imports
- Group imports with blank lines between sections
- Use absolute imports where possible
- Remove unused imports immediately (F401 prevention)
- Handle GStreamer imports correctly:
- Place
gi.require_version()calls before importing fromgi.repository - Add
# noqa: E402to imports that must come aftergi.require_version() - Example:
import gi from .config.models import CameraConfig # noqa: E402 gi.require_version("Gst", "1.0") from gi.repository import GLib, Gst # noqa: E402
- Place
- Use built-in types instead of typing module equivalents:
listinstead oftyping.Listdictinstead oftyping.Dictsetinstead oftyping.Settupleinstead oftyping.Tuple
- Use union syntax
X | Noneinstead ofOptional[X]orUnion[X, None] - Use modern exception types
TimeoutErrorinstead ofsocket.timeout - Always add return type annotations to functions (prevents no-untyped-def)
- Add proper type guards for nullable types:
- Check
if not variable:before indexing operations onType | None - Example:
# BAD - mypy error self.memory_map[0:10] = data # memory_map: mmap.mmap | None # GOOD - with type guard if not self.memory_map: raise Error("Not initialized") self.memory_map[0:10] = data
- Check
- No unused variables - remove or prefix with underscore if intentionally unused
- Use descriptive names - avoid single letter variables except for loops
- Clean up loop variables - don't leave them accessible after loops
- Secure file permissions - use 0o644 for files, 0o755 for directories, never 0o666
- Secure temp file usage - use
tempfile.mkstemp()ortempfile.NamedTemporaryFile() - Proper socket permissions - use appropriate permissions for Unix sockets
- Clean resource cleanup - always use context managers or try/finally
- Never use bare except - always specify expected exception types
- Use modern exception types -
TimeoutErrornotsocket.timeout - No redundant exception classes - don't catch both parent and child exceptions
- Proper exception chaining - use
raise ... from errfor context
- No redundant list() calls - don't wrap already iterable objects
- No redundant type conversions - check if conversion is needed
- Use comprehensions - prefer list/dict comprehensions over loops where appropriate
- Avoid repeated calculations - cache expensive operations
- Check imports - are they sorted and modern?
- Plan type hints - what types will parameters and return be?
- Consider exceptions - what specific errors might occur?
- Check for duplication - does similar code already exist?
- Security review - any file/network operations that need securing?
- Run import sorter - ensure I001 compliance
- Check type annotations - all functions have return types
- Remove unused imports/variables - clean F401/F841 issues
- Verify exception handling - specific, not generic
- Security scan - no permissive permissions or insecure temp files
- Efficiency check - no redundant operations