3 ms·
Am I the only one who likes verbosity while reading code? When I stumble upon a codebase that looks like the following: def unzip(tuples): if tuples:
by NTDF9 9y ago
Am I the only one who likes verbosity while reading code?
When I stumble upon a codebase that looks like the following:
def unzip(tuples):
if tuples:
return [tuple(t[i] for t in tuples) for i, _ in enumerate(tuples[0])] <-------------------------WTF IS THIS LINE DOING????
else:
return []
It takes too much mental energy if the code is littered with the WTF-like statements above.
- danielvf 9y agoYeah, that's horribly ugly. But that is because list manipulation was hacked onto the Python language. The fault is more Python's than anything else. In other languages that same idea could be fairly clearly expressed.
- Doxin 9y ago> that is because list manipulation was hacked onto the Python language. No. It's because whoever wrote that doesn't understand what list comprehensions are for.
- dragonwriter 9y agoI don't think verbosity helps that; I means, to me that's clear, idiomatic Python code except that “for i, _ in enumerate(tuples[0])” would more clearly state it's intent as “for i in range(len(tuples[0]))” [0] which, unless I miscounted, is actually one character shorter. I'd also break the list (outer) comprehension into two lines for readability, but that doesn't really affect verbosity. [0] replace range with xrange in Python 2.x to avoid allocating an unnecessary intermediate list.
- NTDF9 9y agoYou're not getting the point. In a large codebase, I want to be able to skim code. I don't want to parse idiomatic code. I literally want to be able to read it like a novel and get down to the lines that I'm interested in. Granted Java is not the best. But Python-like languages (with no static typing and "tricky idiomatisms") make it so hard to parse a 10,000 lines100,000files.
- arvinsim 9y agoThe purpose of idioms is to have common vocabulary that everyone can understand easily when you read it. The downside is that you do have to recognize those idioms in the first place.
- stouset 9y agoThis, so so so so so much. Idioms and good abstractions are precisely how one mentally elides large volumes of code. When reading the following snippets: int doubles[10]; for(int i = 0; i < sizeof(doubles) / sizeof(int); I++) { doubles[i] = i * 2; } (0..9).map { |i| i * 2 } the latter is the one that I can understand the purpose of at a single glance. The details of iteration, properly counting the number of iterations, etc. are incidental to the thing I'm actually trying to accomplish. Worse, if you do start to filter out those things it's maddeningly easy to overlook obvious bugs because of a typo in the boilerplate. One of these requires me to think like a computer, and one of these hides the irrelevant details and lets me focus on the actual problem being solved. Multiply this one example by dozens of times per source file, and the incidental complexity becomes absolutely massive.
- dragonwriter 9y agoNo, I'm getting the point (I think: you do directly contradict yourself when you say you want to skim code and to read it like a novel, since skimming and reading word-for-word are very different, nearly polar opposite, approaches.) I'm saying that, but for the issues I noted, I find the code you gave easy both for skimming and direct reading, it's clear, concise, direct, and well-formatted with a familiar shape for the basic outline of what it does (again, but for the points raised in my earlier post), the first two points making it easy to read, the last two making it easy to skim.
- nickthemagicman 9y agoLol Ruby is the worst at this. It can cram so much information density into one line of code it can get mind boggling.