|
Good points.
Question, do you work by yourself, or in a team (a shop). Will there ever be a scenario where 8 months down the road someone else has to maintain your most excellent code?
|
|
|
|
|
I have worked in brownfields for over 15 years. I know that code should be readable. In that time I've seen unreadable code due to insanely long names, and I've seen unreadable code due to one-letter naming.
You do not choose either, you use what is readable.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: you use what is readable.
I agree. I, and our team, use what is readable.
|
|
|
|
|
Context rules in variable names.
"Hey You" is perfectly acceptable in some circumstances (in addition to song titles), while your full name is required in others (even "Slacker007" is allowed, in suspicious websites).
|
|
|
|
|
|
It kind off depends?
If you have a nested for loop you could use i and j eg as is often done, but I have seen (and have done ) situations that ended up in having i, j, k, l, ... or worse i, ii, iii etc...
I use simple one character variables or short abbreviations only when it remains clear. for single or double for loops that is i and j. If it becomes more complicated I use more descriptive naming.
For StreamWriter I usually use a short name if it is in a "write" method eg. If I would use it throughout the class or if I would use multipe instances, naming would be more descriptive.
shorter names tend to make it, for me at least, more easy to read and understand.
|
|
|
|
|
Depends on payment.
If (paid more, or also paid zero (as in open source)) {
Use meaningful names
} else {
Use cryptic names as a means of self-obfuscation
}
|
|
|
|
|
My first coding job was on SAP, and I grew to to like the abbreviated table and column names, where most table names were always four characters, and most column names five, e.g. BKPF-BELNR, or the Accounting Document Header table is BKPF and BELNR is Document Number.
This way it's easy to remember after a few months, and there is no uncertainty like in using full names for e.g. underscores vs dashes, misspellings, spaces, etc.
No object is so beautiful that, under certain conditions, it will not look ugly. - Oscar Wilde
|
|
|
|
|
Explains a lot about SAP.
|
|
|
|
|
No thank you.
|
|
|
|
|
The SAP abbreviations probably make sense in the original German
"book of accounts" -> "Buch der Konten"
|
|
|
|
|
Indeed they do, but while I worked on SAP, I found it very easy to pick up on the German terms used, and that made the naming pseudo-convention very easy to memorize and understand.
No object is so beautiful that, under certain conditions, it will not look ugly. - Oscar Wilde
|
|
|
|
|
In the very old times when storage was expensive, you had to shorten the variable names to the extreme levels where "user" was shortened to "usr".
Today it's just pointless.
There's no point to use overly short variable names outside the fact that you are used to it.
You should of course use a name as short as possible, what random stands for would in most cases be obvious from the context of the scope, and only in the few cases where it isn't, or where you need more than one random variable you would need to specify RandomTemp or RandomPostCode.
<edit>I should probably add that I find it ok to use abbreviations like sw for StreamWriter when they are standardized, but as soon as there is room for ambiguity I spell them out.</edit>
modified 18-Sep-15 8:00am.
|
|
|
|
|
In loops, "i" and "k" are my gold standard. I'm more verbose in any other place, but "random" might be just the first version. "Self-explanatory" names are subject to refactoring...
|
|
|
|
|
TRhank got me thinking the first time I had to code a pointer I used the name pointy (which was released), I tend to use i, j, k for loops and short names like Cars[] for array of Car types (don't ask!) I tend to sw for Stream Writer, sr for Stream Reader. The only place I have had to follow a strict coding style was when I was at uni...
|
|
|
|
|
I use single character names for iterators, even when nested provided the content of all loops is small enough to see easily:
for (int i = 0; ...
for (int j = 0; ...
... And I use single characters where they are appropriate: "XCoord" and "YCoord" don't add anything useful to "X" and "Y".
And I'll use two or three char names for "disposables" like a streamwriter where I'll create, write, close, and dispose in three or for lines, or for a file dialog:
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string filepath = ofd.FileName;
...
} The rest of the time, it's "sensible", "meaningful" names all the way!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Slacker007 wrote: My personal opinion is that you should use meaningful names. I concur. I've never heard any developer complain that someone's code was too clear.
/ravi
|
|
|
|
|
The complexity of the variable name should reflect the size of its scope. The larger the scope, the more complex the name. And limit the scope as much as practical.
Don't name a loop variable ThisIsTheVariableThatIsUsedToIndexIntoTheArrayOfUsersWeReceivedFromThePreviousWebMethodCall .
Slacker007 wrote: r versus random
In .net, you should probably have a private static field to hold an instance of a Random, that means the scope is the whole class, so a good meaningful name is appropriate -- I name mine randy .
Slacker007 wrote: sw versus streamWriter
I generally use StreamWriters in a small using statement, so sw is appropriate. If the code in the scope becomes too large, then it should be broken into methods -- with a more complex name for the StreamWriter parameter.
|
|
|
|
|
I'm in agreement with many of the replies. I use single letter iterators like i, j, & k for nested for-loops accessing a multi-dimensional array unless: a) scope is too large or b) it's helpful know what the dimensions represent.
I personally prefer "reasonably" sized var names (entirely subjective I know) that make it clear what it represents three pages down and 14 months later.
I do also follow the guideline, "when in Rome...". If I'm modifying someone else's code, I will adopt their style of brace positioning, commenting, and var naming conventions (e.g. if they use Hungarian notation).
The overarching principles for me are readability and clarity later on if I need to augment or fix it.
Cheers,
Mike Fidler
"I intend to live forever - so far, so good." Steven Wright
"I almost had a psychic girlfriend but she left me before we met." Also Steven Wright
|
|
|
|
|
|
For iterators I most always use single character names, but on occasion I have been know to use unrelatable, long, bizarre and ridiculous names just to confound, annoy, bemuse and jeer at those who may have to maintain or alter my code long after my departure (be it due to retirement, death, or lottery windfall )
|
|
|
|
|
Single letter names are ok if the scope of the variable fits in a few lines, so that you can see the definition along with all the uses. Iterators are (usually) an example of this rule. For people who have been programming long enough, i,j, and k naturally name the first, second, and third array dimensions. It's an ancient FORTRAN thing that lives on in the modern world, the way foo, bar, and blah from LISP 1.5 do).
Like all coding rules, this one has to be used with judgement. If using single-letter variable names is the alternative to 150-character long statements, which adds the most complexity.
|
|
|
|
|
BBC Report[^]
Anyone else suspect he just wanted to find out what it was like to have a bee sting his penis and was surprised as anyone when he got funding for it.
Quote: Another recipient, Michael Smith from Cornell University, ranked the pain of bee stings on different parts of the body, by orchestrating repeated stings to the four corners of his own anatomy. These ranged from the skull, the middle toe and the upper arm (at the less painful end) to the penis shaft, the upper lip and the nostril.
Wonderful, just wonderful.
Quote: Dr Rodrigo Vasquez, from the University of Chile, received the biology Ig Nobel for the crucial observation that if you raise a chicken with a weighted, artificial tail stuck to its backside, it will walk like a dinosaur.
I particularly like this one.
Quote: Diagnostic medicine - Diallah Karim (Stoke Mandeville Hospital, UK) and colleagues, for determining that acute appendicitis can be accurately diagnosed by the amount of pain evident when the patient is driven over speed bumps.
When I had appendicitis it was pushing a supermarket trolley that caused me incredible pain, led me to look up my symptoms (not usually a sensible thing to do) and take myself off to the hospital that night. If it hadn't been for the shopping I'd probably have sat on it for a while. Cannot remember crossing speed bumps now, but do remember that the position of sitting in the car (I drive with the seat leaning further back than most) was very comfortable. When I reported to Minor Injuries they decided that I needed to go to a proper hospital and offered to let me drive myself or go by ambulance. I decided to drive myself in case I got discharged (this was about 1 in the morning by now IIRC). Unfortunately what I was going to do about my car which would become illegally parked the following morning after I got admitted didn't cross my mind.
Some men are born mediocre, some men achieve mediocrity, and some men have mediocrity thrust upon them.
|
|
|
|
|
chriselst wrote: Anyone else suspect he just wanted to find out what it was like to have a bee sting his penis was down the pub when he came up with the research plan and was surprised as anyone horrified when he got funding for it.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
chriselst wrote: by orchestrating repeated stings to the four corners of his own anatomy. These ranged from the skull, the middle toe and the upper arm (at the less painful end) to the penis shaft, the upper lip and the nostril.
I don't know about the rest of you guys, but my reproductive gear isn't on one of the "four corners of my body"..
Incidentally, I've been stung inside the lower lip by a wasp (multiple times, by the same angry wasp) when it flew into my mouth by accident as I was walking down the street. Also inside my ear too when I was a kid. Neither incidents were pleasant
chriselst wrote: When I had appendicitis it was pushing a supermarket trolley that caused me incredible pain, led me to look up my symptoms (not usually a sensible thing to do) and take myself off to the hospital that night. If it hadn't been for the shopping I'd probably have sat on it for a while.
Another mutant like the guy with the penis on the corner of his body?
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|