Click to See Complete Forum and Search --> : How do you name stuff?


TheLinuxDuck
07-26-2001, 03:15 PM
What kind of naming scheme do you give for functions/subs/methods and variables/fields?

I generally use stuff like 'j' or 'k' for one time temp variables for looping. If it's a temp variable that will be holding some kind of data for a short duration, I call it 'temp'.

Anything that will be temp storage for a read from a file or whatnot, I generally use 'buffer'.

I am generally against using underscores in names. I think it adds another unecessary character into a name making it even longer. That is one complaint I have with java is that names of stuff are long. I have had certain lines that are unbreakable that spand 100+ characters.. to me, that's no good. (^=

I used to name stuff like 'copyofb' and 'nextregister' or 'firstparam'... but, as I began getting into java, I started really liking using the caps for each word (minus the first) for stuff.. it makes them more readable (though I wouldn't have admitted that before I started using it)

I don't like global variables. Perl kinda makes you use some globals, unfortunately. There are ways around that, but sometimes it's just more work than it's worth.

In C/C++, I generally try to use 'ptr' on the ends of vars that are pointers.

On methods/functions/subs, I tend to do the all-words-first-letter-in-caps-except-first-word thing. (and yes, that is the technical term for it.. ::smile: :)

I try to use common sense when naming stuff, but sometimes I get real lazy and don't.

Anybody wanna share their style?

EyesWideOpen
07-26-2001, 03:54 PM
Pretty much what you said with the exception of using underscores. I don't mind using underscores to delimit words in variable or function names. Maybe if I get into Java I might change my tune and do the all-words-first-letter-in-caps-except-first-word thing.

I usually use idx or i for incrementing variables in loops and whatnot.

I try to make my function names read as English as much as possible where applicable so when I call the function it will be very easy to tell what it's doing (get_element($some_element), throw_error($some_error), etc).

'sabout it. ;)

[ 26 July 2001: Message edited by: EyesWideOpen ]

Strike
07-26-2001, 04:00 PM
Depends very much on the app. If I'm building a GUI with like GTK, I always try and end it with the type of element it is and I try to preface it with a two or three letter abbreviation of the container it is in and an underscore. Like a button that says "Done" that is in a window named "main_window" will be - mw_done_button.

binaryDigit
07-26-2001, 04:39 PM
i'm still developing my naming style, but so far i've come up with these standards:

loop variable index - index, index1, index2, etc...
temporary holders - tmp, tmp1, tmp2, etc...
anything that's a pointer - p_<variable_name>
struct - s_<struct_name>
class (yeah i'm learning c++ again) - c_<class_name>
oh yeah : pointer to a pointer - pp_<variable_name>

other than that i just try to make the name as descriptive as possible, it seems to make
comments less necessary if you have a good variable name.

unlike TLD i like to use the underscores. 8^)

TheLinuxDuck
07-26-2001, 04:45 PM
It seems that I am in a minority when it comes to underscores...

(^=


but, I must confess that I've never actively used them, and there is a chance that I would not mind them, if I did.

I'm happy with things as they are right now, though... (^=

TheLinuxDuck
07-26-2001, 04:46 PM
Originally posted by binaryDigit:
<STRONG>
loop variable index - index, index1, index2, etc...</STRONG>

That's qool.. makes sense... I do have one exception to loops.. if I'm writing a double loop, I use 'inner' and 'outer' for each segment, so it's very obvious which is what...

Strike
07-26-2001, 06:58 PM
Underscores are better than StuDlY CaPs, in my opinion. And you have to have some way of separating long names....

optech
07-26-2001, 07:03 PM
no caps in my vars at all... i can't stand that... and i don't put vowels either...
usually, for quick and easy variables (like counters) i use i for a single,
or x,y,z for multiple dimensional arrays...

my subs/functions are usually something to do with the function itself..
ie.
if it's supposed to sort ****, then i'll name it
sort_**** or srt_sht

i also do a sort of hierarchy with _'s...
like javascript's .'s that way it's easy to classify your variables (and most languages won't let you use . in a variable...)

sans-hubris
07-27-2001, 01:16 AM
For loops, start with i, and go down the alphabet (e.g., the next letter would be j, then k, etc.) Usually, I don't get past j (and one really ought not to.) I avoid acronyms always because people always forget what they originally meant. I think acronyms are a Bad Thing(tm). OTOH, I do try to keep my stuff succinct. I try to use names or nouns described in the original problem description. That makes it easier to see how things are going.

As far as caps or underscores, it depends. I'll do what the other people are doing. Usually, on my own projects, I'll use underscores.

Temporary variables are always buffers.

Global variables: rarely, but there are certain applications for them (e.g., games sometimes.)

jemfinch
07-27-2001, 02:37 AM
Recently I've been using very short variable names, but I use them very consistently.

If a function takes and operates on string, I call that string "s". If it takes and operates on an array, I call that array "a". Sames with lists and "l" and hashes and "h". With numbers, I generally use "n" unless it's to be used as an index, when I use "i" instead. In my irclibs (both in Python and in O'Caml) I use "m" for my IRC message objects.

