by mostlylurks · 4y ago · 0 comments
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); /*------------------------------------------------------------------------
; 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/05aead581acf050edf610c...
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.