Back to all reviewers

eliminate code duplication

ggml-org/llama.cpp
Based on 7 comments
C++

Avoid duplicating code blocks by extracting common functionality into reusable functions, using templates for type-generic operations, and moving shared utilities to common modules. Code duplication makes maintenance difficult, increases the likelihood of bugs, and violates the DRY (Don't Repeat Yourself) principle.

Code Style C++

Reviewer Prompt

Avoid duplicating code blocks by extracting common functionality into reusable functions, using templates for type-generic operations, and moving shared utilities to common modules. Code duplication makes maintenance difficult, increases the likelihood of bugs, and violates the DRY (Don’t Repeat Yourself) principle.

When you find similar code patterns:

  1. Extract common logic into helper functions
  2. Use templates for type-generic operations instead of copying code for different types
  3. Move shared utilities to common modules (like common.cpp) for reuse across files
  4. Refactor similar functions to share implementation details

Example of extracting a common string utility:

// Instead of duplicating string suffix logic:
static bool str_has_suffix(const std::string & str, const std::string & suffix) {
    return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), str.size(), suffix) == 0;
}

// Move to common.cpp as string_ends_with() for reuse

Example of using templates instead of duplication:

// Instead of separate functions for different types:
template<typename T>
static void ggml_compute_forward_repeat(const ggml_compute_params * params, ggml_tensor * dst) {
    // Generic implementation
}

This approach improves maintainability, reduces bugs, and makes the codebase more consistent and easier to understand.

7
Comments Analyzed
C++
Primary Language
Code Style
Category

Source Discussions