Skip to content
Back to skills

Common Best Practices

ASecurity

An 80-line function with five nesting levels is doing too much. Refactor it into a short orchestration function plus small, intention-revealing helpers. Preserve the existing behavior first; do not redesign domain rules while extracting code.

  • 584 stars
  • 0 votes
  • 0 copies
  • 7 views
  • Added September 5, 2026
development

Security analysis

A100/100

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

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill common-best-practices --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Common Best Practices?

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

Security grade badge for Common Best Practices
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-common-best-practices-27f566e8/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-common-best-practices-27f566e8)

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
# Refactor the deeply nested function

An 80-line function with five nesting levels is doing too much. Refactor it into a short orchestration function plus small, intention-revealing helpers. Preserve the existing behavior first; do not redesign domain rules while extracting code.

## Approach

1. Identify the function's input validation, decision logic, transformation, and side effects.
2. Move validation to guard clauses at the boundary.
3. Give each meaningful branch a named predicate or handler.
4. Keep the public function responsible only for orchestration; put persistence, I/O, and business rules behind injected dependencies.
5. Keep every function below 30 lines and remove comments that merely describe control flow. Retain comments only for non-obvious business reasons.

For example, a nested workflow can be reshaped like this:

```ts
type WorkflowRequest = {
  kind: string;
  payload: unknown;
};

type WorkflowResult =
  | { ok: true; value: unknown }
  | { ok: false; reason: string };

type WorkflowDependencies = {
  isSupportedKind: (kind: string) => boolean;
  validatePayload: (payload: unknown) => boolean;
  execute: (request: WorkflowRequest) => unknown;
};

export function runWorkflow(
  request: WorkflowRequest | null,
  dependencies: WorkflowDependencies,
): WorkflowResult {
  if (!request) return { ok: false, reason: "request is required" };
  if (!dependencies.isSupportedKind(request.kind)) {
    return { ok: false, reason: "unsupported workflow kind" };
  }
  if (!dependencies.validatePayload(request.payload)) {
    return { ok: false, reason: "invalid workflow payload" };
  }

  return { ok: true, value: dependencies.execute(request) };
}
```

The actual predicates and `execute` implementation should be extracted from the original function, not invented. If the original function has several independent operations, split those operations into functions such as `validateRequest`, `selectWorkflowHandler`, and `persistWorkflowResult`. If new variants are expected, use a map of handlers or strategy objects so adding a variant does not require another long conditional chain (Open/Closed Principle).

Before declaring the refactor complete, compare old and new behavior for valid input, missing input, invalid input, every branch, dependency failures, and side-effect ordering. Add focused tests around the extracted helpers and one integration test for the orchestration function.


Files in this skill

  • eval-1.baseline.md1.7 KB
  • eval-1.with-skill.md2.4 KB
  • eval-2.baseline.md1.4 KB
  • eval-2.with-skill.md1.9 KB
  • eval-3.baseline.md1.5 KB
  • eval-3.with-skill.md2.1 KB
  • trigger-1.md180 B
  • trigger-2.md184 B
  • trigger-3.md164 B
  • trigger-4.md158 B

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.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.