users.map {|u| "(" + u.first_name + ")" }
'u' would be a very bad variable name in a broader context. Here, we can easily see it's a user.
users.map {|u| "(" + u.first_name + ")" }
'u' would be a very bad variable name in a broader context. Here, we can easily see it's a user.
There are at least two reasons scope is important. First, the larger a name's scope, the more likely it is that short names will end up conflicting with other names at some point. Of course conversely, the smaller the scope is, the less likely it will conflict with something and the shorter you can make the name. And second, if the definition of something is a long ways away, then it becomes harder to find it to figure out the context. But in the example above, `u` has very small scope and its definition `users.map` is right there and makes it very easy to see what u stands for.
It's also important to realize that long names have a cost. Above the obvious small cost of typing time, it is also harder to disambiguate them visually.
class Array
def method_missing(m, *args, &block)
self.map {|x| x.send(m)}
end
end
users.first_nameUntil it isn't. How do you enforce this?
"Please use descriptive variables everywhere, except for when you are iterating over a list of users. Use "u" in that case. Thanks."
When the scope is limited to a single line lambda, "first letter of the thing it is" is plenty descriptive enough, especially given that it's a strong convention in ruby.
(In an example of the opposite extreme, I'm currently working int codebase where every non-function local/non-instance variable has to use its fully qualified path name. Extremely descriptive; extremely unreadable.)
Your own post provides good examples - you use "it" and "this" because, in context, they are clear and easier to write and read than a longer version.
The other direction is the same. If your team doesn't share a "common sense", then someone will end up writing value[axis_x][axis_y][axis_z], because single-letter names are prohibited.
There's absolutely nothing wrong with single-letter variable names with miniscule scopes. (Well, never use lower-case 'l' for anything, but the rest are fine.)
So? They are different names and have different types. Maybe this is a ruby specific thing. Are you suggesting since "u" looks more different from "users" than "user", it's easier to tell it's a different type? Usually in non statically typed languages, I thought you make the variable more descriptive to make up for lack of types not less descriptive. For example:
user_collection.each { |user| puts user.name }
If I've got a bunch of little lambdas, short loops (under 10 lines, perhaps), or list comprehensions, I'm not going to use longer names in them, because the context is clear and limited in scope.
If the variable's got to stick around longer (like throughout a function/method), it'll get a more descriptive name. As the scope grows, the variable loses the context that it came from, and it's more likely to need a longer name to make its meaning clear.
On a separate matter: you should read the HN guidelines.
Irrelevant. This function is painfully short.
> small functions can grow
It's a microlambda passed to a map function. 99% of these will never grow past a single expression. Hell, 95% of these will never even be modified.
> consistency is better
What, exactly, is this inconsistent with?
> you should read the HN guidelines
There's a difference between being uncivil and expressing frustration frankly.
Irrelevant. Other functions are longer. And needless human minification will undoubtedly creep in there too.
> What, exactly, is this inconsistent with?
Other functions.
> expressing frustration frankly.
Calling someone a wanker isn't exactly a pinnacle of expression.
No, it's not. But you shouldn't confuse your own identity with your coding style preferences, which were what the comment used "wankery" to describe.
> multiplied by the amount of loops, is somewhat inconsiderate.
And using the more detailed variable name is misleading. It implies a longer scope, to me.
"Don't use 'i' because it occurs often in keywords, making it hard to search for" is the most outlandish thing I've head in a good several weeks, even without the Scrabble reference.
If you need to specify the "u" in order to re-use it in one place only, then your language is syntactically challenged. Use a better language instead of caring about the names. A more flexible language would enable a predefined name like "it" or "_" instead, e.g:
users.map{"(" + it.first_name + ")"}
or even provide syntax to do common stuff, e.g. the asterix in: users*.{"(" + first_name + ")"}http://research.microsoft.com/apps/pubs/default.aspx?id=6974...
That's not always an option.
Also, I don't find the language OP linked syntactically challenged. C# has a similar syntax and it's much more readable than what you've provided:
people.Sort(p => p.FirstName);