5 ms·
> There is also a place for functions that only get called from one location Do it inline. If you need it to look separate from the other context try an immedi
by cheepin 12y ago
> There is also a place for functions that only get called from one location
Do it inline. If you need it to look separate from the other context try an immediately invoked function or at worst some comment demarcation. No need to spray functionality across a file(s) when you can do it all inline, especially if it is only called from one place. I'll admit I didn't come up with this 100% myself, but having tried both ways, I really like inlining.
http://number-none.com/blow/john_carmack_on_inlined_code.html http://number-none.com/blow/john_carmack_on_inlined_code.htm...
- TeMPOraL 12y agoIf you're looking more at improving readability than performance, I like Lisp's approach to that, with flet and labels, that let you establish a bunch of functions limited to a scope (just like let does for variables) - you can split your function into few named subroutines and then define them inside that function. So you still get readability, and can keep the code that's only called once near the place it will be called from.
- wernercd 12y agoLooking at that, and considering A/B/C, I've seen myself using B most often - but I've not been afraid to use any of those. I'm not a fan of A, because I want to see the "bigger picture" first and then dive into details (minor 1/2/3/...) as needed. The reason for doing B vs C, in my experience with C#: * Logging. I can add, via PostSharp ( https://www.postsharp.net/features https://www.postsharp.net/features ) ( or similar extension/addons ) something as simple as `[LogDebug]` to a function and get logging goodness without having to mess with lines inside the code. PostSharp and Log4Net are my goto's. * Simplification - Partially included above (no need for a lot of logging code with the right tools). * Mental Gymnastics - its easier to see Step 1, 2, 3 when they are next to each other instead of pages apart. * Scope - only the parts that get sent in via parameters are worried about, and only a single return is normally needed. * Nesting - I've used minor functions to remove 3-4-5 levels of nesting before.
- yoklov 12y agoAside from logging (and games don't typically do a lot of logging), everything you mention is covered, and argued against, in the article.
- yoklov 12y agoThanks for this link. A lot of these things were things I've grown to believe from working in game development (and listening to programmers better than myself), but I never had seen it written out as competently and thoroughly as this.
- NhanH 12y agoYou know, unless my reading was wrong, the conclusion to be made is that: Knuth was right ... Inline seems to be an extreme poor man version of literate programming, isn't it?