|
2012-10-18 A danger of default values for function arguments (in illustrated form)Due to recent events, I've been working on a program to measure our disk IO latencies. Since it only needs timing accuracy in the millisecond range, I've been writing it in Python (which is more than fast enough to not add distortions to the IO timings). In the process of developing this code, I made a classic absent-minded mistake that shows a danger of default arguments. The code needs to know the size of the range it will be doing IO on. In the beginning, it worked only on files and got the size from the size of the file, and the code looked something like:
Then I discovered that I needed to make the code work on raw disks
too. Getting the size of a raw disk is much more complicated than
getting the size of a file and anyways, they're huge and I didn't want
to do that much testing. I decided that clearly the thing to do was give
I then spent an embarrassing amount of time trying to figure out what was wrong with my code such that the IO offsets weren't being computed right (there were complicating factors, evidently including insufficient coffee). The problem (as you could probably see immediately) is that I'd
forgotten to update the code in (I was lucky that things broke in a clear, directly observable way.) This is not the first time I have revised function arguments this way. When I add a new argument to a function I always have a temptation to give it a default and make it optional; there's a little voice in the back of my head that says 'this is the right way to keep existing code working'. Very often this is clearly wrong for the same reason as it was here, namely that I'm going to immediately revise all of the callers to explicitly pass in this new 'optional' argument. If I'm always passing an argument, giving it a default value too is simply inviting argument count errors. I really should know better by now. (There are cases where default values for arguments are really useful, but this is not one of them.) (4 comments.)
python/DefaultArgumentDanger written at 01:33:29; Add Comment
|
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |