4 ms·
Sometimes it's much clearer the number itself, specially when it's a mathematical formula. For instance, is const MILLIS_PER_SECOND = 1000; ... ... ... ...
by drowsspa 3y ago
Sometimes it's much clearer the number itself, specially when it's a mathematical formula. For instance, is
const MILLIS_PER_SECOND = 1000;
...
...
...
...
...
const durationMs = MILLIS_PER_SECOND * duration
really clearer than
const durationMs = 1000 * duration
?
- RHSeeger 3y agoAlong the same lines, I see this a lot long delay = (5 * 60 * 1000); // 5 minutes, in millseconds And it's perfectly clear to me. Now, I think the comment is really helpful there (indicating intent), but I don't think having separate constants for each of the numbers there is going to make the code better. As it is, it's very easy to read at a glance, know what it's intended to do, and determine if it's correct (should you be worried about that at the moment). Which is what's important there.
- gregmac 3y agoI do that as well, but typically name that variable delayMilliseconds so there's no confusion. sleep(delay) always looks ok, but sleep(delayHours) is probably going to catch your eye as suspicious.
- squeaky-clean 3y agoThe latter one leaves the possibility for this though const duration = 1000 * duration
- thebackup 3y agoCapital M is for Mega. I would use duration_s and duration_ms. const duration_ms = 1000 * duration_s And _us for microsec. const duration_us = 1000 * duration_ms But then the tool would probably reject my code for not following the naming conventions which ”disallows using underscores in variable names”. Guess what I wanted to say is that there are always exceptions to the rule and there should always be some way to turn off the automatic checker for certain sections of the code.