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

Chapter 4 Basics of Clean C++

The three if statements raise another question: Is this function the right place to do such configurations? Definitely not! This does not belong over here.

The following lines (starting from 1814) are taking access to additional configuration data. It cannot be determined exactly, but it looks as if the last used file filter (LastFilterName) is loaded from a source that contains configuration data, either

a configuration file or the Windows registry. Especially confusing is that the already defined filter, which was set in the previous three if blocks (aDlg.SetCurFilter(...)), will always be overwritten at this place (see lines 1820-1823). So, what is the sense of setting this filter in the three if blocks before?

Shortly before the end, the reference parameter rName comes into play. Hold it ...

name of what, please?! It is probably the filename, yes, but why is it not named filename to exclude all possibilities of doubt? And why is the filename not the return value of this function? (The reason you should avoid so-called output arguments is a topic that is discussed later in this chapter.)

As if this were not bad enough, the function also contains commented-out code. This function consists of only about 50 lines, but it has many bad code smells. The

function is too long, has a high cyclomatic complexity, mixes different concerns, has many arguments, and contains dead code. The function name QueryFileName() is unspecific and can be misleading. Who is queried? A database? AskUserForFilename() would be much better, because it emphasizes the interaction with the user. Most of the code is hard to read and difficult to understand. What does nFileType & FT_BASIC_ LIBRARY mean?

But the essential point is that the task to be performed by this function (filename selection) justifies an own class, because the class BasicFrame, which is part of the application’s UI, is definitely not responsible for such things.

Enough of that. Let’s take a look at what has to be considered by a software crafter while designing good functions.

One Thing, No More!

A method or function should have a very precisely defined task represented by its significant name. In other words, a function or method should do exactly one logical thing.

93