Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 146: Refactoring Techniques

Refactoring refers to the modification of existing code into an improved version. Although refactoring is often done while changing code to add features or fix bugs, the term particularly refers improving code without necessarily adding features or fixing bugs.

Section 146.1: Goto Cleanup

In C++ code bases which used to be C, one can find the pattern goto cleanup. As the goto command makes the workflow of a function harder to understand, this is often avoided. Often, it can be replaced by return statements, loops, functions. Though, with the goto cleanup one needs to get rid of cleanup logic.

short calculate(VectorStr **data) { short result = FALSE; VectorStr *vec = NULL;

if (!data)

goto cleanup; //< Could become return false

// ... Calculation which 'new's VectorStr

result = TRUE; cleanup:

delete [] vec; return result;

}

In C++ one could use RAII to fix this issue:

struct VectorRAII final { VectorStr *data{nullptr}; VectorRAII() = default; ~VectorRAII() {

delete [] data;

}

VectorRAII(const VectorRAII &) = delete;

};

short calculate(VectorStr **data) { VectorRAII vec{};

if (!data)

return FALSE; //< Could become return false

// ... Calculation which 'new's VectorStr and stores it in vec.data

return TRUE;

}

From this point on, one could continue refactoring the actual code. For example by replacing the VectorRAII by std::unique_ptr or std::vector.

GoalKicker.com – C++ Notes for Professionals

681