9 ms·
Long Names are Long
- yoklov 10y agoA 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.
- _1 10y agoYou are correct. The feature request for adding type-based overloaded function was denied: https://github.com/dart-lang/sdk/issues/49#issuecomment-108301493 https://github.com/dart-lang/sdk/issues/49#issuecomment-1083... > 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.
- afsina 10y agoHow about this? They might have changed their minds.. https://github.com/dart-lang/sdk/issues/26488 https://github.com/dart-lang/sdk/issues/26488
- munificent 10y agoYes, overloading is back on the table. 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.
- _yosefk 10y agoI 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 10y agoRather 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 10y agoThat 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.
- kerkeslager 10y ago> 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. 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).
- falcolas 10y ago> 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.
- gilgoomesh 10y agoEven if "crpm" is a contextually known entity, I'd consider "idxcrpm" a terrible identifier. 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)
- cessor 10y agoCode 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/WSRE-2016-Proceedings-2.pdf https://fg-sre.gi.de/fileadmin/gliederungen/fg-sre/wsre2016/...
- abecedarius 10y agoUpvoted, 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.)
- michaelfeathers 10y agoI 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 10y ago"u" is a terrible variable name in that context also.
- burkaman 10y agoWhy?
- Larrikin 10y ago3 extra characters to make the variable user takes no time and makes the code clearer
- conistonwater 10y agoDoes 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.
- choward 10y ago> 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."
- 10y ago
- krallja 10y agoHungarian 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 10y agoSome would say those should be their own types, even if simply aliases to built-in ones.
- krallja 10y agoI strongly agree, but nobody* uses languages that would make it a compilation error to add "cw" and "ix" together.
- wvenable 10y agoTrue. But for documentation purposes it makes more sense than tacking on prefixes to names.
- geofft 10y agoNobody uses C? https://github.com/torvalds/linux/blob/v4.3/arch/alpha/include/asm/page.h#L26-L33 https://github.com/torvalds/linux/blob/v4.3/arch/alpha/inclu... Or C++? http://www.boost.org/doc/libs/1_60_0/libs/serialization/doc/strong_typedef.html 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.")
- michaelfeathers 10y agode jure != de facto In practice it was used far more for type.
- wvenable 10y agoStill a terrible idea. I agree with the poster below that these should just be their own types.
- e0m 10y agoIt'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.
- icebraining 10y ago+1. Another one I use is "generateX()", usually for heavy calculations like image processing or PDF rendering.
- Kristine1975 10y agoMacOS'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/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html https://developer.apple.com/library/mac/documentation/CoreFo...
- jobigoud 10y agocom.sun.java.swing.plaf.nimbus.InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState http://www.javafind.net/gate.jsp?q=/library/36/java6_full_apidocs/com/sun/java/swing/plaf/nimbus/InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState.html http://www.javafind.net/gate.jsp?q=/library/36/java6_full_ap...
- Analemma_ 10y agoI wonder what the most interesting program you can fit in the number of bytes it takes just to represent this class's name is.
- bsder 10y agoI 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 10y agoHow is it better? (This is a literal, non-rhetorical question, not a joke about even discussing it.)
- bsder 10y agoFinding 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.
- judemelancon 10y agoWhy would I ever want to find every reference to a loop or lambda variable with a given name in a file? Such variables repeat frequently with distinct meanings, and each has a narrow scope. 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?
- yongjik 10y agovim: /\<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).
- noonespecial 10y agoGlossed 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 10y agoAs in the following? var foo = new Foo(); Totally disagree. I find differentiating between definition and instance via caps very intuitive and clear.
- mjevans 10y agoI 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.
- ludamad 10y agoLong 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.
- jstimpfle 10y ago> 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"?
- nailer 10y ago> June 2016 > Dart usage inside Google is cranking up o_0
- deleted 10y ago[deleted]
- caseymarquis 10y agoThe 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?
- wodenokoto 10y agoI 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 10y agoPEP8 is a good start, in case you're unaware of it. It contains a bunch of style guidelines for python, including some naming conventions.