3 ms·
time.sleep_seconds is appealing, but this approach doesn't work when a function takes additional arguments. for example, if your function is poll_file_descri
by rahimiali 5y ago
time.sleep_seconds is appealing, but this approach doesn't work when a function takes additional arguments. for example, if your function is
poll_file_descriptors(read_descriptors, write_desciptors, timeout_in_seconds)
it would be awkward to change the name of the function just because on of the arguments happens to refer to time.
- joshvm 5y agoI 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]
- agloeregrets 5y agoThis is where Objective C brings subtle magic with mid-function name arguments. Sure it's crazy verbose but insanely readable: printFile(file: File)withDelayInSeconds(delaySeconds: int)andPrintColor(PrintColor: UIColor)
- joshuamorton 5y agoThis is just the builder pattern but worse?
- josephg 5y agoIt’s actually just an obj-c method call. There’s no temporary builder object (or associated class) that needs to be implemented and instantiated like you’d need for a builder.