Project

General

Profile

Review Request #12836

Minimize temporaries

Added by Richard Neswold over 4 years ago. Updated about 4 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Start date:
06/02/2016
Due date:
% Done:

0%

Estimated time:
Duration:

Description

This is a style suggestion instead of an actual problem. This comment can be closed after reading since the code is correct, as written.

Adding the temporary (retVal) generates extra code and, if the function gets larger than this one, can obfuscate what is going on. For instance, I'd write this function as:

uint32_t i2cStart(void)
{
    uint32_t timeout = 0;

    LPC_I2C->CONSET = I2CCONSET_STA;
    do {
        if (I2CMasterState == I2C_STARTED)
            return TRUE;
    } while (++timeout < MAX_TIMEOUT);
    return FALSE;
}

I know some people like returning from functions at only one point, so my approach is unappealing to them. But, to meet that requirement means you complicate your algorithm by adding temporaries. Also, if you have a nested loop, the break won't help you since it only breaks out of the inner-most loop. You'll resort to using a goto, which is generally frowned upon.

History

#1 Updated by Gregory Giese over 4 years ago

  • Status changed from Assigned to Closed

#2 Updated by Gregory Giese about 4 years ago

I would be one of those that doesn't care for multiple returns in a function but I see Rich's point. This processor has tons of memory so I didn't really code specifically to reduce memory usage. It certainly wouldn't be difficult to change if anyone sees the need.



Also available in: Atom PDF