Long Names are Long
journal.stuffwithstuff.com
journal.stuffwithstuff.com
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.
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."
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.
class Array
def method_missing(m, *args, &block)
self.map {|x| x.send(m)}
end
end
users.first_nameThere 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.
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);
Or C++? http://www.boost.org/doc/libs/1_60_0/libs/serialization/doc/...
(I'd buy "Nobody uses Rust", but I can't resist a "maybe they should.")
In practice it was used far more for type.
In Systems Hungarian notation, the prefix encodes the actual data type of the variable.
Apps Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable's purpose is, or what it represents.
http://pandas.pydata.org/pandas-docs/stable/generated/pandas...
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.https://developer.apple.com/library/mac/documentation/CoreFo...
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.
> If you need a different variant of a method, create a method with a different name. ... type-based overloading is a bad idea, creating brittleness and ambiguity.
All of the cons with it are still there—it really is a hairy feature—but the pros are real too and might be worth doing. We still haven't figured out whether or not we will and, if so, what the exact semantics will be.
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?
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 [1] https://docs.python.org/3/library/functions.html#sorted
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.
This is true, but that's a code smell if the language supports interfaces (I don't know much about Dart, but it appears that Dart does support interfaces).
http://www.javafind.net/gate.jsp?q=/library/36/java6_full_ap...
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.
a) it's two words without a clear boundary – "idx_crpm" would be better (or idxCrpm if your project uses camelCase)
b) it's an unnecessary abbreviation – "index" is only two chars longer than "idx" and legibility trumps raw brevity
c) it's grammatically incorrect (without the "into" preposition that you used), hindering legibility – gramatically correct would be "crpm_index" (or crpmIndex)
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
[2] Much shorter version published in https://fg-sre.gi.de/fileadmin/gliederungen/fg-sre/wsre2016/...
Are you sure it's not an ID for an XCRPM? :)
// Bad:
String nameString;
DockableModelessWindow dockableModelessWindow;
See it? Don't dance around reserved words/used names in a space with caps. Arg.var foo = new Foo();
Totally disagree. I find differentiating between definition and instance via caps very intuitive and clear.
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.
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)
> 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"?
> Dart usage inside Google is cranking up
o_0
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.
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.
In any case, I wouldn't use an IDE that was so glitchy unless I was forced by necessity; I certainly wouldn't adopt a coping mechanism for it into other languages. Also, for a typical compiled language, deleting the declaration gives you all usages as long as you can (attempt to) build.
Are there any other reasons? Edit: Is this advice perhaps intended to be specific to a particular context?
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).
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.