3 ms·
What I find to be the most annoying false cognate is attribute accesses which are secretly method calls. In [ x.y() for x in X], it's immediately obvious that
by yummyfajitas 14y ago
What I find to be the most annoying false cognate is attribute accesses which are secretly method calls.
In [ x.y() for x in X], it's immediately obvious that x.y() is doing some work. Not so obvious in [ x.y for x in X ].
This is a very practical concern - in Django code (both mine and other people's), I see lots of unnecessary SQL queries all over the place because of this.
Of course, Ruby, Scala, etc, are not immune to this criticism.
- jbrechtel 14y agoScala (and likely Ruby) have a very specific reason for doing this. http://en.wikipedia.org/wiki/Uniform_access_principle http://en.wikipedia.org/wiki/Uniform_access_principle It does put more onus on the API implementer to be careful about hiding non-trivial work, like you say.
- wahnfrieden 14y agoAlso terrible is expecting a property but accidentally evaluating a method as a boolean (which always evaluates true). Hard one to catch in debugging.
- masklinn 14y ago> What I find to be the most annoying false cognate is attribute accesses which are secretly method calls. The issue being that attributes can be accessed at all of course. > Of course, Ruby, Scala, etc, are not immune to this criticism. Well they are — or at least Ruby is — in that they don't allow attribute accesses from third-parties at all. Just consider that Python is the same (it is). Hell, in Python `.` is already a method call. In fact it's a whole sequence of method calls.
- dkarl 14y agoThe criticism does not apply to Scala because paren-less method calls are the norm, and because () was never intended to distinguish between field access and a method call. To assume x.y is a cheap operation in Scala would be to apply an assumption from other languages that is not valid in Scala. (Using parens for a zero-arg method in Scala communicates something else: by convention, x.y() is used if y has side effects, and x.y is used otherwise. I don't know how widely that convention is observed, though.) Python is different because the style guide explicitly discourages [1] computationally expensive accessor methods (as well as accessor methods with side effects) specifically so that programmers can treat accessor methods the same way they would treat a data field. Assuming that x.y is a field access in Python is not supposed to lead to problems, and if it does, it's the fault of the class implementer and not the user. [1] http://www.python.org/dev/peps/pep-0008/#designing-for-inheritance http://www.python.org/dev/peps/pep-0008/#designing-for-inher...
- chimeracoder 14y ago> Not so obvious in [ x.y for x in X ] Yes it is - that's always a method call: http://docs.python.org/reference/datamodel.html#invoking-descriptors http://docs.python.org/reference/datamodel.html#invoking-des... The problem is that you're unclear whether it's a mutating method or not, as well as whether it's an expensive operation. But that can be solved a number of ways - immutability and lazy evaluation would be one approach, though unfortunately neither Python nor Ruby enforces immutability, and both use absurdly eager evaluation. (Regarding that last point, try doing [x.y() for x in foo][0] and you'll see that y is called for every x!)