3 ms·
Also, BTW, I hate the practice of using default arguments to avoid breaking existing code, because it makes the code reflect how it got there and not what it do
by nostrademons 13y ago
Also, BTW, I hate the practice of using default arguments to avoid breaking existing code, because it makes the code reflect how it got there and not what it does. You end up with a bunch of call sites, some that take n arguments and some that take n+1, and you have to look at the function definition to see what the n-arg call sites are doing. I would much rather use an automated refactoring tool to edit all call sites to take n+1 arguments with the default inline in the call, although this is a bit hard to do in Python.
In my experience, the best use for default arguments is when the API contract really does call for "default usage", but occasionally you need to pass an extra arg for special purposes. So if 95% of call sites only need n args but 1-2 of them need to vary a parameter or two of the algorithm, then defaults are appropriate. Passing the extra args by keyword usually ends up being much more readable in this case though.
- tome 13y agoYou can also just give the n+1 argument function a different name, and implement the old version in terms of it.