Aikido

Why you should make a function’s purpose self evident for clearer code

Readability

Rule
Make a function's purpose self-evident.
Function names should clearly communicate what the function
does without requiring readers to examine the implementation.

Supported languages: 45+

Introduction

Function names that don't reveal their purpose force developers to read the entire implementation to understand behavior. A function named process() or handle() could do anything, requiring mental effort to deduce its actual purpose. Clear, descriptive names like validateUserEmail() or calculateOrderTotal() communicate intent immediately, making code self-documenting.

Why it matters

Code maintainability: Unclear function names slow down comprehension. Developers waste time reading implementations to understand what functions do instead of focusing on business logic. This compounds when returning to code months later or when new team members join.

Incorrect usage: Ambiguous function names increase the chance of misuse. A function named update() might validate, transform, persist, or notify, leading developers to use it incorrectly because the name doesn't specify behavior. Clear names prevent misuse by documenting constraints and side effects.

Code examples

❌ Non-compliant:

function process(data) {
    const result = data.map(item => ({
        ...item,
        processed: true,
        timestamp: Date.now()
    }));
    db.save(result);
    notifyService.send(result);
    return result;
}

Why it's wrong: The name process() reveals nothing about what happens to the data. Readers must examine the implementation to learn it transforms items, saves to database, and sends notifications. The function's side effects and responsibilities are hidden.

✅ Compliant:

function markItemsAsProcessedAndSave(data) {
    const processedItems = data.map(item => ({
        ...item,
        processed: true,
        timestamp: Date.now()
    }));
    db.save(processedItems);
    notifyService.send(processedItems);
    return processedItems;
}

Why this matters: The name explicitly states the function marks items as processed and saves them. Readers immediately understand the function's main purpose without reading implementation. The name hints at side effects (saving to database), setting appropriate expectations.

Conclusion

Function names should answer "what does this do?" without requiring code inspection. Include verbs that describe actions and nouns that describe targets. If a name becomes too long, the function likely does too much and should be split.

FAQs

Got Questions?

How long can function names be?

As long as necessary to be clear. calculateTotalPriceWithTaxAndShipping() is better than calc(). Modern IDEs have autocomplete, making longer names easy to type. Prioritize clarity over brevity. If a name exceeds 5-6 words, the function might have too many responsibilities.

Should function names include implementation details?

No, describe behavior and intent, not implementation. Name functions by what they accomplish, not how. sortUsersByName() is better than quickSortUsers(). Implementation details can change, but purpose remains stable. This keeps names relevant during refactoring.

What about helper functions or utility functions?

They still need clear names. Instead of helper() or util(), use specific names like formatCurrency() or validateEmail(). If you can't think of a specific name, the function's purpose might be unclear and needs better definition.

How do I name functions that do multiple things?

Split them. Functions that need names like validateAndTransformAndSave() violate single responsibility. Create separate functions: validate(), transform(), save(). If the operations must happen together, use an orchestrator function with a name describing the workflow: processUserRegistration().

Get secure for free

Secure your code, cloud, and runtime in one central system.
Find and fix vulnerabilities fast automatically.

No credit card required | Scan results in 32secs.