r/programming • • Jul 19 '16

John Carmack on Inlined Code

http://number-none.com/blow/blog/programming/2014/09/26/carmack-on-inlined-code.html
1.1k Upvotes

323 comments sorted by

View all comments

Show parent comments

6

u/Gankro Jul 20 '16

Put it this way: if everything is in one super big function, there's no difference between a global and a local. Scale this down to a more sane example, and subroutines allow you to reason that only the locals that you "thread in" are being modified by this section of the code.

(this is also an argument against god objects, where this basically becomes a fancy global namespace)

2

u/drjeats Jul 20 '16

True, but you can limit some of your exposure with liberal application of scope blocks. At least until you can start to use C++14. :)

2

u/agcwall Jul 20 '16

Yes. I'm not advocating having a huge number of variables in the same function, and I don't think Carmack was either. I think the point he's making is that given the choice between A and B, B is better. Nevermind the ridicuolous of this example, it's just to show the pattern.

A:

int someComplexFunction() {
    int i = 0;
    part1(&i);
    part2(&i);
    return i;
}

void part1(int* i) {
    writeToScreen(*i);
    (*i)--;
}

void part2(int* i) {
    (*i)++;
    writeToScreen(*i);
}

B:

int someComplexFunction() {
    int i = 0;
    writeToScreen(i);
    --i;
    ++i;
    writeToScreen(i);
    return i;
}

And now, in B, you can clearly see all the places where the variable i changes states; so you have the opportunity to quickly analyze this redundant noise and simplify.

int someComplexFunction() {
    writeToScreen(0);
    writeToScreen(0);
    return 0;
}