
Long Names are Long - skybrian
http://journal.stuffwithstuff.com/2016/06/16/long-names-are-long/
======
michaelfeathers
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.

~~~
choward
"u" is a terrible variable name in that context also.

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

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

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

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

~~~
sullyj3
Alternatively, you could search for "int i" or "\<i\>", or "for (i".

------
krallja
Hungarian Notation is not, and was never, for representing the type of a
variable. It's about usage of that variable. "c" represents a change, "w"
means width, "x" means horizontal position, "ix" is an index into an array.
All of them are `int`s, but "cw" "cx" "ix" and "x" are all different kinds of
variables.

[http://www.joelonsoftware.com/articles/Wrong.html](http://www.joelonsoftware.com/articles/Wrong.html)

~~~
icebraining
Some would say those _should_ be their own types, even if simply aliases to
built-in ones.

~~~
krallja
I strongly agree, but nobody* uses languages that would make it a compilation
error to add "cw" and "ix" together.

~~~
geofft
Nobody uses C?
[https://github.com/torvalds/linux/blob/v4.3/arch/alpha/inclu...](https://github.com/torvalds/linux/blob/v4.3/arch/alpha/include/asm/page.h#L26-L33)

Or C++?
[http://www.boost.org/doc/libs/1_60_0/libs/serialization/doc/...](http://www.boost.org/doc/libs/1_60_0/libs/serialization/doc/strong_typedef.html)

(I'd buy "Nobody uses Rust", but I can't resist a "maybe they should.")

------
e0m
It's also really useful to add naming conventions that convey how expensive
something is. For example:

    
    
        myObject() // Returns immediately always
        getMyObject() // Might do some light calculation
        fetchMyObject() // Doh, better cache this!
    

Names can be extremely useful to convey expected usage as well.

~~~
Kristine1975
MacOS's Core Foundation API has naming conventions related to object
ownership: "create"/"copy" = you own the object returned by the function, you
have to release it after you're done with it. "get" = you don't own it.

[https://developer.apple.com/library/mac/documentation/CoreFo...](https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html)

------
yoklov
A lot of the time that type names end up in method parameters, it's due to a
lack of support of overloading in the language.

In particular, isn't Dart (the language described here) one such language?
It's been a while since I used it, and I never wrote much, but I at least
_thought_ that was the case.

~~~
_yosefk
I used to like how overloading lets you call it sort instead of
sortRectsByArea but over the years came to the opposite conclusion. Very short
and generic names are great for locals or for truly generic functions but
generally a function name shouldn't be too generic IMO, both because grep has
its uses regardless of IDE support and because it makes it less necessary to
look up definitions. mergeTableCells is better than merge even if you know
it's passed a list<TableCell> because merge could be merging the cells or the
cell contents or who knows what else, and because looking up the type of the
argument costs in itself (as it's not always a local declared right near the
call and even if it is, the type might not be spelled out in the declaration.)

Also, reusing names through overloading without providing actual polymorphism,
either run time or compile time, is IMO bad, as in I don't get what's there to
like. If one open function gets a file name and another gets a username and a
password, what's so great in that they have the same name?

~~~
xapata
Rather than ``sortRectsByArea`` I'd rather have a generic ``sort`` that
supports a Schwartzian transform, like Python's ``sorted(stuff,
key=transform)``.

Function or method names that postfix the mode onto the verb, like
"parse_from_file", are usually an indication that the design could be improved
by creating a consistent interface for the various modes. Sometimes it's
better to provide a handful of modes, but I always double-check when I see
myself writing names like that.

[0]
[https://en.wikipedia.org/wiki/Schwartzian_transform](https://en.wikipedia.org/wiki/Schwartzian_transform)
[1]
[https://docs.python.org/3/library/functions.html#sorted](https://docs.python.org/3/library/functions.html#sorted)

~~~
marcosdumay
That depends a lot on your actual architecture and domain.

Some times, you need parse_from_file, and nothing else. Other times it's worth
parametrizing everything and just assembly generic code at the top level.

------
jobigoud
com.sun.java.swing.plaf.nimbus.InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState

[http://www.javafind.net/gate.jsp?q=/library/36/java6_full_ap...](http://www.javafind.net/gate.jsp?q=/library/36/java6_full_apidocs/com/sun/java/swing/plaf/nimbus/InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState.html)

~~~
Analemma_
I wonder what the most interesting program you can fit in the number of bytes
it takes just to represent this class's name is.

------
falcolas
> I’m glad we now live in a futuristic utopia where keyboard farts like p,
> idxcrpm, and x3 are rare.

Perhaps it's the old "fart" in me, but these seem pretty clear.

p: a throwaway pointer, indicating that I'm probably in a C style for loop
over an array.

idxcrpm: and index into crpm. What is 'crpm'? Dunno - what is 'window'?

x3: The third, distinct, x value. I see such variables used frequently in
spatial algorithm implementations, where 'x' has distinct meaning.

It's more about knowing the idioms of the code you're in than some overarching
rule of law.

~~~
cessor
Code using single letter variable names and abbreviations makes people up to
15% slower at understanding code [1, 2].

Of course it is possible to determine - bottom up - what a variable is
supposed to do or that p is short for pointer, it still slows others down
because it requires them to perform mental mapping. This effect, of course,
depends on experience. If you write your vars like this all the time your
penalty won't be as big.

[1] Influence of identifier length and semantics on the comprehensibility of
source code, [https://github.com/cessor/bachelor-
thesis](https://github.com/cessor/bachelor-thesis)

[2] Much shorter version published in [https://fg-
sre.gi.de/fileadmin/gliederungen/fg-sre/wsre2016/...](https://fg-
sre.gi.de/fileadmin/gliederungen/fg-sre/wsre2016/WSRE-2016-Proceedings-2.pdf)

~~~
abecedarius
Upvoted, but I'll add that I'm surprised at how small you found the effect of
naming things a,b,c,... with no mnemonics at all. (I skimmed the shorter
paper.)

------
noonespecial
Glossed over this but it drives me nuts:

    
    
      // Bad:
      String nameString;
      DockableModelessWindow dockableModelessWindow;
    

See it? _Don 't_ dance around reserved words/used names in a space with
_caps_. Arg.

~~~
caseymarquis
As in the following?

var foo = new Foo();

Totally disagree. I find differentiating between definition and instance via
caps very intuitive and clear.

------
mjevans
I agree with much of this on principle, however one of the first examples
really strikes me as poorly conceived.

One such example is the variable 'name'; there are a lot of other languages
and contexts in which 'name' can be a reserved keyword. As a member on an
object it's probably OK, but in a larger syntax it isn't quite precise enough.
A better variable might be nameDisplay.

This presumes that you are referring to the way that a user prefers their name
to be displayed in native output; it does not attempt to make any distinction
to name components which may or may not be applicable.

------
wodenokoto
I struggle with naming things, especially in python where we dont have
explicit types,i do end up with the "strawberryList" kinds of names.

I work in NLP / machine learning and a lot of us are not first and foremost
programmers. It can be hard to come by good code in terms of coding style and
naming, so I'll ask here if anybody has good examples or even know of any
guides (even little things like a consensus on calling it "targets", "y",
"labels" or "gold" would be nice)

~~~
sullyj3
PEP8 is a good start, in case you're unaware of it. It contains a bunch of
style guidelines for python, including some naming conventions.

------
jstimpfle
> It needs to be clear: you need to know what the name refers to.

> It needs to be precise: you need to know what it does not refer to.

Is there such a generally accepted distinction between the meanings of "clear"
and "precise"?

------
caseymarquis
The fact that this is an issue at Google makes me think this is entirely the
result of the review process rewarding it. Could they really hire so many
highly qualified people with no sense of DRY?

------
ludamad
Long variable names have some fatigue associated, but when I'm hacking on
20,000+ line files, those long names really save similar things from being
blurred.

------
nailer
> June 2016

> Dart usage inside Google is cranking up

o_0

------
bsder
I am amazed at the number of people who use single letter variable names.

It's almost always better to use a double letter: uu, kk, ii, jj.

It's one of the few code patterns that most people will adopt without even
discussing it once they see it.

~~~
judemelancon
How is it better? (This is a literal, non-rhetorical question, not a joke
about even discussing it.)

~~~
bsder
Finding it in a sea of text.

Try searching for all the instances of variable "u" or "i" in a file.

Syntax-aware IDE's fail often enough that I have to rely on "search-and-
destroy" far more often than I would prefer.

~~~
yongjik
vim: /\<i\>/

Actually, if your cursor is on the variable "i", just pressing * or # will do
the trick. I'm pretty sure other editors have similar functions.

If you really want double-letter names, :%s/\\(\<[a-z]\>\\)/\1\1/g would
probably do the trick (well, except that every occurrence of "a" in a comment
will become "aa": how to avoid it is left as exercise).

~~~
squeaky-clean
Just tested it in some different editors with some javascript because I care
way too much about editors or something.

Sublime Text 3 captures the independent 'i' correctly. Though it also picks it
up in comments and strings.

VS Code incorrectly highlighted all instances of the character 'i', even if it
was part of another symbol. "Change All Occurrences" affected all instances of
the character 'i'.

Atom doesn't appear to have this feature without plugins (though someone
should correct me if I'm wrong).

IntelliJ and it's variants handled it perfectly, even understanding the actual
variable, rather than just the symbol. "Rename All" would ask if I wanted to
affect strings and comments, or just code (if there was an ' i ' in a string
or comment, otherwise it wouldn't prompt), and it would only rename the
variable within the correct scope. I tried it with both var and let.

I would try more, but I think I'm wasting too much time, haha. But moreover,
like others are saying, I can't think of a time where I've ever actually
wanted to do that. Renaming all instances of a single letter variable?
Generally the single letter is a very common convention I wouldn't change
(i,j,k, u, x,y, _), or is in a very small, uncomplicated function where I
wouldn't need a text editor's help to rename it because it only occurs once or
twice. Anything more advanced than that gets a more verbose name that any of
these editors could easily ctrl+F.