I use slightly longer names sometimes, but generally, the blocks of code that I use are small enough that the Really Short Variable Names are descriptive enough to carry a programmer through my code.

Function names, however, are a different story. I'll make a function name 20 characters long if that's what it takes to describe what it does. Names like "add_callback" are pretty standard, and I've been known to write stuff like "hostname_from_hostmask" or even worse on occasion. (Actually, I cheated a bit on the last one: I dropped the underscores, used a bit of latin, and changed a bit of my nomenclature: "hostexhostmask" was what I eventually used :))

Objects are fairly easy for me. I name them as what they are: "irc_connection", "irc_user", "callback", or whatever. It's really simple.

I almost got into using capitals in my naming, but now that I'm writing in O'Caml (which doesn't allow you to start variable names with a capital, and most other code, including the standard library, uses underscore-separated names anyway) I've gotten back with the holy "underscore_separated_words" naming scheme :)

Jeremy_aka_jemfinch

tecknophreak
07-27-2001, 08:29 AM
Let's see,

structs/classes - name them after what they do, logically use caps for the names.

constants I also name in caps but that's where I draw the line.

in for loops i like to use the

for (int i = 0;.......)

That way I can use i,j,k where ever I want and not worry about them in other places.

Buffers I use buff/buf depending on my mood, or state of laziness. Temp data that isn't an array/string I use temp/tmp(explination below).

Counters used for the whole program are cnt or cntr for the same reasons as above.

Other than that, I use descriptive names for my variable.(or at least I try to).

I think that's about it. Underscore sometimes on the functions, but not always.

