Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,484 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,222 people online right now. Registration is fast and FREE... Join Now!




Naming Conventions

2 Pages V  1 2 >  
Reply to this topicStart new topic

Naming Conventions

Mike007
post 19 Jun, 2008 - 07:27 PM
Post #1


D.I.C Head

Group Icon
Joined: 30 Aug, 2007
Posts: 200



Dream Kudos: 75
My Contributions


I've had quite a few problems with naming conventions in the past (as I’m sure a lot of you had) and now i'm trying to find the best set of rules i can always follow to be more consistent.

The problem is not that big when just using the same language like when using Java it is absolutely clear to me that the best practice is to use camelCase naming system. But what about when read a value from a field in a database. The field name is probably going to be in lower case separated by _ (field_name). Therefore, it kinda makes sense to call a variable that gets that value "field_name". I used this logic for my PHP scripts, until i started using some classes, and using phpDoc so it works much better using Java naming conventions.

There was also something I’ve seen in quite a few programing books I read, called the Hungarian notation. I found this to work great with visual c++ for user interface components, giving them a name such as lblFirstName and txtFirstName is very nice when you want to validate a form for example and highlight any problems with the data . This principle also works well in other languages such as Java and is also good for HTML/Javascript form validation.

But again there is a problem to how far to actually take this. The following rules seem to work for the most part:
1. Data related components(file names, database design, XML, HTML tags) should use a lower case names separated by an underscore (_).
2. Data components related to programming(programming languages) should use camelCase.
3. GUI components that will be manipulated by the program(HTML and GUI classes in some programing languages) should use Hungarian notation.

However, I still find myself breaking them every once and a while, sometimes they don't make much of a sense.

So I guess my question to you is how do you deal with this? What set of rules do you follow?
Do you believe that every language or every situation needs to adhere to different naming standards? or a universal standard needs to be created?

P.S.

I didn't even talk about abbreviations and too wordy names.

Here is some information I found in Wikipedia: http://en.wikipedia.org/wiki/Identifier_naming_convention.
User is offlineProfile CardPM

Go to the top of the page

Einherjar
post 19 Jun, 2008 - 08:34 PM
Post #2


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


I have such a hard time with naming conventions as well, especially when it's just a project I'm working solo. I almost always use camelcase when I'm programming in something like C/C++/C#/Java and then switch over to underscores when I'm using PHP/Ruby. But honestly I mix and match, and sometimes I will use hungarian notation and sometimes I won't. Can be frustrating. I would really like a universal naming standard between languages just so I could be consistent with myself.
User is offlineProfile CardPM

Go to the top of the page

rgfirefly24
post 21 Jun, 2008 - 04:24 AM
Post #3


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 330



Thanked 5 times

Dream Kudos: 150
My Contributions


something i was sent by my boss

Programming standards
User is offlineProfile CardPM

Go to the top of the page

Tom9729
post 21 Jun, 2008 - 09:39 AM
Post #4


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,447



Thanked 10 times

Dream Kudos: 325
My Contributions


Well from what I understand, most programming projects/jobs will have coding conventions you must follow that dictate things like this.

That said, when coding Java I usually stick to their naming style (itLooksLikeThis).

In almost anything else I use the C-style, with_underscores_like_this. It's just more readable to me.

Naming conventions are just something that you get used to.
User is offlineProfile CardPM

Go to the top of the page

KYA
post 22 Jun, 2008 - 12:21 AM
Post #5


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,209



Thanked 50 times

Dream Kudos: 1150
My Contributions


Hungarian notation is fading in my opinion since languages are strongly typed nowadays.

