xiaohongshu
xiaohongshu
fix-bad-practices
Fix bad coding practices identified by audit, following fail-fast principles
Full skill instructions
Systematically fix bad coding practices identified by audit, following fail-fast principles.
Use this skill:
/audit-code-qualityWhen fixing bad practices:
raise from or raiseexcept: pass → Specific Exception + LoggingBefore (BAD):
try:
do_something()
except:
pass
After (GOOD):
try:
do_something()
except SpecificError as e:
logger.exception("Failed to do_something: %s", e)
raise
Or if recovery is possible:
try:
result = do_something()
except SpecificError as e:
logger.warning("do_something failed, using fallback: %s", e)
result = fallback_value # Only if fallback is valid!
except Exception → Specific ExceptionsBefore (BAD):
try:
data = parse_json(text)
except Exception as e:
logger.error(e)
return None
After (GOOD):
try:
data = parse_json(text)
except json.JSONDecodeError as e:
logger.exception("Invalid JSON: %s", e)
raise ValueError(f"Cannot parse JSON: {e}") from e
except FileNotFoundError as e:
logger.exception("File not found: %s", e)
raise
continue → Explicit Error HandlingBefore (BAD):
for item in items:
if not item.is_valid():
continue
process(item)
After (GOOD) - Option A: Fail on first error:
for item in items:
if not item.is_valid():
raise ValueError(f"Invalid item: {item}")
process(item)
After (GOOD) - Option B: Collect errors, report at end:
errors = []
for item in items:
if not item.is_valid():
errors.append(f"Invalid item: {item}")
continue
process(item)
if errors:
raise ValueError(f"Processing failed with {len(errors)} invalid items:\n" +
"\n".join(errors))
After (GOOD) - Option C: Log warning if skip is intentional:
for item in items:
if not item.is_valid():
logger.warning("Skipping invalid item: %s (reason: %s)", item, item.validation_error)
continue
process(item)
return None on Error → Raise ExceptionBefore (BAD):
def load_config(path):
if not os.path.exists(path):
return None
with open(path) as f:
return json.load(f)
After (GOOD):
def load_config(path: Path) -> dict:
"""Load configuration from path.
Raises:
FileNotFoundError: If config file doesn't exist
json.JSONDecodeError: If config is not valid JSON
"""
if not path.exists():
raise FileNotFoundError(f"Config file not found: {path}")
with open(path) as f:
return json.load(f)
Before (BAD):
def process_data(data):
if not data:
return -1, "No data provided"
if not validate(data):
return -2, "Invalid data"
result = transform(data)
return 0, result
After (GOOD):
def process_data(data):
"""Process the data.
Raises:
ValueError: If data is empty or invalid
"""
if not data:
raise ValueError("No data provided")
if not validate(data):
raise ValueError(f"Invalid data: {get_validation_errors(data)}")
return transform(data)
Before (BAD):
def process(data):
if data is None:
return
# ... rest of processing
After (GOOD) - If None is never valid:
def process(data):
if data is None:
raise ValueError("data cannot be None")
# ... rest of processing
After (GOOD) - If function should handle None gracefully:
def process(data: Optional[Data]) -> Optional[Result]:
"""Process data if provided.
Args:
data: Data to process, or None to skip processing
Returns:
Result if data was processed, None if data was None
"""
if data is None:
logger.debug("process() called with None, returning None")
return None
# ... rest of processing
Before (BAD):
subprocess.run(["git", "commit", "-m", "message"])
After (GOOD):
try:
subprocess.run(
["git", "commit", "-m", "message"],
check=True,
capture_output=True,
text=True
)
except subprocess.CalledProcessError as e:
logger.exception("Git commit failed: %s\nstderr: %s", e, e.stderr)
raise
Before (BAD) - Adds noise:
try:
do_something()
except SomeError as e:
logger.error(f"Error: {e}")
raise
After (GOOD) - Option A: Add context:
try:
do_something()
except SomeError as e:
logger.exception("Failed during do_something in context X")
raise RuntimeError(f"Operation failed in context X") from e
After (GOOD) - Option B: Just let it propagate:
do_something() # Let caller handle the exception
Ensure each module has a logger:
import logging
logger = logging.getLogger(__name__)
| Level | Use For |
|---|---|
logger.debug() | Detailed diagnostic info |
logger.info() | Normal operation milestones |
logger.warning() | Recoverable issues, degraded operation |
logger.error() | Errors that don't stop execution |
logger.exception() | Errors with stack trace (use in except blocks) |
logger.critical() | Fatal errors, application cannot continue |
# GOOD - Use exception() in except blocks (includes stack trace)
except SomeError as e:
logger.exception("Operation failed: %s", e)
# GOOD - Use lazy formatting
logger.debug("Processing item %s of %s", i, total)
# BAD - Eager string formatting
logger.debug(f"Processing item {i} of {total}") # Formats even if debug disabled
# GOOD - Include relevant context
logger.error("Failed to load preset '%s' from %s: %s", preset_id, path, e)
# BAD - Vague messages
logger.error("Error occurred")
# Run audit first
/audit-code-quality
# Or run grep patterns directly
grep -rn "except.*:$" --include="*.py" -A1 | grep -B1 "pass$"
Priority order:
except: pass or except Exception: pass (CRITICAL)except Exception (HIGH)For each issue:
# Run tests
uv run pytest MouseMaster/Testing/Python/ -v
# Run linting
uv run ruff check .
# Re-run audit to confirm
/audit-code-quality
For simple patterns, use sed (review changes before committing!):
#!/bin/bash
# CAUTION: Review all changes manually!
# Find files with except:pass (for manual review)
echo "Files needing manual review:"
grep -rl "except.*:" --include="*.py" | while read f; do
if grep -q "pass$" "$f"; then
echo " $f"
fi
done
Note: Automated fixes are risky. Always review manually and run tests.
Is this a programming error (bug)?
├─ Yes → Let it crash (don't catch)
└─ No → Can user/system recover?
├─ Yes → Catch, log, handle gracefully
└─ No → Catch, log with context, re-raise or wrap
When catching:
├─ Can you name the specific exception?
│ ├─ Yes → Catch that specific type
│ └─ No → Research what exceptions can occur
└─ Is logging added?
├─ Yes → Good
└─ No → Add logger.exception() call
Not all exception handling is bad. Valid cases include:
try:
value = int(user_input)
except ValueError:
logger.warning("Invalid input '%s', prompting user", user_input)
show_error_to_user("Please enter a valid number")
try:
import optional_dependency
FEATURE_AVAILABLE = True
except ImportError:
logger.info("Optional feature unavailable: optional_dependency not installed")
FEATURE_AVAILABLE = False
try:
resource = acquire_resource()
use_resource(resource)
finally:
resource.cleanup() # Always runs
for attempt in range(max_retries):
try:
return do_operation()
except TransientError as e:
logger.warning("Attempt %d failed: %s", attempt + 1, e)
if attempt == max_retries - 1:
raise
time.sleep(backoff)
try:
result = external_api.call()
except ExternalAPIError as e:
logger.exception("External API failed")
raise OurDomainError(f"Service unavailable: {e}") from e
After fixing, add tests for error conditions:
def test_load_config_missing_file():
"""Test that missing config raises FileNotFoundError."""
with pytest.raises(FileNotFoundError, match="Config file not found"):
load_config(Path("/nonexistent/path"))
def test_process_invalid_data():
"""Test that invalid data raises ValueError with details."""
with pytest.raises(ValueError, match="Invalid data"):
process_data({"bad": "data"})
After fixes:
fix: replace exception swallowing with proper error handling
- Remove except:pass in module_name.py
- Add specific exception types with logging
- Add error path tests
Follows fail-fast principle: errors now surface immediately
with full context for debugging.
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience