Question

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?

Était-ce utile?

La 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.

Autres conseils

Storing the return value in a variable is the best solution imo. But you can do something else :

if (value_compared_to != LongFunctionName(first_argument,
                                          another_argument,
                                          finally_last_argument))

You have two choices:

1)

Accept that it looks this way

OR

2)

Evaluate the return value of the function-call in a separate Statement (pseudo-code)

retval = LongFunctioName(first_argument, second_argument, third_argument);
if(retval != desired_val) { ... }

Don't get us started on that one, I would do

if
(       LongFunctionName
        (       first_argument
        ,       another_argument
        ,       finally_last_argument
        ) != value_compared_to
)
{
        // stuff to be called
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top