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

Chapter 4 Basics of Clean C++

You may ask now: But how do I know when a function does too many things? Here are some possible indications:

•\

The function is too long, that is, it contains too many lines of code

 

(see the following section about small functions).

•\

You try to find a meaningful and expressive name for the function

 

that exactly describes its purpose, but you cannot avoid using

 

conjunctions, such as and” or or,” to build the name. (See one of the

 

following sections on names.)

•\

The code in the body of a method or function has been grouped by

 

its developer using blank lines. These code groups represent the

 

individual process steps that make up the method. Often these groups

 

are also introduced with comments that are like headlines. In other

 

words, the developer already thought that the method would consist

 

of partial steps without introducing sub-methods for these steps.

•\

The cyclomatic complexity is high. That means it has deeply nested

 

control structures. The function contains many if, else, or switch-­

 

case statements.

•\

The function has many arguments (see the section about arguments

 

and return values later in this chapter), especially one or more flag

 

arguments of type bool.

The indicator mentioned in the first bullet point, that the method or function contains too many lines of code, leads us directly to the topic in the following section.

Let Them Be Small

A central question regarding functions is this: What is the maximum length of a function? (When I talk about functions in the following section, I also mean methods.)

There are many rules of thumb and heuristics for the length of a function. For example, some say that a function should fit on the screen vertically. Okay, at first glance that seems to be a not-so-bad rule. If a function fits on the screen, there is no need for the developer to scroll. On the other hand, should the height of my screen really determine the maximum size of a function? Screen heights are not all the same. So, I personally don’t think that it is a good rule.

94

Chapter 4 Basics of Clean C++

Note  Functions or methods should be pretty small. Ideally 4–5 lines, maximum 12–15 lines, but not more.

Panic! I can already hear the outcry of many developers: Lots of tiny functions? ARE YOU SERIOUS?!”

Yes, I am serious.

Large functions usually have a high complexity. Developers often cannot tell at a glance what such a function does. If a function is too large, it typically has too many responsibilities (see the previous section) and does not do only one thing. The larger a function is, the harder it is to understand and maintain. Such functions often contain many, mostly nested decisions (if, else, or switch) and loops. This is also known as high cyclomatic complexity.

Of course, as with any rule, there can be few justified exceptions. For instance, a function that contains a single large switch statement might be acceptable if it is

extremely clean and straightforward to read. You can have a 400-line switch statement in a function (sometimes required to handle different kinds of incoming data in telecommunication systems), and it is perfectly okay.

“But the Call Time Overhead!”

People now might raise the objection that many small functions reduce the execution speed of a program. They might argue that any function call is costly.

Let me explain why I think that these fears are unfounded in most cases.

Yes, there were times when C++ compilers were not very good at optimizing, and CPUs were comparatively slow. It was at a time when the myth was spread that C++ is generally slower than C. Such myths were propagated by individuals who did not know the language very well. And the times have changed.

Nowadays, modern C++ compilers are very good at optimizing. For instance, they can perform manifold local and global speed-up optimizations. They can reduce many C++ constructs, like loops or conditional statements, to functionally similar sequences of very efficient machine code. And they are now smart enough to inline functions automatically, if those functions can be basically inlined (... of course, sometimes it is not possible to do that).

And even the Linker can perform optimizations. For example, many modern C++ compilers nowadays offer a feature such as whole program optimization (Microsoft

95