5 ms·
I would always make a local variable for things like this, to avoid magic numbers (especially if a function signature has several numeric arguments). Then you c
by joshvm 5y ago
I would always make a local variable for things like this, to avoid magic numbers (especially if a function signature has several numeric arguments). Then you can explicitly put the units in the variable name, e.g. duration_s = 300 and pass that. It's fine if the signature tells you the units, but it doesn't help anyone reading the code if you have poll_file_descriptors(read_descriptors, write_desciptors, 300).
- mulmen 5y agoBut how do you know the unit to use in the variable?
- Karellen 5y agoYou have to read the documentation. But you need to do that in order to be able to use the API properly anyway. The point the article is making is not about making it easier to write a line of code, but about making it easier to read a line of code. Given that most lines of code are read many more times than they are written, this is a good thing to focus on. "Programs must be written for people to read, and only incidentally for machines to execute." ― Abel and Sussman, Structure and Interpretation of Computer Programs, 1984
- josephg 5y agoBeing forced to read the documentation carefully every time you call the method is really bad UX though. And the approach in the article (let ms = 5; sleep(ms);) can be wrong. Maybe the method is actually taking microseconds not milliseconds and this causes a bug. I can also imagine linters (or other humans) fighting this style, since it’s longer, and something you have to actively do every time you call the method. Putting the units in the method name (when you can) fixes all of this. Sleep_milliseconds(5) is impossible to misuse or leave undocumented.
- deleted 5y ago[deleted]