LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 February 10 2015

Badieh
Member

Programming Naming Convention

Hello All,

I've been trying to find the best way to name variables, classes, forms, controls, etc... In our .NET projects. However, it seems like there are a lot of controversy when it comes to naming, and so I'd like to see what you guys think.

To start off, I have the following questions:
1 - Do you prefix your controls with their type abbreviation? Or do you Suffix them with their type name? Or do you suffix them with a simple 'Label / Field' ? (EX: txtFirstName , FirstNameTextBox, FirstNameField)
2 - When adding a new form, do you prefix the name, suffix the name, or leave it as it? (EX: frmLogin, LoginForm, Login)
3 - When you instantiate a private object from a Class, do you prefix it? If yes, what with? oProgram, cProgram, mProgram, _Program?
4 - Do you use the prefix 'm' for local private variables, and 'g' for public global variables?

Thanks :)

Last edited by Badieh (February 10 2015)

Offline

#2 February 10 2015

rolf
Member

Re: Programming Naming Convention

Camel Case FTW!

Offline

#3 February 10 2015

samer
Admin

Re: Programming Naming Convention

One approach would be to find a popular style guide and stick with it.
Examples:

- Microsoft style guide for C#
- Google style guide for C++
- Airbnb style guide for JavaScript

Offline

#4 February 11 2015

Badieh
Member

Re: Programming Naming Convention

Thanks, however, these guidelines do not answer my questions. I've seen them before, and this is why i asked these questions specifically :).

Offline

#5 February 11 2015

xterm
Moderator

Re: Programming Naming Convention

Back when I was developing for .NET, we used StyleCop to deal with style guidelines. When all the rules are left checked, it can be very annoying to deal with, but it's all for the better. You get used to a strict style of writing code and it becomes muscle memory.

What you're asking about break the following two rules, so my answer to you is NO:
SA1305: FieldNamesMustNotUseHungarianNotation
SA1308: VariableNamesMustNotBePrefixed

Good luck.

Offline

#6 February 11 2015

Badieh
Member

Re: Programming Naming Convention

Thanks xterm for your input, I've read so many posts hating on hungarian notations. Kind of easy to avoid for variables, but kind of sucks for Controls. How would you name your controls? TextBoxUserName? UserNameTextBox? UserNameField?

Offline

#7 February 12 2015

MAS
Member

Re: Programming Naming Convention

Badieh wrote:

To start off, I have the following questions:
1 - Do you prefix your controls with their type abbreviation? Or do you Suffix them with their type name? Or do you suffix them with a simple 'Label / Field' ? (EX: txtFirstName , FirstNameTextBox, FirstNameField)
2 - When adding a new form, do you prefix the name, suffix the name, or leave it as it? (EX: frmLogin, LoginForm, Login)
3 - When you instantiate a private object from a Class, do you prefix it? If yes, what with? oProgram, cProgram, mProgram, _Program?
4 - Do you use the prefix 'm' for local private variables, and 'g' for public global variables?

Hey,
there's no general rule for what you are asking, but there's a best practice,

1- For that its better to stick to one convention for all the controls, FirstName_TextBox for example, here the name is divided into the DescriptionOfTheField_DescriptionOfTheControl.
2- LoginForm or Login_Form, leaving a Form name without prefix/suffix will make you suffer when the project get bigger or when you come after a while to check something in it.
3- For private object name, lower first character case is enough : program.
4- For private variables add "_" , _transactionDate , public variables: TransactionDate.

Offline

#8 February 13 2015

Badieh
Member

Re: Programming Naming Convention

MAS wrote:
Badieh wrote:

To start off, I have the following questions:
1 - Do you prefix your controls with their type abbreviation? Or do you Suffix them with their type name? Or do you suffix them with a simple 'Label / Field' ? (EX: txtFirstName , FirstNameTextBox, FirstNameField)
2 - When adding a new form, do you prefix the name, suffix the name, or leave it as it? (EX: frmLogin, LoginForm, Login)
3 - When you instantiate a private object from a Class, do you prefix it? If yes, what with? oProgram, cProgram, mProgram, _Program?
4 - Do you use the prefix 'm' for local private variables, and 'g' for public global variables?

Hey,
there's no general rule for what you are asking, but there's a best practice,

1- For that its better to stick to one convention for all the controls, FirstName_TextBox for example, here the name is divided into the DescriptionOfTheField_DescriptionOfTheControl.
2- LoginForm or Login_Form, leaving a Form name without prefix/suffix will make you suffer when the project get bigger or when you come after a while to check something in it.
3- For private object name, lower first character case is enough : program.
4- For private variables add "_" , _transactionDate , public variables: TransactionDate.

Thanks, this is what I went with in the end, except without the underscores since some hate on it. As for private vs Global members, I am prefixing them with 'm' and 'G'. I wanted to go with the underscore, but again, many hate on that.

Offline

Board footer