Search code examples
c++readability

Readability of long function calls


I have been writing code that uses function calls that are really long and are often longer than 80 characters. Normally I would split those function calls like this:

LongFunctionName(first_argument,
                 another_argument,
                 finally_last_argument);

However, it looks weird when I try to put this into an if statement, mainly because it becomes not very clear what value it is comparing to:

if(LongFunctionName(first_argument,
                    another_argument,
                    finally_last_argument) != value_compared_to)
{
    // stuff to be called
}

How would you format this statement to be more readable and fit into 80 characters?


Solution

  • I would consider putting the function call on its own line:

    const auto value = LongFunctionName(first_argument,
                                  another_argument,
                                  finally_last_argument);
    if (value != value_compared_to) {
      // ...
    }
    

    You can even give the value variable a nice descriptive name that helps to understand the code.