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

Chapter 4 Basics of Clean C++

Visual-Studio Compiler/Linker) and link-time optimization (gcc or LLVM/Clang), which allows the compiler and linker to perform global optimizations with information on all modules in the program. And with another Visual-Studio feature called profile-guided optimizations, the compiler optimizes a program using gathered data from profiling test runs of the .EXE or .DLL file.

Even if we do not want to use the optimization options of the compiler, what are we talking about when we consider a function call?

An Intel Core i7 2600K CPU can perform 128,300 million instructions per second (MIPS) at a clock speed of 3.4GHz. Ladies and gentleman, when we are talking about function calls, we are talking about a few nanoseconds! Light travels approximately 30cm in one nanosecond (0.000000001 sec). Compared to other operations on a computer, like memory access outside of the cache, or hard disk access, a function call is magnitudes faster.

Developers should rather spend their precious time on real performance issues, which usually have their roots in bad architecture and design. Only under very special circumstances do you have to worry about function call overhead.

Function Naming

In general, it can be said that the same naming rules that apply to variables and constants are also applicable to functions and methods. Function names should be clear, expressive, and self-explanatory. You should not have to read the body of a function to know what it does. Because functions define the behavior of a program, they typically have a verb in their name. Some special kinds of functions are used to provide information about a state. Their names often start with is ...or has....

Tip  The name of a function should start with a verb. Predicates, that is, statements about an object that can be true or false, should start with “is” or “has.”

Listing 4-21 shows some examples of expressive method names.

96

Chapter 4 Basics of Clean C++

Listing 4-21.  A Few Examples of Expressive and Self-Explanatory Names for Member Functions

void CustomerAccount::grantDiscount(DiscountValue discount); void Subject::attachObserver(const Observer& observer); void Subject::notifyAllObservers() const;

int Bottling::getTotalAmountOfFilledBottles() const; bool AutomaticDoor::isOpen() const;

bool CardReader::isEnabled() const;

bool DoubleLinkedList::hasMoreElements() const;

Use Intention-Revealing Names

Take a look at the following line of code, which is, of course, just a small excerpt from a larger program:

std::string head = html.substr(startOfHeader, lengthOfHeader);

This line of code looks good in principle. There is a C++ string (header <string>) named html, containing a piece of HTML (Hypertext Mark-Up Language) obviously. When this line is executed, a copy of a substring of html is retrieved and assigned to a new string named head. The substring is defined by two parameters: one that sets the starting index of the substring and another that defines the number of characters to include in the substring.

Okay, I’ve just explained in detail how the header from a piece of HTML is extracted. Listing 4-22 shows another version of the same code.

Listing 4-22.  After Introducing an Intention-Revealing Name the Code Is More Understandable

std::string ReportRenderer::extractHtmlHeader(const std::string& html) { return html.substr(startOfHeader, lengthOfHeader);

}

// ...

std::string head = extractHtmlHeader(html);

97