Back to all reviewers

Extract and reuse

neondatabase/neon
Based on 3 comments
C

Create focused utility functions for repeated or complex operations instead of duplicating logic across the codebase. When implementing functionality, first check if similar code already exists that can be reused.

Code Style C

Reviewer Prompt

Create focused utility functions for repeated or complex operations instead of duplicating logic across the codebase. When implementing functionality, first check if similar code already exists that can be reused.

Benefits:

  • Improves code readability by abstracting implementation details
  • Reduces duplication, making maintenance easier
  • Makes testing isolated functionality simpler

Example:

// Before: Repeated parsing logic in multiple places
if (strncmp(wp->config->safekeeper_connstrings, "g#", 2) == 0)
{
    char *endptr;
    errno = 0;
    wp->safekeepers_generation = strtoul(wp->config->safekeeper_connstrings + 2, &endptr, 10);
    if (errno != 0)
    {
        wp_log(FATAL, "failed to parse generation number: %m");
    }
    // ...
}

// After: Extracted to a utility function
bool split_off_safekeepers_generation(const char* connstring, 
                                      uint32* generation, 
                                      const char** remaining)
{
    if (strncmp(connstring, "g#", 2) == 0)
    {
        char *endptr;
        errno = 0;
        *generation = strtoul(connstring + 2, &endptr, 10);
        if (errno != 0 || *endptr != ':')
            return false;
        
        *remaining = endptr + 1;
        return true;
    }
    *generation = INVALID_GENERATION;
    *remaining = connstring;
    return true;
}

// Usage
const char* remaining;
if (!split_off_safekeepers_generation(wp->config->safekeeper_connstrings, 
                                     &wp->safekeepers_generation, 
                                     &remaining))
{
    wp_log(FATAL, "failed to parse generation number");
}

When implementing new functionality, scan the codebase for similar operations that might already be implemented as utility functions. For complex operations like parsing, prefer creating dedicated functions that can be reused across the project.

3
Comments Analyzed
C
Primary Language
Code Style
Category

Source Discussions