Favorite naming convention?

Pages: 12
What is your favorite naming convention?My personal favorite is lower camel case

example:
int myNewVar;
I tend to use the upper camel case style, though I do use lower camel case when the first "word" is a single letter. (Like iSide or jSide instead of having a capital i or j.)
1
2
3
4
Type1 type1[12];
Type2 type2[5];
std::cin >>type1[0];
type1[1]=type1[0]*type2[3]

Did I just defeat the whole purpose of named variables? Yes, because I'm hardcore.
Last edited on
closed account (z05DSL3A)
Camel case is a Capitalisation Style not a naming convention (could possibly considered as part of a naming convention).

Whether I use camel case or pascal depends on what I am naming.
Last edited on
I try to call things relevant names.

I have some conventions:
1
2
3
int this_is_a_function(int andThisIsAVariable) {
    return 0;
}


I have also taken to prepending underscores for functions that aren't meant to be called from other functions; e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int sleep_for_ten_seconds(void) {
    int secondsSlept = 1;

    while (secondsSlept <= 10)
        _sleep(1);

    return secondsSlept;
}

void _sleep(int seconds) {
    int start = (int) clock() / CLOCKS_PER_TICK;
    int end = start + seconds;

    while ((int)clock() < end); /* Use up the entire CPU >:) */
}


I've noticed how in the Linux kernel they've #define'd some "empty" symbols, like
#define __init
and then they prefix function definitions called by init with __init, like
void __init initialize_something(void)
I think that's quite a good idea.
Last edited on
Did I just defeat the whole purpose of named variables? Yes, because I'm hardcore.


rofl
Lol...
closed account (iw0XoG1T)
I am very interested in this subject—seeing how others organize their code helps me quite a bit.

At the present I am using the following naming conventions:

Function are named with verbs and are all lower case and words are separated by a “_”.
Variables are lower case nouns and words are separated by a “_”.
Classes are nouns with the first letter capitalized separated by a “_”.
If a variable has a very limited scope it should have a short name.
If a variable has large scope it should have longer and more descriptive name.
Pointers are all prefixed with “p_”.
Header wrappers are named after the header with every letter capitalized and the “.” is replaced with a “_” ; e.g. “MYHEADER_H”.
Macros are long descriptive names all in capitals.
Enumerators are all capitalized.

I choose my conventions based on what I see experienced programmers using.
Macros don't need to be particularly long, but all in caps I agree with. It separates them. I hate when people make data types and capitalize them; I've seen
1
2
3
enum BOOLEAN {
    FALSE, TRUE
};


Pointers are all prefixed with “p_”.

What so like this:
int myInt, *p_myInt;?

Header wrappers are named after the header with every letter capitalized and the “.” is replaced with a “_” ; e.g. “MYHEADER_H”.

I do them like this:
#ifndef _MY_HEADER_H_
I separate words with underscores and pre- and suffix them.
Last edited on
closed account (iw0XoG1T)
@chrisname

I am new to programming, but I almost never declare multiple variables on the same line, and when I declare a pointer I put the “*” next to the type.
int* p_mypointer;

I am not sold on this convention and I really have only seen it used a few times, but it seems like a good idea to me.

I also have taken up the practice of ensuring that functions and classes can all be read on one screen. For a class I declare all the members in a one header, and keep all the definitions in a separate source file named the same as my class , and if a function gets too long I break it down into smaller functions.
Last edited on
I am new to programming, but I almost never declare multiple variables on the same line, and when I declare a pointer I put the “*” next to the type.
int* p_mypointer;

So do I. But I also declare multiple things on one line:
int* myPointer = NULL, **pMyPointer = &myPointer;
The reason most programmers tend to put the * next to the variable is because you're declaring the variable as a pointer to int; so technically the datatype is int; but the variable is a pointer to that datatype. So when you declare multiple pointers on one line you have to put an asterisk (or more) after every comma...
Also you should always initialize pointers to NULL if not anything else.

I believe NULL is defined as ((void*) 0l). 0l is 0 long.

I also have taken up the practice of ensuring that functions and classes can all be read on one screen. For a class I declare all the members in a one header, and keep all the definitions in a separate source file named the same as my class , and if a function gets too long I break it down into smaller functions.

That's good.

And by the way, I'm also fairly new. I've got 9 months under my belt so far.
Then again that's enough time for a foetus to develop into a child...
Last edited on
1
2
3
4
5
6
7
#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif 
Oh... I've always thought it was 0l.
Nevermind.
You know whats funny? I used classes now for the first time ... ever in all programming languages, I kept procrastinating but now that I learned I feel like I grew 2 extra arms.
Same as me with pointers. I love pointers.
Hehehe I am procrastinating with pointers as well...
Don't! Pointers are awesome!
Is there an article or something?
void *p=(void *)&p;
... I am still confused :(
Pages: 12