Single Purpose Variables

Review code for variables serving multiple purposes and suggest single-purpose replacements.

41|27|Updated Oct 6, 2025
One-click install
npx skills add https://github.com/obra/clank --skill single-purpose-variables-obra
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Single Purpose Variables
Source: https://github.com/obra/clank/tree/main/skills/coding/single-purpose-variables
Command: npx skills add https://github.com/obra/clank --skill single-purpose-variables-obra

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill eliminates code confusion caused by variables that serve multiple purposes or have hidden meanings, making your code more readable and maintainable.

Core Features & Use Cases

  • Hybrid Coupling Prevention: Stop using variables that represent different types (like count and error flags).
  • Hidden Meaning Elimination: Separate variables with special value interpretations into clear, distinct variables.
  • Use Case: When your code uses page_count = -1 to indicate an error instead of a count, this Skill helps you create separate variables for the count and error state.

Quick Start

Review this code for variables that serve multiple purposes and suggest improvements to make each variable have a single clear meaning.

Frequently Asked Questions about Single Purpose Variables

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix variables that represent different things at different times in my code?

Variables serving multiple purposes create confusion and bugs. Separate them into distinct variables—use one for counts, another for error states, and clear names for each concept. This eliminates hybrid coupling and makes your code's intent explicit.

Why is using sentinel values like -1 to signal errors a problem?

Sentinel values like -1 force a variable to mean two things: a valid result or an error flag. This hidden meaning breaks readability and causes bugs. Replace it with separate variables—one for the actual value, one for error status—with explicit error handling.

What does single-purpose variable design mean for code maintainability?

Single-purpose variables ensure each name clearly represents one concept. Maintainers instantly understand what a variable holds without decoding hidden meanings or tracking state changes. This practice applies across all languages and improves code clarity.

When should I refactor code to use separate variables instead of reusing one?

Refactor when a variable holds different types of data at different times—counts mixed with flags, temporary values repurposed later, or special values encoding multiple states. Each distinct concept deserves its own variable with a clear, specific name.

Does this approach work for all programming languages?

Yes. Single-purpose variable design is a language-agnostic best practice. Whether you code in Python, JavaScript, Java, or any other language, separating concerns into distinct variables improves readability and maintainability across your entire codebase.

What naming conventions help enforce single-purpose variables?

Use explicit, descriptive names that reveal intent: `error_occurred` instead of reusing `result`, `retry_count` instead of `counter`. Clear naming naturally prevents variable reuse and makes code reviews catch coupling issues before they cause bugs.