2 ms·
Well, yes, but I'd also like to think that code should raise the level of the programmers reading it...You should avoid gratuitous complexity I have a differen
by stcredzero 15y ago
Well, yes, but I'd also like to think that code should raise the level of the programmers reading it...You should avoid gratuitous complexity
I have a different set of policies than most programmers, which arises from my observation that our field's priorities are out of whack with the actual cost-benefit.
Our greatest costs involve understanding systems, so our first priority should typically be to produce readable and understandable code.
You shouldn't avoid language features just because some people don't know about them.
One should pick language features to optimize for readability, which is entirely contextual. If your shop has a culture of using ?: to the point where it's like a coding standard then you should keep on doing that.
So long as code can be read and understood, programmers will learn. Better yet, if the culture of a shop is that use of language features and other tools are motivated by contextual cost-benefit, then programmers will learn from this example. As it is, programmers generally are more interested in showing off, having fun, and writing things as easily as possible. It's less common to have a culture of prioritizing reading.
- SiVal 15y agoI agree strongly. It amazes me that in the ugly vs. beautiful category, the article uses this as the beautiful: return [i/2 for i in nums if not i % 2] "not i % 2"? That's beautiful? We do a division and ask whether the remainder is not true? What does it mean for a remainder of a division to be not true? Is sqrt(3) untrue? Is 17 not yellow? Is this really the clearest way to say even number? Yes, after years of working in C, I'm well aware of how C does bools, but that's because C's values are small and fast, not beautiful and clear. In C, there's barely any abstraction to leak--you just manipulate bits and don't worry about mixing your metaphors. But Python has different priorities, which is why I prefer it to C when my users (and I) won't be hurt by the performance difference. If we're showing off clarity instead of cleverness, wouldn't this be a better way to demonstrate it: return [i/2 for i in nums if i%2 == 0] And I prefer the concept of "clarity" to "beauty" when it comes to code. Beauty is, well, whatever in the eyes of the beholder. Clarity, for me, is the question of how fast code can be read and understood correctly by a given programmer who is familiar (not more) with the language, not necessarily familiar with other languages, and unfamiliar with what the code does. The faster such a person can skim the code and understand it correctly, the easier it will be to modify and keep free of bugs. If we're so smart, why don't we show it by using our brains to write code that is quicker to read and understand correctly than code written by lesser lights?
- tripzilch 15y agoI agree, I wondered about that same snippet. And I tested with the `timeit` module, there's not really much of a performance difference either (although the version with `not` is slightly faster, it's just by less than a percent or so).
- jrockway 15y agoI have a different set of policies than most programmers, which arises from my observation that our field's priorities are out of whack with the actual cost-benefit. What metrics do you use to determine if code is readable or not? What metrics do you use to determine "actual cost-benefit"?
- stcredzero 15y agoWhat metrics do you use to determine "actual cost-benefit"? Well, if one were to take into account hard metrics for every 4 line snippet of code, then I'm not sure enough would get done fast enough. In the context of everyday programming and of the 3 examples I quoted, it's enough to ask yourself questions like: what would a newbie understand? What would an average programmer recognize immediately? If there's a quick obvious answer to either of those questions, and no onerous externalities involved, then that's what you write. (The code being 10X longer or too slow or too likely to contain bugs would be an onerous externality.) This isn't to say that metrics aren't useful here. The question is how to apply them at a low enough cost. In a large company, perhaps one could A/B test variations on coding standards. In a diverse group of small programming shops with internally consistent coding standards or styles, one might gather metrics on bugs per line of code versus features of coding styles. Something to think about. It's not as if metrics are commonly used to make these decisions now. Either edicts come from on high, or the local alpha-coder declarers what's best in her/his experience.