4 ms·
I've lately found myself doing more of this, rather than switch statements or if/else. It works when you find yourself in a function that does only that thing,
by cryptnoob 17y ago
I've lately found myself doing more of this, rather than switch statements or if/else. It works when you find yourself in a function that does only that thing, but often, you're in much more complex function, and you're not ready to return a value. Should you split this code into it's own function, allowing this type of structure, or use the switch, or if/else? I don't add functions for reasons as weak as this, so in those cases, I still have to use 'else'.
- pmichaud 17y agoI'll add functions at the drop of a hat. Even for one line of code: instead of commenting the line to explain that it Does Foobar, I'll just drop it into a function DoFoobar().
- DTrejo 17y agoI'd like to hear more about this sort of thing. Are there any other good resources/discussions on adding functions?
- pmichaud 17y agoYou should read Martin Fowler's Refactoring, it's the bible on that sort of thing.
- cryptnoob 17y agoI know I'm probably being too conservative from my years doing embedded and assembly programming, but I disagree with this. As you know, there is a cost to adding functions. They are not free from a code performance standpoint. Are they so close to being free that you don't need to worry? That depends on the processor and the application. I still do embedded and assembly programming, so I can't afford to drop this habit, because there, it does matter. So for web apps, I continue to use them liberally, but definitely not "at the drop of a hat".
- gjm11 17y agoWith a Sufficiently Smart Compiler (tm), there is no cost to adding functions because the compiler can inline them. FWIW, on the simple example I just tried "gcc -O3" is a sufficiently smart compiler.