5 ms·
In general, programming language does not influence the quality of names, although Java code tends to exhibit better naming, perhaps due to the early popularity
by userbinator 10y ago
In general, programming language does not influence the quality of names, although Java code tends to exhibit better naming, perhaps due to the early popularity of Java coding style guidelines.
I completely disagree, having actually worked with a lot of Java once. The language in general is extremely verbose, and the names make it worse. What saddens me is that it seems a lot of the "better naming" discussed here is the complete opposite of what I'd consider good --- I mean, one of the guidelines listed there is "Identifiers should not consist of fewer than eight characters, with the exception of {short list containing very few dictionary words}"!? In the (not Java) code I work with, the majority of identifiers, i.e. locals, are below 8 characters.
IMHO identifiers should be short and memorable, preferably even having a unique and equally memorable pronounciation (I think "strlen", given as a "flawed" identifier in one of the tables, is an example of a good one, as are the others in the C library), but it seems a lot of "modern" code is going in the opposite direction. "Long and descriptive" identifiers may seem more "friendly" at first, but I feel it gets rather tiring after having to read plenty of code in that style --- especially if many of the identifiers share a common prefix, forcing you to read through the extra noise but having to compare to ensure they're actually the same.
Thus there appears to be two distinct style groups, one with the long and verbose "dictionary word" names and traditionally represented by Java and C#. This group is spreading, I think partly because of the "ease of entry" noted above. The other group uses the short, succint, abbreviated naming traditionally represented by C, Asm, sh, awk, and most of the existing Unix conventions. (As another example to contrast sh, PowerShell is a member of the former group.) This article seems to say the former is better than the latter. I am not convinced.
Some examples for concreteness:
Former style: https://github.com/dotnet/roslyn/blob/master/src/Compilers/CSharp/Portable/Compiler/Compiler.cs https://github.com/dotnet/roslyn/blob/master/src/Compilers/C...
Latter style: http://v6.cuzuco.com/v6.pdf http://v6.cuzuco.com/v6.pdf
Edit: Another example of the latter style, from something more vaguely related to the example in the former style: https://news.ycombinator.com/item?id=5748672 https://news.ycombinator.com/item?id=5748672
- manyxcxi 10y agoI use probably 70% Java in a given week, there was a time when I was 100% .NET, and there have been long stretches where the majority was overwhelmingly C, Node, Python, PHP. This is a long way of saying I've spent a lot of time floating around some very stylistically different languages so I don't really adhere to one dogma over another and I've grown to write code that mostly kind of/sort of feels the same between languages. Naming convention-wise at least. Java/.NET naming verbosity is great when I'm trying to figure out what the heck I'm receiving without tracking down the class definition. The thing is, you're only willing to be that verbose because you've got autocomplete in an IDE so peeking into that long object/class/parameter name is only a click away. The naming conventions like OncePerRequestHttpWebHandlerSecurityContextAwareInterceptorFactoryImpl are obsurd, but there's no reason you can't come to a more concise descriptive name. Frankly, if you're implementing the interface you can call it whatever you want. On the other hand I've seen some C and JS code that I could SWEAR was run through an obfuscator before I got to it, but no, the devs refused to use more than about three letters max. This is not helpful. Names like strlen, idx, propCnt are concise AND descriptive. I generally like to put units on things if they're some kind of measurement so I typically do stuff like intvlMS, distKM. There's nothing wrong with being descriptive, but you can really do a disservice to those (including yourself) coming after you when it becomes very hard to fit the mental model.
- milansm 10y agoEnglish is not my native language, and it took some time for me to actually parse those short variable names you provided as examples. Though, I still don't know what propCnt is supposed to mean. But even if I did, it takes some cognitive effort to parse shortened names. I think that's the case with native speakers as well, but it's more noticeable at non-natives because the time interval is longer. IMO those little parsing efforts add up and are tiresome if you have to do it all day long. Imagine if the books are written that way.
- kosievdmerwe 10y agoYou're not taking something into account with the shortened names: they are common and have consistent meaning. So after a few exposures it becomes trivial to read.
- greenhatman 10y agoI find it frustrating. Why not make it trivial to read the first time? I didn't get that intvl was interval until I read the reply. Why not just write interval or propertyCount? It doesn't make it much longer, and it's much easier to read. Long names only becomes a problem when you're concatenating more than three words for one variable. And that's really the problem, the number of words in a variable, not its character lenght.
- jstimpfle 10y agoYou can answer it yourself, but here goes: Because the trivial investment you have to make to learn these ubiquitous abbreviations might (no general judgement made) be outweighed by the benefit of better readability once you made that investment. "propertyCount" is 6 characters longer than "propCnt". In arithmetic expressions this can easily amount to 12 or 18 characters saved. I suggest you point out a specific example and show how you would do it in a better way.
- andreareina 10y agoI suspect it's supposed to be popCnt (no "r"), which is short for "population count", which is the number of set bits in a byte/short/int/long/what-have-you.
- BurningFrog 10y agoTo me, naming has two competing and somewhat contradictory goals. Names need to (1) descriptive and (2) short and memorable. Often people who try to write real "good" code overdo the descriptiveness and end up with technically perfectly precise names that are awful to read and remember. Learning how to find a short, yet descriptive name for things is real hard, and very important. It's the closest I come to being a poet in my work. The third goal I do these days is that names should be unique, if possible. When I search the project, it's best if I don't find several things with the same name.
- jstimpfle 10y agoIt depends heavily on the type of code. C code with complex bit manipulations is different than C# or Java business code. But as a general rule I like to think that if there are many variables whose purpose is so hard to remember that their names must be super descriptive, then the problem decomposition is probably lacking. I also think that bad decomposition is often a consequence of OO style (where concerns tend to be intermingled rather than separated because objects tend to be trashcans for everything "related" to ill-defined concepts). A good decomposition leads to source files with distinct concepts that lend themselves to short and memorable (but not overly descriptive) names within their limited scope. Example: The following code has a very clearly defined scope and implements a data transformation with 5 distinct concepts (cols, rows, objs, spec, database). Incidentally these show up in just about every function of the file. https://github.com/jstimpfle/python-wsl/blob/master/wsl/wslh/objects2rows.py https://github.com/jstimpfle/python-wsl/blob/master/wsl/wslh... (I haven't bothered to document these concepts yet. Assuming documentation I'm convinced 4-letter variable names are just right, and repeating the documentation at every usage location is wrong).
- continuational 10y ago"strlen" is a bad name partly because it's not immediately obvious what is meant, and partly because it contains the name if the type. One if the things I think Java actually does well is to provide good namespacing facilities for methods (eg. classes). That allows you to omit the type in method names. C: strlen(x) Java: x.length()
- jstimpfle 10y agoBut strlen is not a method. Why are you arguing? Besides, it's a very good name for a free-standing function.
- masklinn 10y agoThat it's a free-standing function is no excuse, generic functions have been a thing for decades, likewise namespaces.
- jstimpfle 10y agoIt's a C function. C doesn't have generic functions.
- masklinn 10y agoC's a shitty language, nothing new there, that doesn't make "strlen" any good it just means you get a historical baggage obsessed with nonsensically short names (hello creat(2), fcntl(2), fgetwln(3), wcsftime(3) or strpbrk(3)) and the additional constraints of a namespace-less language with no generics.
- pjtr 10y agoMaybe "good" is relative? Good for whom? For the active developers that work with the same identifiers frequently for a long time, the short names are convenient and sufficient. All abbreviations are obvious to them, and all contextual implications are clear. For new developers or developers that frequently switch between working with many different environments the short names can be a huge problem. "strlen" is not the biggest problem; you can look up what that means, as it's well documented. The problem is much worse when short names and abbreviations are introduced ad hoc without sufficient contextual clues. Actually bothering to document abbreviations helps. Does "proc" mean procedure, process, processor, ...? double chnopsnrg; double chnopsEnergy; double carbonHydrogenNitrogenOxygenPhosphorusSulfurEnergyInKiloJoulePerGram; double chnopsnrg; // C.H.N.O.P.S. (Carbon, Hydrogen, Nitrogen, Oxygen, Phosphorus, Sulfur) Energy in kJ/g I'll take the 4. over the others, but anything over the 1. Also scope matters. In local scope, names can (and should) be shorter. The context is clear. let fooBar = ... where fooBarPath = .., foorBarFile = File(fooBarPath) let fooBar = ... where path = ..., f = File(path) Public global names are a different matter. Also tooling matters. In old shells and editors without auto-completion long names of course quickly get annoying. With auto-completion and highlighting of identical words this is less problematic. On the other hand a good "go-to-definition" or "peek-documentation" feature makes short (documented) names less problematic. I give credit to PowerShell to at least attempt to balance the two aspects, as it supports both long and short names for most things. The tooling aspect seems to have been ignored though.
- userbinator 10y agoWith auto-completion and highlighting of identical words this is less problematic. Autocomplete only helps when writing, not reading; and it doesn't solve the problem of long names which have the same or similar prefices, obscuring the important differences. Long names still require reading. Here's a commmon pattern in Java and C# code: FooBarBazIntervalFactory fooBarBazIntervalFactory = new FooBarBazIntervalFactory(...); ... fooBarBazIntervalFactoryType = fooBarBazIntervalFactory.GetFooBarBazIntervalFactoryType(); fooBarBazIntervalFactorySize = fooBarBazIntervalFactory.GetFooBarBazIntervalFactoryType(); I prefer something more like FooBarBazIntervalFactory fbbif = new FooBarBazIntervalFactory(...); ... fbbifType = fbbif.type(); fbbifSize = fbbif.size();
- edblarney 10y agoI'm not sure if the examples support your case :) The 'former style' is instantly readable code. You can see roughly what is going on and make a mental picture of it. The 'later style' reads like Chinese. Everything requires a lot of context. I should point out a major difference: in Java etc. you're often dealing with abstractions that have allegories in the real world, i.e. 'isEditable'. Well, that means something can be edited or not, obviously. But in C, you might be dealing with so many low-level things like memory addresses, pointers, complex data structures etc.. Software has to be maintained, and I'd personally rather have a little dose of 'tedious' than to have to decrypt some byzantine names every time I glance at some code. I wonder what most people think about this?
- vorg 10y agoPerhaps combine the benefits of both the 'former style' and 'later style' into one -- use actual Chinese so it's still readable by most of the world's programmers (who live in China) but also utilizes the short context-required names (e.g. two characters '会编' instead of 10 'isEditable').