first-class paper redactors provide service here
essays
cheap,
Editing,
paper,
help,
professional,
education,
services,
internet,
editors,
open-source: del.icio.us tag/open-source
open-source
open_source
blogs
Software_Development
essays
rants
healthcare_systems
I was looking through some C code today, and stumbled across this lovely little gem:
1 2 3 4 5 |
tmp = "\"#"; while (*tmp) { FD_SET(*tmp, url_encode_map); tmp++; } |
Now, be honest. I don’t care how good you are at C, it takes you a few brain cycles to process that and figure out that it is just setting two bits in a bit field. It really should have been written like this:
1 2 |
FD_SET('"', url_encode_map); FD_SET('#', url_encode_map); |
This raises the question: why wasn’t it? I’ll tell you why:
Programmers have this burning desire to avoid code duplication. We’re taught, almost since the cradle, to abhor duplicated code and to avoid it all cost. Duplicating code is evil, it leads to unmaintainable code, and propogates bugs. Never, ever, do it!!!
Allow me to let you in on a little secret.
Calling the same function twice is NOT duplicating code. Not if the arguments change between calls.
Even calling the same function three times in a row is kosher. Four times, even. At some point, you might want to consider a loop, if the arguments can be determined functionally, but only do so when the list of similar function calls is harder to read and understand than the loop is. This is often when the loop takes fewer lines of code than the function calls do:
1 2 3 4 |
for (i = 127; i < 256; i++) { FD_SET(i, hdr_encode_map); FD_SET(i, url_encode_map); } |
There. Had to get that off my chest. Now, back to work.