Question on variable naming style (i.e. r versus random)
-
r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.
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 onerandom
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>Wrong is evil and must be defeated. - Jeff Ello
-
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
No thank you. :confused:
-
r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.
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 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 :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
Eddy Vluggen wrote:
you use what is readable.
I agree. I, and our team, use what is readable. :thumbsup:
-
Simply put, I can't be bothered to type the full word.
veni bibi saltavi
Nagy Vilmos wrote:
I can't be bothered to type the full word.
:-D Fair enough.
-
Meaningful names don't have to be long. We shun long names where I work.
well, "random" is long, longer than "rand" or "rnd" for example. So elementIterator or eleIter or elIt? In a couple of month I'd forget what "elIt" may be, but the complete form is way too long. It really depends and there is nothing exact...
Geek code v 3.12 { GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- r++>+++ y+++* Weapons extension: ma- k++ F+2 X } If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
-
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.
Wrong is evil and must be defeated. - Jeff Ello
-
r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.
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...
-
I use "meaningful" names for variables everywhere except for the "throw-away" variables in lambda expressions, for which I always use single char names.
You have just been Sharapova'd.
-
r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.
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
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
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 onerandom
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>Wrong is evil and must be defeated. - Jeff Ello
-
r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.
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
. ;PSlacker007 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, sosw
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. -
r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.
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
-
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
:thumbsup:
-
r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.
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 ) :-D
-
Nagy Vilmos wrote:
I can't be bothered to type the full word.
:-D Fair enough.
No, not fair enough. That's a cop-out. Meaningful variable names should be used everywhere. Programmers who claim that it's too much typing need to either take a typing lesson or hope that I never need to maintain their code.
Gus Gustafson
-
Sometimes even a variable name like random may not be enough. What does random stand for - Random temperature value? Random foreign exchange value? Random number? Random forum post number? So use
r
and consult the documentation anyway.Mobile Apps - Sound Meter | Color Analyzer | SMBC | Football Doodles
-
r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.
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.
-
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
-
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