Which block of code is "better"?

To promote good programming habits and improve the efficiency of my code (read: “My brother and I argue over some code”), I offer this question to experienced programmers:

Which block of code is "better"? For those who can’t be bothered by reading the code, is it worth putting the conditional code inside the for loop to reduce the amount of redundant code, than bring it to the outside and make 2 for-loop? Both parts of the code work, the question is efficiency and readability.

    - (NSInteger)eliminateGroup {
            NSMutableArray *blocksToKill = [[NSMutableArray arrayWithCapacity:rowCapacity*rowCapacity] retain];
            NSInteger numOfBlocks = (NSInteger)[self countChargeOfGroup:blocksToKill];
            Block *temp;
            NSInteger chargeTotal = 0;

//Start paying attention here

            if (numOfBlocks > 3) 
                for (NSUInteger i = 0; i < [blocksToKill count]; i++) {
                    temp = (Block *)[blocksToKill objectAtIndex:i];
                    chargeTotal += temp.charge;
                    [temp eliminate];
                    temp.beenCounted = NO;
                }
            }
            else {
                for (NSUInteger i = 0; i < [blocksToKill count]; i++) {
                    temp = (Block *)[blocksToKill objectAtIndex:i];
                    temp.beenCounted = NO;
                }
            }   
            [blocksToKill release];
            return chargeTotal;
        }

Or...

        - (NSInteger)eliminateGroup {
            NSMutableArray *blocksToKill = [[NSMutableArray arrayWithCapacity:rowCapacity*rowCapacity] retain];
            NSInteger numOfBlocks = (NSInteger)[self countChargeOfGroup:blocksToKill];
            Block *temp;
            NSInteger chargeTotal = 0;

//Start paying attention here

            for (NSUInteger i = 0; i < [blocksToKill count]; i++) {
                temp = (Block *)[blocksToKill objectAtIndex:i];
                if (numOfBlocks > 3) {
                    chargeTotal += temp.charge;
                    [temp eliminate];
                }
                temp.beenCounted = NO;
            }
            [blocksToKill release];
            return chargeTotal;
        }

, . , , for 1 15 , 64. , , , , . (: , .)

+3
8

, "". ? ? ? ? ? ? ? ? (, , .)

- , , , , , , , ! , .

, , . . - , , .

+8

, check numOfBlocks > 3 .

. .

,

bool increaseChargeTotal = (numOfBlocks > 3)

, , , .

( ), , , ; , " ".

+9

, , . - ; , , , , , ( , ARM iPhone ).

, , , : , . , - .

, , , . , .

+8

. , 2 , , , , . , .

, , , .

+5

, [blocksToKill ]/[blocksToKill release] / , , . , , , .

IMHO, , , .

, - :

    - (NSInteger)eliminateGroup {
        NSMutableArray *blocksToKill = [NSMutableArray arrayWithCapacity:rowCapacity*rowCapacity];
        NSInteger numOfBlocks = (NSInteger)[self countChargeOfGroup:blocksToKill];
        NSInteger chargeTotal = 0;

        BOOL calculateAndEliminateBlocks = (numOfBlocks > 3);
        for (Block* block in blocksToKill) {
            if (calculateAndEliminateBlocks) {
                chargeTotal += block.charge;
                [block eliminate];
            }
            block.beenCounted = NO;
        }
        return chargeTotal;
    }

( , ), , , , , , - , , , - /.

+5

.

, , . .

.

bool "" LTE - , , , , .

+4

"" - CPU; . , , . , .

,

(temp blocksToKill) {...}

, .

+3

(, , ) , , , .

, / , ( ). , , , , , , . , .

0

Source: https://habr.com/ru/post/1711467/


All Articles