Constants and Reading and Writing

It’s common wisdom — and almost always correct — that it’s better to use constants than literals. As in return VSSidebarWidthMaximum instead of return 256.0f. I agree completely with that wisdom.

But there are two particular (and similar) cases where I disagree. Which is better?

response.send(200, someStuff);

…or…

response.send(statusCodes.OK, someStuff);

For me the first one is far better. By now the HTTP response codes are wired deep into my brain, and so the 200 literal is much easier to write and read than any constant.

Another example:

[request setHTTPMethod:@"POST"];

…or…

[request setHTTPMethod:VSHTTPMethodPost];

The first one wins. Yes, there is the possibility that I’ll mis-type it, and of course the compiler won’t tell me. But that trade-off is totally worth it, because now I can see instantly that it’s a POST method request. My brain doesn’t need to translate. (The odds of my mis-typing @"POST" and not noticing it are ridiculously small.)

10 Apr 2014

Archive