News for hackers. Stuff that matters.
Re:Long Names are Long by michaelfeathers on Friday June 17, 2016 @10:05AM (#11922676)
I think that the most important thing to understanding about naming is how it interacts with scope. Class names tend to be longer because there is no outer scope (unless you have packages or namespaces). Likewise, single character names are fine in very localized contexts like this:

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.

Re:Long Names are Long by mightybyte on Friday June 17, 2016 @01:08PM (#11923860)
This. So much this.

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.

Re:Long Names are Long by GregBuchholz on Friday June 17, 2016 @12:42PM (#11923676)

    class Array 
      def method_missing(m, *args, &block)
        self.map {|x| x.send(m)}
      end
    end
    
    users.first_name
Re:Long Names are Long by coderzach on Friday June 17, 2016 @03:40PM (#11925078)
using `x` as a variable name is not the reason this code is terrible.
Re:Long Names are Long by choward on Friday June 17, 2016 @11:15AM (#11923108)
"u" is a terrible variable name in that context also.
Re:Long Names are Long by jng on Friday June 17, 2016 @12:00PM (#11923406)
I disagree. It is a great, clear, readable, succinct name, the same way that 'i' is a great name for an index in a loop, or i,j,k can be great names for nested indices. Einstein's multi-index notation was based in the same principles and works great in the contexts where it is used, the same that 'u' works great above.
Re:Long Names are Long by pklausler on Friday June 17, 2016 @12:12PM (#11923488)
Use 'j' and 'k', avoid 'i', especially for loop controls. It's so much easier to find their uses in the loop body with an editor, since they're high-valued Scrabble letters and they don't show up in C++ reserved words apart from "break".
Re:Long Names are Long by nikanj on Friday June 17, 2016 @12:50PM (#11923709)
Use an IDE instead of an editor and finding/hilighting usages is just one keypress away. I'm often surprised how many people assume IDE=editor, but with higher system requirements.
Re:Long Names are Long by kazinator on Friday June 17, 2016 @05:11PM (#11925584)
In Vim (editor, not IDE), if your cursor is on the i, then * will jump to the the next occurrence, # the previous one. In addition, they all get highlighted if ":set hls" (highlight search) is on, which is the default.

"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.

Re:Long Names are Long by sullyj3 on Sunday June 19, 2016 @07:27AM (#11932607)
Alternatively, you could search for "int i" or "\<i\>", or "for (i".
Re:Long Names are Long by kazinator on Friday June 17, 2016 @05:13PM (#11925595)
More importantly, don't use Notepad or Pico for writing code.
Re:Long Names are Long by choward on Friday June 17, 2016 @12:01PM (#11923419)
Why "u"? Why not "a"? Because "u" stands for "user"? Then make the variable "user" and don't add extra mental overhead for me when I read it.
Re:Long Names are Long by pklausler on Friday June 17, 2016 @12:10PM (#11923469)
And then you have the similar names "user" and "users" present in the same scope.

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.)

Re:Long Names are Long by SiVal on Friday June 17, 2016 @05:16PM (#11925610)
I recommend skipping both cases of the letter O as well. I'm surprised so many people use it, especially to mean "object". Really, code that uses `my_dict[O]` where the object is a hashmap key or `O1 + O2` demonstrating the wonders of operator overloading even show up in textbooks. No scope is small enough to justify O as a variable name.
Re:Long Names are Long by choward on Friday June 17, 2016 @12:19PM (#11923549)
> And then you have the similar names "user" and "users" present in the same scope.

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 }

Re:Long Names are Long by khedoros on Friday June 17, 2016 @01:23PM (#11924017)
Single letters for iteration values are a common convention in a number of languages. In the case of user/users, I'm likely to miss the distinction when scanning the code.

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.

Re:Long Names are Long by nailer on Friday June 17, 2016 @04:43PM (#11925428)
Iterating user for users is also a common idiom. Additionally making others having to look at the context to know what a u is, multiplied by the amount of loops, is somewhat inconsiderate.
Re:Long Names are Long by stouset on Friday June 17, 2016 @11:05PM (#11926842)
FFS, it's on the same line of code. The variable is live for a single expression. This is style wankery at its absolute worst.
Re:Long Names are Long by nailer on Saturday June 18, 2016 @07:38PM (#11931128)
Sure but most functions are longer, small functions can grow, and consistency is better.

On a separate matter: you should read the HN guidelines.

Re:Long Names are Long by stouset on Sunday June 19, 2016 @05:19AM (#11932357)
> most functions are longer

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.

Re:Long Names are Long by nailer on Sunday June 19, 2016 @09:09AM (#11932836)
> This function is painfully short.

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.

Re:Long Names are Long by pklausler on Monday June 20, 2016 @12:43PM (#11939192)
> 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.

Re:Long Names are Long by khedoros on Saturday June 18, 2016 @02:27AM (#11927344)
I know that it's common. I use it in cases where there are more than a few lines of code, where the reader would have to maintain context. Wider context equates to a more specific variable name.

> 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.

Re:Long Names are Long by dmansen on Friday June 17, 2016 @01:44PM (#11924208)
i'll take 'u' here any day. would never mistake it for anything else
Re:Long Names are Long by burkaman on Friday June 17, 2016 @11:20AM (#11923146)
Why?
Re:Long Names are Long by Larrikin on Friday June 17, 2016 @11:59AM (#11923398)
3 extra characters to make the variable user takes no time and makes the code clearer
Re:Long Names are Long by conistonwater on Friday June 17, 2016 @12:06PM (#11923450)
Does it actually make the code clearer, though? If u is used as a temporary local variable meaning a user from users throughout the code, I think it's quite clear. Consistency seems like the important thing here.
Re:Long Names are Long by choward on Friday June 17, 2016 @12:14PM (#11923501)
> If u is used as a temporary local variable meaning a user from users throughout the code

Until 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."

Re:Long Names are Long by akavi on Friday June 17, 2016 @12:51PM (#11923713)
"Please use variable names that are more descriptive the larger the scope of their use is."

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.)

Re:Long Names are Long by yongjik on Friday June 17, 2016 @02:51PM (#11924741)
In the same way you enforce anything else: common sense.

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.

Re:Long Names are Long by icebraining on Friday June 17, 2016 @12:53PM (#11923731)
All names depend on context. "user" may or may not be description depending on the surrounding context. Single-letter variables are the same.

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.

Re:Long Names are Long by falsedan on Friday June 17, 2016 @01:38PM (#11924160)
I agree, they should have used '_'.
Re:Long Names are Long by shmageggy on Friday June 17, 2016 @01:45PM (#11924214)
I disagree. In my experience, _ is used for assignments that will not be reused, basically throwaways. This is not one of those cases.
Re:Long Names are Long by vorg on Friday June 17, 2016 @04:34PM (#11925375)
> users.map {|u| "(" + u.first_name + ")" }

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 + ")"}
Re:Long Names are Long by kazinator on Friday June 17, 2016 @05:05PM (#11925548)
In a tight scope like this, `u` is the same thing as the classic "dummy variable", like the `i` in a for loop. "For a user u, do this". Dummy variables must never be given long names, because long names say, "this is not a dummy variable", which is a lie in that case.
Re:Long Names are Long by GregBuchholz on Friday June 17, 2016 @05:04PM (#11925541)
Re:Long Names are Long by robwilliams on Friday June 17, 2016 @05:08PM (#11925565)
>Use a better language instead of caring about the names.

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);