[Python-ideas] Improving fn(arg=arg, name=name, wibble=wibble) code

Anders Hovmöller boxed at killingar.net
Fri Sep 14 02:58:23 EDT 2018


>> In that case, you should be able to link to a compelling example. If you go to the trouble of finding one, I'll take time to try to refactor it.
> 
> https://github.com/django/django/blob/master/django/db/models/sql/compiler.py#L707 <https://github.com/django/django/blob/master/django/db/models/sql/compiler.py#L707>
> 
> Is a pretty typical one.
> 
> That call is recursive, so it's unlikely that the author would shift the parameters around without testing the call and changing the argument positions appropriately.

Maybe. Still it would be better with keyword arguments. Testing is one thing, quickly finding problems is another. Keyword arguments fail fast and cleanly with signature changes, positional arguments only do when you add or remove parameters at the very end, all other changes are potentially very annoying to debug because you can get a long way past the problem call site before hitting a type or behavior error.

I'm not sure we even agree on this basic point though. Do you agree on that?


> The signature uses the parameter "default_order" and the call uses the argument "order". It seems that was a deliberate choice that wouldn't conform to the `f(a=a)` pattern.

Maybe. It's a bit weirdly named quite frankly. Do you set the default_order by passing that argument? Or do you set the order? The code below sounds like it's order, but the signature sounds like it's default order. It can't be both, can it? 

I don't know the specifics but it might just be that this code is hard to read precisely because it doesn't conform to the pattern, and if it *did* use the suggested feature the mismatch in names would stand out and I would believe that it was intentional and not a mistake at the call site, because it would look like:


results.extend(self.find_ordering_name(
	=item, 
	=opts, 
	=alias, 
	default_order=order,
	=already_seen,
))

now the odd ball out stands out instead of hiding. Different things should look different, and similar things should look similar (which would have been a good addition to the Zen of Python imo).

> The call is oddly split across lines 707 and 708, despite nearby lines being much longer. it could easily have been written as a single line.

Agreed. It's the ghost of PEP8, but that's a totally different morass! Let's keep to one extremely controversial topic at a time :)

/ Anders

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180914/625ada5a/attachment.html>


More information about the Python-ideas mailing list