Skip to content
Back to skills

Code Patterns 2

ASecurity

Code implementation patterns and best practices learned from real mistakes. Covers file I/O safety (locking, atomic writes), data persistence, and validation. Use for file-based state, shared resources, concurrent access. NOT for auto-learning patterns (see learned-patterns skill).

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 27, 2026
businessrust

Security analysis

A100/100

Pro scans all 4 files and shows the line behind each finding

Scanned September 27, 2026

npx -y skills add David-Li0406/meta-skill-evloving --skill code-patterns-2 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Code Patterns 2?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Code Patterns 2
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/david-li0406-code-patterns-2/badge)](https://www.skillsdirectory.com/skills/david-li0406-code-patterns-2)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: code-patterns
description: Code implementation patterns and best practices learned from real mistakes. Covers file I/O safety (locking, atomic writes), data persistence, and validation. Use for file-based state, shared resources, concurrent access. NOT for auto-learning patterns (see learned-patterns skill).
version: 1.0.0
languages: all
---

# Code Patterns

Best practices distilled from implementation lessons. These patterns prevent common bugs in file I/O, data persistence, and validation.

## Core Principle

**LEARN FROM MISTAKES - DON'T REPEAT THEM**

Each pattern here was extracted from a real bug or issue. Apply these patterns proactively to prevent the same mistakes.

## When to Use

**Always use for:** File-based state, shared resources, data persistence, validation layers, concurrent access

**Especially when:** Multiple processes/hooks access same file, persisting JSON data, creating factory functions, implementing save/load logic

## The Patterns

### 1. File Locking (`references/file-io.md`)

Advisory file locking for shared state access.

**Problem:** Multiple processes writing to same file causes data corruption or race conditions.

**Solution:** Use `.lock` file pattern with timeout and stale lock detection.

**Load when:** Implementing file-based state accessed by multiple hooks/processes

### 2. Atomic Writes (`references/data-persistence.md`)

Safe file persistence that survives crashes.

**Problem:** `writeFileSync()` mid-write crash corrupts file.

**Solution:** Write to `.tmp`, rename to final path (atomic on POSIX), handle Windows with backup pattern.

**Load when:** Persisting any JSON/data file that must survive unexpected termination

### 3. Schema Validation (`references/data-validation.md`)

Validate before every persist operation.

**Problem:** Factory functions create invalid data that persists and causes downstream issues.

**Solution:** Validate at creation AND before every write. Never trust "it was validated earlier."

**Load when:** Creating factory functions, implementing create/update operations, building data pipelines

## Quick Reference

```
Shared file access → file-io.md (add locking)
JSON persistence → data-persistence.md (atomic writes)
Factory function → data-validation.md (validate output)
```

## Prevention Checklist

Before implementing file I/O:
- [ ] Will multiple processes access this file?
- [ ] What happens if write crashes mid-operation?
- [ ] Is data validated before every write?
- [ ] Are stale locks handled (dead process detection)?

## Origin

These patterns were extracted from ACE (Agentic Context Engineering) implementation review:
- Race condition in concurrent delta writes → file-io.md
- File corruption risk on crash → data-persistence.md
- Invalid deltas persisting → data-validation.md

## Task Planning Notes

- Always plan and break many small todo tasks
- Always add a final review todo task to review the works done at the end to find any fix or enhancement needed

Files in this skill

  • SKILL.md2.9 KB
  • references/data-persistence.md4.5 KB
  • references/data-validation.md6.1 KB
  • references/file-io.md3.7 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…