4 ms·
My "between" implementation would be like this: def between( a, low, high ): r = [] for x in a: if low < x < high:
by acqq 14y ago
My "between" implementation would be like this:
def between( a, low, high ):
r = []
for x in a:
if low < x < high:
r.append( x )
return r
or even
def between( a, low, high ):
return [ x for x in a if low < x < high ]
instead of their:
def between(numbers, low, high):
winners = []
for num in numbers:
if (low < num) and (num < high):
winners.append(num)
return winners
The more variable is local the shorter it should be. Also by using mathematical notation you display "functionality" instead of distracting readers with the terms you coined. "Winners"?
- gee_totes 14y agoFor me, the middle one is the most readable, but it's the hardest to figure out what data structure is being returned.
- deleted 14y ago[deleted]
- jaredmcateer 14y ago> The more variable is local the shorter it should be. I disagree, as someone with low experience with python, I found their example much more understandable than both of your examples.
- qu4z-2 14y agoIt's a tradeoff between readability for novices and readability for experts.
- jaredmcateer 14y agoI don't agree with that either, even in the languages I am well versed in not having to guess intentions or deduce variables is much easier. I think the tradeoff is really just keystrokes and laziness. Even if I'm wrong, how often is it an expert, that is not yourself, left to maintain your old code?
- gbog 14y agoYes, I thought so too times ago, but when your numbers become more complex things, and one has overridden gt(), and you need to debug in a hurry, because what you are trying to do is not even remotely related, then the verbose version if much easier.
- gizmo686 14y agoIf the code is small, than short variables are easier to debug, because they are easier to manipulate, have less visual clutter, and are short scoped enough that you do not get a benefit from using a longer name. Not to mention the fact that you do not have the descriptive name obscuring what the code is actually doing.
- gbog 14y ago> descriptive name obscuring what the code is actually doing. Sorry but descriptive names are clarifying what the code is doing, or are not descriptive.
- gizmo686 14y ago>Sorry but descriptive names are clarifying what the code is doing Generally speaking this is true. However, when you are debugging, this no longer is true. When variables say what they are supposed to be, then it is more difficult to see that they are something else.
- qu4z-2 14y agoDescriptive names clarify what the code is supposed to be doing. Sometimes there's a world of difference between that and what it's actually doing.
- calinet6 14y agoWell, that's the beauty: however you define "between", once you have it, you can just say "if between(...)" and it becomes very readable. If you want, you can subdivide this further and more clearly define the concepts used in the between function. But we don't generally choose to do that if the result is already a scant 5 lines. Point: you should also take into account good modular design, something you have control over.
- mattmanser 14y agoHonest question, why? What do you think you gained? Why use short variable names in this day and age given text editor auto-completion, refactoring tools, etc. The very plurality of `numbers` conveys so much more meaning than `a` in the method signature. Is that a Python idiom, to use `a` to mean array as a parameter? What happens if you don't use `a` until line 23 of the function, far from its original declaration? Being someone who likes reading random code in random languages, I find single or 2 letter variable names a horrible curse to the uninitiated of a problem domain. It means you have an extra step figuring out a program, what the abbreviations even mean in that particular domain. The only exception being when it's the standard way of using the syntax of a language (i in a for loop for example). Obviously these code examples are trivial, but when you start getting a slightly longer function, it starts becoming hard to remember what's what half-way down a function or where they even came from if they're just virtually meaningless single or dual letters.
- deleted 14y ago[deleted]
- btilly 14y agoWhat happens if you don't use `a` until line 23 of the function, far from its original declaration? Then you have horribly violated the previous poster's note that the more local a variable is, the shorter its name should be. I find single or 2 letter variable names a horrible curse to the uninitiated of a problem domain. It means you have an extra step figuring out a program, what the abbreviations even mean in that particular domain. It is a trade-off. If you master that domain, you'll be using that all the time, and you'll get the savings all the time. You need to amortize the cost of memorizing the notation over the expected usage of that notation. Programmers, by profession, have to use a lot of notations from a lot of fields, and therefore benefit from using notations that require a minimum of memorization. But that is not always the right trade-off. As I pointed out in https://news.ycombinator.com/item?id=5157539 https://news.ycombinator.com/item?id=5157539, The rule is not that you need long or short variables. You need meaningful ones. A short variable name is inherently ambiguous, which can lead to confusion and mistakes. Thinking through anything with a long-variable name abuses your working memory, limiting how complex your thoughts can be.