5 ms·
It is excessive to claim that code cannot express the "why". You can almost always express the "why" with code if you really want to, though whether or not that
by mostlylurks 4y ago
It is excessive to claim that code cannot express the "why". You can almost always express the "why" with code if you really want to, though whether or not that's a good idea is another matter.
For example, you could express the same information that the comment in your example provides in the following manner with self-documenting code.
enum PerformancePrioritization {
FastWorstCase,
FastAverageCase
}
function sort(data: List<TypeOfData>, performancePrioritization: PerformancePrioritization) {
if (performancePrioritization == FastWorstCase) {
heapsort(data);
} else {
quicksort(data);
}
}
...
sort(data, FastWorstCase);
- mannykannot 4y agoI feel that this code is its own counter-example.
- PhineasRex 4y agoAn extra nine lines and extra runtime complexity just to avoid putting it in a comment.
- mostlylurks 4y agoI did explicitly mention that it might not be a good idea, though this is an exaggerated case since situations like these realistically constitute a very small portion of actual codebases. But a sibling comment [0] did provide an example that would give a much simpler approach to accomplishing the same thing in this particular instance, with zero extra lines and runtime complexity. [0]: https://news.ycombinator.com/item?id=31822657 https://news.ycombinator.com/item?id=31822657
- renox 4y agoAnd then you have an automated checker which tells you FastAverageCase isn't used..
- spc476 4y agoOkay, here's some code I wrote [1], with the existing comment: /*------------------------------------------------------------------------ ; For the RR OPT, the class field is actually the size of the UDP payload, ; and the TTL field are a bunch of flags. We have a separate field for ; the UDP payload size, so here we make the adjustment behind the scenes ; so you don't have to know this crap. ;-------------------------------------------------------------------------*/ if (answer->generic.type == RR_OPT) { answer->opt.class = answer->opt.udp_payload; answer->opt.ttl = ((data->rcode >> 4) & 0xFF) << 24 | (answer->opt.version & 0xFF) << 16 | (answer->opt.fdo ? 0x80 : 0x00) << 8 | htons(answer->opt.z & 0xFFFF) ; } I'd like to see how I could do away with the comment and make this more "self-documenting". [1] Code to encode/decode DNS packets: https://github.com/spc476/SPCDNS/blob/05aead581acf050edf610c6cb5cf86d33c686062/src/codec.c#L864 https://github.com/spc476/SPCDNS/blob/05aead581acf050edf610c...