3 ms·
>maintenance If used right, macros make things much more maintainable. If you have a hundred nearly-identical codeblocks, where only (say) a string constant i
by xamuel 11y ago
>maintenance
If used right, macros make things much more maintainable. If you have a hundred nearly-identical codeblocks, where only (say) a string constant is varying, and suddenly you need to change what those blocks do, bam, you've got a hundred blocks to change. If those blocks had been refactored with a macro, all you have to do is change the macro once. It also prevents the new guy from coming and doing a bandaid fix to line #72 making it slightly different from the 99 lines around it and making your life a living hell.
- dorfsmay 11y agoNot a lisper, so bear with me please. Couldn't you do this with a function?
- roryokane 11y agoYes, you could do that with a function. I think that was a bad example. I listed some situations where you need a macro in my comment that explains the “first rule of the macro club” you asked about: https://news.ycombinator.com/edit?id=9513988 https://news.ycombinator.com/edit?id=9513988
- xamuel 11y agoThe code block in question might be something that doesn't really deserve a whole function on its own, and could also involve local control-flow statements (return/continue/break/etc.) that can't really be outsourced to a function nicely. For example, you're parsing a line from a flat file and you want to assign a value to a certain variable based on the first word on the line. So you have a few dozen/a hundred lines of if ( key == "hitpoints" ) {player.hitpoints = value; return;} if ( key == "mana" ) {player.mana = value; return;} Those "return"s make it rather awkward to use a function. At best, said function would have to return a bool and you'd end up with something like if ( maybe_assign( key, "hitpoints", &player.mana ) ) return; which hardly gains you anything and in fact reduces readability a fair amount.