4 ms·
I disagree that the C# example is more readable. Let me just compare your own human-language description of the task with the Python code: the total leng
by BerislavLopac 6y ago
I disagree that the C# example is more readable. Let me just compare your own human-language description of the task with the Python code:
the total length of all strings in a list that start with the letter 'a'
sum( len(s) for s in strings if s.startswith("a") )
Of course that does not necessarily apply in all possible cases, but in this particular example it is practically a 1:1 match. The C# example is a better match with the way one would describe an algorithm:
From a list of strings take all strings that start with the letter 'a', calculate their lengths and add them up
strings .Where(s => s.StartsWith('a')) .Select(s => s.Length) .Sum()
I personally find the Python's syntax much closer to natural language in most cases, which is why the language has been called "executable pseudo-code".
- majewsky 6y ago> I disagree that the C# example is more readable. You then go on to show that the Python example is closer to the English language. That doesn't prove better readability.
- BerislavLopac 6y agoFair point -- I do generally consider "readability" to be quite a subjective concept, with different people having different ideas of what is more or less readable. For me personally, code that reads like natural language is generally more readable; however a) I am aware that other people have different preferences, and b) in some cases I also prefer a more "technical" approach.
- majewsky 6y agoI might be more sensitive to this topic because I'm currently learning Japanese, which has the interesting quirk of doing several grammatical constructions backwards compared to most European languages (e.g. verbs always come at the end of the sentence). If "readable" had a correlation with "similar to English", Japanese would be utterly indecipherable.