(For those who don't know Hungarian notation is putting a letter in the front of the variable name to indicate what it is i.e.smile.gif

cpp

iNumber /*integer*/
pDiddy /*pointer*/
/*etc...*/


This post has been edited by KYA: 22 Jun, 2008 - 12:21 AM
User is offlineProfile CardPM

Go to the top of the page

Einherjar
post 22 Jun, 2008 - 04:34 PM
Post #6


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


QUOTE(KYA @ 22 Jun, 2008 - 04:21 AM) *

Hungarian notation is fading in my opinion since languages are strongly typed nowadays.

(For those who don't know Hungarian notation is putting a letter in the front of the variable name to indicate what it is i.e.smile.gif

cpp

iNumber /*integer*/
pDiddy /*pointer*/
/*etc...*/



There are still a few though that are prevalent, especially popular ones like PHP/Javascript/Perl where it can be useful to use. Even though I don't so much anymore...
User is offlineProfile CardPM

Go to the top of the page

Mike007
post 22 Jun, 2008 - 09:25 PM
Post #7


D.I.C Head

Group Icon
Joined: 30 Aug, 2007
Posts: 200



Dream Kudos: 75
My Contributions


@rgfirefly24- Thanks for the link, looks interesting. I would like to see some more of these documents on the web tho, particularly for web development.

@tom- Well the underscore thing works great when it comes to procedural programming, but how often do you really see that today? (except like in javascript). So for OOP I think the best choice is camelCase to differentiate between classes and everything else. I actually used the CSomeClassName thing that was a standard in one book and recommended by Microsoft in the old MFC age. But nowadays that looks pretty silly to me.
I know that every project has its' own conventions set up for it, but I was hoping to set some for my personal projects. Maybe even set some guide lines for </D.I.C> members to have more consistent tutorials and snippets.

@KYA- well Hungarian can be more than one letter can also be 3. I use it as I mentioned before in HTML forms when I need to identify a label and a text field or another component to notify the user of any mistakes he/she as made in entering the information. Is there a better way to do that?
User is offlineProfile CardPM

Go to the top of the page

KYA
post 22 Jun, 2008 - 11:16 PM
Post #8


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,209



Thanked 50 times

Dream Kudos: 1150
My Contributions


Sounds like a good application of that naming convention icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

Tom9729
post 23 Jun, 2008 - 11:10 PM
Post #9


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,447



Thanked 10 times

Dream Kudos: 325
My Contributions


QUOTE(Mike007 @ 23 Jun, 2008 - 01:25 AM) *

@tom- Well the underscore thing works great when it comes to procedural programming

Procedural programming is I guess what I do the most of then. smile.gif

I don't think you'll find any one standard convention though. As you've pointed out, different languages and programming paradigms call for different naming conventions. I say that if it's just for your personal projects, use whatever you like the most. icon_up.gif

I've never heard of this "hungarian" style before, but it sounds like just making descriptive sounding variable names. I usually use one or two letter variables to be completely honest, I just get so sick of retyping long names. If a long name really is necessary, then I'll use preprocessor macros to shorten it up a bit. Otherwise, I try to keep my functions short (so that whoever is reading it can see the declarations and the body at the same time), and I try to write comments that explain what the #%!^!# is going on.
User is offlineProfile CardPM

Go to the top of the page

1lacca
post 24 Jun, 2008 - 01:43 AM
Post #10


code.rascal

Group Icon
Joined: 11 Aug, 2005
Posts: 3,822



Thanked 11 times
My Contributions


QUOTE(Tom9729 @ 24 Jun, 2008 - 09:10 AM) *

I've never heard of this "hungarian" style before, but it sounds like just making descriptive sounding variable names.

It's Hungarian, because it's a nation, and I'm one of them, so watch your capitalization crazy.gif
On the other side I don't use it, because I mostly program in Java, and it has it's own recommended convention, that I find pretty clear and fine.
A big advantage of the Hungarian notation is that since you can guess the type of the variable from it's name, you don't have to look up it's declaration when you land in the middle of a class or function, and this makes reading and understanding the code easier. Sure, a modern IDE will show it in a hint as well, so it's not that important nowadays. Historically the source of Windows was using this notation.
User is offlineProfile CardPM

Go to the top of the page

Tom9729
post 24 Jun, 2008 - 02:48 PM
Post #11


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,447



Thanked 10 times

Dream Kudos: 325
My Contributions


QUOTE(1lacca @ 24 Jun, 2008 - 05:43 AM) *

QUOTE(Tom9729 @ 24 Jun, 2008 - 09:10 AM) *

I've never heard of this "hungarian" style before, but it sounds like just making descriptive sounding variable names.

It's Hungarian, because it's a nation, and I'm one of them, so watch your capitalization crazy.gif

I'm usually pretty good about capitalization, proper nouns, all that jazz. I'm not really a morning person though. smile.gif
User is offlineProfile CardPM

Go to the top of the page

RodgerB
post 24 Jun, 2008 - 10:29 PM
Post #12


D.I.C Lover

Group Icon
Joined: 21 Sep, 2007
Posts: 2,126



Thanked 15 times

Dream Kudos: 2200

Expert In: Dot Net Technologies

My Contributions


Wow, I knew barely anything about these naming conventions yet I've been following them almost perfectly. wink2.gif

Must have been all the code I've been reading. smile.gif
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 03:26PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month