If we talk about the style and support of the code, then using a variable (that is, creating a memory cell and carrying out this long initializing expression on a separate line) increases the readability and understanding of the code, and, in the case of an update of a specific code fragment, this variable can be reused (unless you can guarantee that this will not happen).
You can insert this long expression into a function in some cases, for example, when a function accepts an rvalue. Or you need to save on the creation of a variable, although this does not affect the whole picture much.
Here is what is said about using such a variable:
Each variable must be declared in the deepest block that surrounds all possible places of use of the variable.
You can also pay attention to this article , in particular, rules 61 and 64. I note that these rules apply to Boolean variables, but the ideology is the same:
bool isFinished = (elementNo < 0) || (elementNo > maxElement); bool isRepeatedEntry = elementNo == lastElement; if (isFinished || isRepeatedEntry) { ... } // NOT: if ((elementNo < 0) || (elementNo > maxElement)|| elementNo == lastElement) { ... }
Specifying boolean variables for expressions will result in the program self-documenting. The design will be easier to read, debug and maintain.
File* fileHandle = open(fileName, "w"); if (!fileHandle) { ... } // NOT: if (!(fileHandle = open(fileName, "w"))) { ... }
Executable expressions in conditions complicate readability. This is especially true for beginners in C / C ++.
Note : I don’t remember which author wrote about this (I suppose many), but there is a recommendation to replace global variables with functions like get() . Creating a getter function will be applied in the situation under consideration. You can wrap this long call into another callable object (function, lambda) with a short name so that it returns the desired value. This is applicable if the requested value may change while your code is running, and the requested value is updated (for example, in another thread). With the variable, you will have to update the value by re-invoking these spaghetti.