I'm very unsure when it comes to naming conventions. I really want to adopt a stringent naming convention for class-names, variables, function-names, html-attributes or database schemata. But whenever I stick with - say camelcase - I am screwed up when working with the PHP language because they use underscores heavily. Or I realize that I should adhere an existing db naming scheme where all tables begin with t_. So until this very day I haven't found a cross-language (php, java, c++) and cross-storage-media (SQL, file-system) naming convention that is tidy and stringent. What are your experiences and which methods work for you best?
My experience is: go with the dominant convention on the platform. For instance, in Java methods are camelCased; in .NET they're PascalCased. If you start bringing over conventions from one platform when you start coding on another, it just doesn't look right.
On some platforms there are no dominant standards - so find something you like and stick with that. Where there is a standard, stick to it.
I generally HATE underscores except to precede a private member. Then I use a combination of Pascal & camel.
IE: Parameters are camel, properties/methods Pascal etc. HTML attributes are always all lowercase.
But when I see underscores to seperate_an_item there is a little rage bubble that pops inside.
But like Jon said, try and stick with the conventions of the Language.
I try to follow the naming convention that seems most prominent with the programming language at hand. Even .NET and Java have some differences despite being very similar languages, so I want my code to blend in to what the system libraries do.
If possible I use tools (soure code formatters) to ensure my code looks native.
Oh but: I absolutley REFUSE to write hungarian notation. I don't care what an object is I want to know what its purpose is.
I always follow the naming convention that seems prevalent in the language or project that I'm working on. It has the advantage that other people that are familiar with that language or project will immediately recognize the style you're using. Of course, it has the disadvantage that you have to adapt every time you switch to such a project/language.
I would also apply this to coding style. If you are working on a project where the convention is to put the { on the previous line, do that. If it's to put it on the next line, do that.
But when others don't follow the project conventions, and adhere to their own personal style, I can always tell who wrote that code...
Stick with the convention that the language uses. Most developers usually will follow the same pattern. There are some places where it may be a good idea to go away from the norm just because you may be a multi-lingual developer. One good example is the use of hyphens within property names.
Progress ABL allows you to do this but it's something I dislike because in other languages that will mean subtraction.
Another area is all caps for keywords. HTML had this as a recommendation until someone probably realized it was a bad idea. So now the contention is lowercase which is more reasonable.
For underscores I use them personally but I believe some languages don't accept them for variable names. So be mindful of the contention you use and try to make it cross-language as much as possible.
As with any coding style questions, the important thing is to be consistent, and to have a good reason.
Whether you use PascalCase, camelCase, or under_scores is really not important, but it is important that you use them consistently. Many languages have a standard style which is usually a good convention.
Examples:
- Java uses camelCase for methods, and PascalCase for classes.
- Common Lisp generally uses hyphens for
combined-names
, and there is a convention to enclose constants in+signs+
and global parameters in*asterisks*
.
There is no one naming convention for all languages - if the language has its convention - then follow it.
- php standard is
under_score_naming
- SQL - I have seen conventions to use lowercase for table and column names AND uppercase for reserved words
- Java -
camelCase
, explicitly specified and followed by everyone - C# -
PascalCase
Actually pre-release the code was camelCased, but short before release they changed it to PascalCase ( Anders Hejlsberg wrote one the first versions Borland Pascal) - C++ stl uses
under_scores
- Win32/COM uses
PascalCase
for function names and Hungarian notation for variable names
Generally I would recommend against using the same naming conventions for all languages. I have seen a shop, that tried to do this - unfortunately it was VB style Hungarian. So in even java code they used:
ls_String
for a local string variableao_Object
for object passed as function parametermi_Int
for a integer member variable
Obviously there's some flavor of a religious war here. Arguably, from a human factors point of view the underscore is quite close to being a space, and provides more physical separation that leads for some to clearer semantic separation. I searched the web for any usability studies in this arena and came up empty.
I have a suspicion that different programmers grok code differently. Jakob Nielsen describes how people scan web pages rather than reading them. It's a personal conviction of mine that some programmers 'scan' code, and others 'read' it in detail, and that scanners are much more interested in formatting, legibility, syntax coloring and so forth, and gather their info that way. (Damian Conway said once in a talk I heard that he hates syntax coloring, which was stunning to me - I can't be without it). I suspect programmers who read the details might be less interested in the conversation either way, but might go for camelCase some of the time, whereas 'scanners' are more likely to lean towards underscores.
I admit this is all wildly speculative on my part...but as a 'scanner' myself I use underscores, align assignment operators, align opening parens when I have groups of prototypes (and do various other alignments as well). My functions tend to be about 25 lines long, so I can grok them at a glance. I claim the prize on my team for "easiest code to understand. :-)
I generally go with the general consensus of whatever language I happen to be working in. Java, for example, generally uses camel case, so I use camel case when writing Java, too. C and Python, on the other hand, generally use underscores, so I use underscores there, too.
If you have to ask, go with the standard.
If you have a good reason not to use the standard, then don't, but make sure you understand what you're trading off by not using the standard.
If you plan on developing for a decent chunk of your life, please do us all a favor and expunge the use of the underscore character wherever you can for the simple reason that it is hard to type and likely to cause your hands physical damage over the decades of typing it... and yes, we will still be stuck with injuryboards... I mean keyboards... for a long time.
Outside of convention, the underscore doesn't serve any purpose. So let's just drop it. Yes, I know it makes your code looks so cryptic and cool, but really, is it clarifying?
Just, please, don't do this: thing_name_onSignalItGot()
It may be clear, and standardized, but it's headache inducing.
i_find_it_easier_to_read_text_with_underscores_separating_the_words, RatherThanHavingAnInitialCapitalLetterSeparatingTheWords.
I have just been changing the style of my current project to match the Python style guide, PEP8. Its been an interesting exercise and Ive discovered that I have definite preferences. I used to use camelCase for methods and variables, and PascalCase for classes. Ive found after changing variables and functions to use underscores, I realise that:
(1) I dont like underscores for variables and I am changing them back. Both perceptually and semantically, camelCase makes more sense for variables. Perceptually, because the gaps left by the underscore breaks up the name and makes it harder to recognise as a single entity amongst other items. Semantically also, I parse variable names like a single, concrete entity, and the fused look of camelCase supports this metaphor. For example I have variables inLinks, outLinks, badLinks - I like them better when they appear like one word. Compare: in_links - it doesn't actually parse as an english phrase anyhow, so why make it look like one?.
(2) Similarly I like PascalCase for classes, again because I parse it as a concrete name, rather than an english description - its a 'thing'.
(2) However, I definitely prefer underscores for method names, because usually they need to be parsed as english descriptions of an action performed. A few examples from my code: is_orphan(), remove_label(), gather_and_propagate(). Here, the underscores are an advantage because I want to see the verb(s).