• 0

[C++] Variable names


Question

Do any of you use naming conventions for your variables? My tutor at college says don't bother but I was wondering if it was a good idea? A book I've got says use a letter at the start of a variable name as follows:

n for int

l for long

f for float

d for double

c for character

sz for string

and then for every new word start it with a capital letter.. But what would you use for other variable types? b for bool? Also, I've been told that when declaring constant veriables it's best to do it all in capitals.. Is this all standard stuff with programmers?

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Just use something that you can understand when you go back later and look at it again. Try to be as descriptive as possible.

Also, I've been told that when declaring constant veriables it's best to do it all in capitals.. Is this all standard stuff with programmers?

Yes.

#define SOME_VARIABLE 1

is the way you should do it.

Link to comment
Share on other sites

  • 0
Just use something that you can understand when you go back later and look at it again.  Try to be as descriptive as possible. 

Yes.

#define SOME_VARIABLE 1

is the way you should do it.

584733222[/snapback]

Or if you wanna be typesafe

const <type> SOME_VARIABLE = <value>;

Link to comment
Share on other sites

  • 0

Hungarian notation :x

It sounds good until you have something like intNumTimesWon, where it's self-explanatory. Or when you get things like lpszMyString, with multiple prefixes...

Link to comment
Share on other sites

  • 0
Where do I put the const one? You put the define method under your includes is that correct?

584735382[/snapback]

Yes, although you can put #define anywhere. #define is especially helpful to prevent #include of a file more than once. const you can put anywhere too, but generally up at the top after #include and #define since you pretty much use those as global variables.

Link to comment
Share on other sites

  • 0

#define, #if, and any other preprocessor statements are expanded before they even thouch the compiler. So as per the #define example above, SOME_VARIABLE will always be replaced in source by 1. This is different than a const variable. The numeric value is not replaced; the variable is always used in calls.

Link to comment
Share on other sites

  • 0
#define, #if, and any other preprocessor statements are expanded before they even thouch the compiler. So as per the #define example above, SOME_VARIABLE will always be replaced in source by 1. This is different than a const variable. The numeric value is not replaced; the variable is always used in calls.

584736872[/snapback]

Yeah, the result is pretty much the same though, one value staying constant throughout the code, unless of course it's redefined.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.