In complex cases where speed is less important than maintainability, I tend to agree.
In this case, a simple comment would suffice. And in fact nothing at all would be okay for any half-competent Python coder, as testing if lists are empty with if not is super-standard.
I never understood that argument. If you can be sure the type is a collection (and this you always should) not list is so moch easier to read and understood than the length check.
Compact does not mean easier to understand. They are different tests. The point is, do not make code less readable for speed unless speed matters. I am not going to argue which is more readable in any specific case. That is up to the developer.
I’d argue that if it’s strict explicitness you want, python is the wrong language. ifnotvar is a standard pattern in python. You would be making your code slower for no good reason.
You always want explicitness when programming. Not everyone reading your code will be deep into Python and relying on falsiness makes it harder to understand.
Yeah, I’m talking less deep than that. Plenty programming beginners will be reading Python code. And personally, I’m a fulltime software engineer, but just don’t do much Python, so while I had it in the back of my mind that Python does truthiness, I would have still thought that var must be a boolean, because it’s being negated. Obviously, a different variable name might’ve given me more of a clue, but it really doesn’t reduce mental complexity when I can’t be sure what’s actually in a variable.
But if those beginners want to stop being beginners, then they must learn the basics of the language. It makes no more sense to demand that everyone who programs in Python caters to beginners, than it makes to demand that everyone writing in English write at a 3rd grade reading level for the sake of English language learners
Yeah, my stance for both of those is the same: If the complexity aids you in communicating better, then use it. But if you’re using big words where small words would do, then you’re doing a disservice to your readers.
I think the explicitness of checking length is worth the performance cost. If ur writing code for speed ur not using python.
In complex cases where speed is less important than maintainability, I tend to agree.
In this case, a simple comment would suffice. And in fact nothing at all would be okay for any half-competent Python coder, as testing if lists are empty with
if not
is super-standard.I never understood that argument. If you can be sure the type is a collection (and this you always should)
not list
is so moch easier to read and understood than the length check.Compact does not mean easier to understand. They are different tests. The point is, do not make code less readable for speed unless speed matters. I am not going to argue which is more readable in any specific case. That is up to the developer.
How many elements in that list? Ah, it’s not list. It’s list, of course, we checked. But it’s not.
I’d argue that if it’s strict explicitness you want, python is the wrong language.
if not var
is a standard pattern in python. You would be making your code slower for no good reason.You always want explicitness when programming. Not everyone reading your code will be deep into Python and relying on falsiness makes it harder to understand.
Containers being “truthy” is quite basic Python and you will find this idiom used in nearly every Python code base in my experience
Yeah, I’m talking less deep than that. Plenty programming beginners will be reading Python code. And personally, I’m a fulltime software engineer, but just don’t do much Python, so while I had it in the back of my mind that Python does truthiness, I would have still thought that
var
must be a boolean, because it’s being negated. Obviously, a different variable name might’ve given me more of a clue, but it really doesn’t reduce mental complexity when I can’t be sure what’s actually in a variable.But if those beginners want to stop being beginners, then they must learn the basics of the language. It makes no more sense to demand that everyone who programs in Python caters to beginners, than it makes to demand that everyone writing in English write at a 3rd grade reading level for the sake of English language learners
Yeah, my stance for both of those is the same: If the complexity aids you in communicating better, then use it. But if you’re using big words where small words would do, then you’re doing a disservice to your readers.