Live data from Hacker News

A Guide to Naming Variables

a-nickels-worth.blogspot.com

81–90 of 175 posts

Re: A Guide to Naming Variables

#81
My trouble with naming standards is the diversity of naming across systems/languages.

For your standard crud app you could have a database record with field ORD_NUM, service layer with property OrderNumber, and in javascript orderNumber.

If all I do is live in one part of the stack this may not bother me. But when I have to add a new field and propagate it through the stack I cringe at every mapping.

Is it too late to camel case everything?

Re: A Guide to Naming Variables

#82
post #4

Most of this, even if it's not my preference, I would never bother arguing with. But I have a question about a practice that is tremendously widespread. I have real trouble with single-letter variable names like "c", for any function more than, say, 2-3 lines. I scan the code and it increases my mental load because I have to remind myself what it means. Obviously a lot of people don't have problems with this, but I d…

Single letter variable names are valid only in two cases:

-loops (for i = 0; ...)

-scientific variables, in which case they should probably be constants anyway (c = speed of light in a vacuum)

Re: A Guide to Naming Variables

#83

Earlier quoted context omitted.

A convention I've always done for this is to just use a plural name. There are fewer errors based on mixing up the names than you'd think, and it's very readable. for fruit in fruits: assert fruit is not 'apple' print(fruit) assert 'banana' not in fruits etc. Especially in Python, it makes code flow very much like English. Edit: It also goes with the "don't put the type in the name" point, which I agree with in most…

I've always considered it a dangerous idiom in dynamic typed languages like Python to create identifiers that differ from each other by only a single character (fruit/fruits), point to completely different objects, and are used in close proximity to each other. It's just asking for a hard-to-detect typo that will blow up at runtime.

I would normally agree, but in the case of plurals like this (at least for myself) it's instantly obviously when one is being used incorrectly.

    for fruit in fruit:
      print(fruit)
My brain is screaming at me right now that the above doesn't make sense. Even with a click glance, it sticks out like a sore thumb. I'm not entirely sure why, but I'd guess it's because it mimics natural language more, so my eyes are more used to parsing that for mistakes?

Re: A Guide to Naming Variables

#84
post #56

A good guide, but I didn't like this part: Avoid Over-used Cliches In addition to not being Teutonic, the following variable names have been so horribly abused over the years that they should never be used, ever. val, value result, res, retval tmp, temp count str Cliches? If you are trying to communicate, these names are well known ways to do that. If there is something more specific to put in there, by all means, bu…

I have mixed feelings. For a return value, retval et al are completely appropriate. Same for count. Tmp/temp borrow meaning from whatever they are a temporary for. These are all fine. But "value" is horrible. You really don't know what it is? You might as well name it "variable". "Str" is in the same boat, unless it's short for "tmpstr".

If I'm iterating over a hash, it seems natural to use "k" and "v" or "key" and "value". Do other people use different names for these?

Re: A Guide to Naming Variables

#85

A good guide, but I didn't like this part: Avoid Over-used Cliches In addition to not being Teutonic, the following variable names have been so horribly abused over the years that they should never be used, ever. val, value result, res, retval tmp, temp count str Cliches? If you are trying to communicate, these names are well known ways to do that. If there is something more specific to put in there, by all means, bu…

I mostly agree. However, "tmp" and "temp" beg the question: "temp what?". But if you're using say node + express, and you're not using res and req - I'd have to question your sanity.

Re: A Guide to Naming Variables

#86
post #4

Most of this, even if it's not my preference, I would never bother arguing with. But I have a question about a practice that is tremendously widespread. I have real trouble with single-letter variable names like "c", for any function more than, say, 2-3 lines. I scan the code and it increases my mental load because I have to remind myself what it means. Obviously a lot of people don't have problems with this, but I d…

I sometimes use one-character names as a semantic hint that the variable is short-lived and relatively unimportant. I may not know at a glance what "c" means, but I know it's something that was assigned within the last few lines, and that won't live beyond the next few.

Re: A Guide to Naming Variables

#87
post #71

Earlier quoted context omitted.

How about just "status" for the return code var name? It should be readily apparent that the var is "my" (the routine's) status [code].

Ok, the 'function' part is not useful. And perhaps C was a bad example. Let's say it's Python now, and for some reason I'm still returning 0, -1 or -2. Then 'statusCode' would be a good name for the variable I guess. The code part indicates that it's a codified version of the status. The smarter way might be to just return a status object or something, in which case perhaps 'statusObject' is better? Not sure. I guess…

I'm OK with the "one word" parameter / return value names, in part, I guess, because I'm also a big proponent of a "pro forma" comment banner for every routine. For simple things, the comment can be a one-liner ("Return thing X"), but otherwise, explain each param and the return value.

Now, if the internal variables require a "data dictionary" level set of comments, that's another matter. Something is (usually) wrong in that case.

Re: A Guide to Naming Variables

#88
post #55

Earlier quoted context omitted.

I too abhor single-letter variable names. My personal convention: minimum identifier length==2. {i,j,k}->{ix,iy,iz}, c->ch.

I'm all for descriptive names but do you really not like iterator variable names like i,j,k or x,y,z? You prepend them i (for integer i suppose)?

I tend to call them index. If the loop is nested I grab functions to call inner bits. i and j especially can get crazy muddled coexisting (could you even pick two more similar letters? Capital I and l depending on the font maybe?).

In general, much prefer languages where I don't ever have to index. E.g. list comprehensions are much more expressive

Re: A Guide to Naming Variables

#89
post #80

Not trying to convince anybody of anything, but as a public service to expand your mind, I strongly urge anyone interested in these types of ideas to FORGET WHAT THEY THINK THEY KNOW OR HAVE HEARD and to REREAD Charles Simonyi's (he invented WYSIWYG) seminal piece of his PhD thesis which now is some 40 years old. Forget what you THINK you remember about these ideas--they've been butchered in popular understanding--re…

I was under the impression that Hungarian was largely not recommended. I find it's redundant in strong static systems, and unhelpful in duck-typed systems.

As I said, READ THE PIECE, forget about what you think you already know.

A discussion of what he actually said will be more interesting than a discussion of what you misunderstand.

But in response to the point you raised, the purpose of Simonyi's system is NOT to avoid compilation errors (what you call redundant). The naming convention encapsulates precisely what programmers need to keep in their heads while they code; humans are not compilers, and the type information involved is NOT THE COMPILER OR LANGUAGE TYPE, it is the abstract (human) type.

It is to (1,2,3 might not be all or the only reasons) (1) speed the coding task for the human (2) speed the maintenance task for the human and (3) enhance programmer understanding of the code to eliminate errors of human logic.

Re: A Guide to Naming Variables

#90

A good guide, but I didn't like this part: Avoid Over-used Cliches In addition to not being Teutonic, the following variable names have been so horribly abused over the years that they should never be used, ever. val, value result, res, retval tmp, temp count str Cliches? If you are trying to communicate, these names are well known ways to do that. If there is something more specific to put in there, by all means, bu…

From "The world's two worst variable names" [1]:

> I declare The World's Worst Variable Name to be:

    $data
> I declare The World's Second Worst Variable Name to be:

    $data2
I would lump in "value" with those two.

[1] http://archive.oreilly.com/pub/post/the_worlds_two_worst_var...

Post reply on HN