3 ms·
Some functions are complicated, some are simple, some are very simple. C makes you write out more of the very simple ones by hand. For example, imagine a very
by msla 7y ago
Some functions are complicated, some are simple, some are very simple. C makes you write out more of the very simple ones by hand.
For example, imagine a very simple bytecode interpreter: In C, you need a function to dispatch all of your bytecodes to the correct implementation functions with the right arguments. In Common Lisp, this is a prime target for a macro to turn a configuration file into actual code using fill-in-the-blanks boilerplate iterated a few times. In C, you have to write the switch statement by hand, which leads to writing a function as long as the number of individual bytecodes your interpreter knows about. Exceeding sixty lines is entirely possible assuming formatting directives are adhered to such that you can't bunch statements up on a single line. However, that switchyard function is the simplest way to write that code; breaking it up would only make things needlessly complicated and harder to check.
- scatters 7y agoWell - in C, you can use macros as well as in Lisp. Classically, you'd use X-Macros, requiring a separate file to hold the list of bytecodes; in the modern style you can use Boost.Preprocessor to operate on preprocessor data structures.
- saghm 7y agoYou can use Boost in C? Is this something that's commonly done? I thought Boost was for C++
- AlotOfReading 7y agoBoost.Preprocessor is the only part usable from C, though I wouldn't generally recommend using it.
- Gibbon1 7y agoThat's my opinion as well. A function should have a one job and do that one job. Not two jobs, not five jobs and usually worse 3/16ths a job because some line limit rule. The line limit rule sounds to me like a CS professors homework assignment instruction that escaped into the workplace.
- AnimalMuppet 7y agoIn C, you could do that with an array of pointers to functions, and just index into the array. (The functions would need to know their own number of arguments, though, and consume them from the byte stream. But I think I prefer that to the dispatcher knowing the number of arguments that each function consumes.)
- msla 7y agoThat would defeat some static analysis, so I can see it being disallowed by a careful style guide.
- Mikhail_Edoshin 7y agoAnd exactly such a function exists in CPython, the main implementation of Python :) It's about 2000 lines of code, as far as I remember.