TheLinuxDuck
07-27-2001, 09:13 AM
Interesting, for sure... (^=

For most variable naming, it looks like most of us are on the same page. I disagree with underscore use, but as I said, that's because I've never gotten in the habit of using them. However, I know that if I was debugging someone else's code, I'd want longer more descriptive function/method and variable/field names instead of short squat stuff.. (^=

Thanks everyone for commenting!

bdg1983
07-27-2001, 12:39 PM
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

const string moo = "input.txt";
const string mooo = "output.txt";

int main()
{
ifstream mo;
ofstream moooo;
vector&lt;string&gt; mooooo;
string moooooo;

mo.open(moo.data());
moooo.open(mooo.data());

getline (mo, moooooo);
while (!mo.eof()){
mooooo.push_back(moooooo);
getline(mo, moooooo);
}
for (int mooooooo = mooooo.length()-1; mooooooo &gt;=0; mooooooo++){
moooo &lt;&lt; mooooo[mooooooo] &lt;&lt; endl ;
}

mo.close();
moooo.close();

return 0;
}

bdg1983
07-27-2001, 12:49 PM
Originally posted by jemfinch:
<STRONG> "hostexhostmask" </STRONG>

Ok, when I read this, mild dyslexia kicked in and I reversed the third and fourth letters... :eek: "Just what is he coding?!!" I thought to myself...

TheLinuxDuck
07-27-2001, 12:57 PM
Bradmont:

LOL!! That code sample is funny!! (^= (^=

You could make it even more confusing and make defines of the method names, calling them other mooo's.. (^=

::guffaw::

bdg1983
07-27-2001, 01:39 PM
You mean something like this:

#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

#define moooooooo main
#define Mo ifstream
#define Moooo ofstream
#define mooooooooo vector
#define mOo string
#define moO open
#define moooooooooo data
#define MOO getline
#define mooooooooooo push_back
#define Moo for
#define MOo close
#define moooooooooooo while
#define mOoOoOo const
#define moOoOoO int
#define mooooooooooooo eof
#define MoO endl
#define oom return
#define M00 0
#define moooooooooooooo length

mOoOoO mOo moo = "input.txt";
mOoOoO mOo mooo = "output.txt";

moOoOoO moooooooo(){

Mo mo;
Moooo moooo;
mooooooooo&lt;mOo&gt; mooooo;
mOo moooooo;

mo.moO(moo.moooooooooo());
moooo.moO(mooo.moooooooooo());

MOO (mo, moooooo);

moooooooooooo (!mo.mooooooooooooo()){
mooooo.mooooooooooo(moooooo);
MOO(mo, moooooo);
}

Moo (moOoOoO mooooooo = mooooo.moooooooooooooo()-1; mooooooo &gt;=0; mooooooo++){
moooo &lt;&lt; mooooo[mooooooo] &lt;&lt; MoO ;
}

mo.MOo();
moooo.Moo();
oom M00;
}

?

BTW, can you do #defines to make aliases for things like (, ), {, }, [, ], ==, ++, =, et cetera? :D

&lt;edit&gt; Forgot a #define for length &lt;/edit&gt;

[ 27 July 2001: Message edited by: Bradmont ]

Dru Lee Parsec
07-27-2001, 01:48 PM
At my current job they require the member attributes of classes to have an m_ in front. So we might have something like:

Hashtable m_selectedSeats;

In general, I subscribe to the standard java policy of naming classes with initial upper case letters and upper case of each "word" in the classname. i.e.

MouseEvent
MapListener
SeatMapAssignerService
and so on.

For method names within a class I use the same naming convention except that the initial letter is lower case. So method names would be something like:

getName()
setName(String name)
findTicketsByPerformanceId(int id)

Within my methods I use i, j, and k for loop variables. I use temp for a temp variable (If I have more than 1 or 2 temp variables then that's a good sign that my logic is overly complex. I'd even say that more than 1 temp variable points to a design problem)

For variable names I use the same schema as method names. Lower case initial letter, upper case for additional "words". i.e.

count
index
selectedSeat
and so on.

But wait a minute! What about Hungarian notation? Well, I use to use it exclusively with I was a C++ programmer but I've never seen it used in Java.

jscott
07-27-2001, 01:50 PM
#define FALSE 0
#define TRUE 1

let the fun begin ;)

BrianDrozd
07-27-2001, 02:02 PM
Originally posted by Bradmont:
<STRONG>BTW, can you do #defines to make aliases for things like (, ), {, }, [, ], ==, ++, =, et cetera? :D
[ 27 July 2001: Message edited by: Bradmont ]</STRONG>

Yes, actually, you can, for some of them at least. I've seen it done on ==, ++, and --. It's annoying as He||, but it can be done. The preprossesor simply replaces the defined term with the correct symbol regardless of what that symbol is. But, some of those symbols may or may not have a meaning to the preprossesor, which limits what symbols you can replace (The parentasis for example has some meaning beyond a simple '(' or ')' to the preprossessor, but I don't know if that means you can or cannot define a term to be equal to one.)

bdg1983
07-27-2001, 02:15 PM
Ok, thanks. That's sorta what I was thinking.

Stuka
07-27-2001, 05:08 PM
Dru Lee-
Whatever diabolical individual thought up Hungarian notation oughta be deported to Hungary! ;) Being forced to read MS documentation (which almost religiously follows that evil notation) is tough on the eyeballs. Personally, I use a (way too) varied style. Sometimes underscores, sometimes StudlyCaps, etc. However, I always use i for my first index, all caps for constants, and I capitalize class names. Other than that, it depends on the mood I was in when I started the program.

Dru Lee Parsec
07-27-2001, 06:08 PM
It was Charles Simonyi and he worked for Micro$oft when he invented it.