views:

5672

answers:

16

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?

+70  A: 

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.

Jon Skeet
...and it upsets the natives.
Paul Beckingham
Exactly, and only one thing to add: There's a standard for HTML casing... all HTML code should be lowercase, so that's pretty straight forward
arnorhs
Except when you're using MFC or Win32, because pseudo-hungarian notation is evil and must be stamped out everywhere.
Randolpho
hungarian notation does work to the benefit of all of us... i'm sorry that it's confused/mistaken by the company behind it (and sorrier that C# marketers HAD to kill it)...
jpinto3912
@Downvoter: Care to give a reason?
Jon Skeet
And for the downvoter today? Reasons?
Jon Skeet
Haters, that's all Jon Skeet :P
Aequitarum Custos
+8  A: 

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.

Quintin Robinson
+9  A: 

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.

froh42
+1 for the mention of hungarian notation
rmeador
This is entirely off-topic, but I don't get your comment re: Hungarian. Encoding the purpose of an object when the type system isn't powerful enough to express that information is exactly the *point* of Hungarian notation. Of course, the *real* fix is to use a language with a better type system.
Jörg W Mittag
@Jörg: I think he talks about systems Hungarian, which is pretty much useless; I agree that there's nothing wrong with apps Hungarian, though
Christoph
Was going to vote for this but won't based on Hungarian comment. If the system you are maintaining uses Hungarian, you should too. I'm not a big fan of it, but don't be so dogmatic.
Steve Rowe
+! Can't stand Hungarian notation
scunliffe
Jörg, OK, I see a possible usage of somthing like Hungarian when you use a language with a strange type system. Otoh with the latter languages type is not so much the issue, more the question whether an object responds to a particular message.
froh42
+16  A: 

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...

Jared Oberhaus
+1 Following the dominant convention in the language or codebase is the best option.
Steve Rowe
+1: Applying it to coding style: "Be consistent" in two words is better than most longer coding styles.
Richard
+11  A: 

VOTE: I prefer camelCase.

Wedge
+9  A: 

VOTE: I prefer under_scores.

Wedge
A: 

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.

Jeremy Edwards
+10  A: 

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*.
Svante
+1 I couldn't agree more. The most important thing is to HAVE a style, regardless of its specifics, and stick to it consistently.
Alan
+1 Naming conventions are a pain to re-learn on each platform, and being consistent requires a lot of mindfulness, but the pluses of being able to guess behavior and grep for stuff outweigh those minuses.
Sarah Mei
+3  A: 

There is no one naming convention for all languages - if the language has its convention - then follow it.

  1. php standard is under_score_naming
  2. SQL - I have seen conventions to use lowercase for table and column names AND uppercase for reserved words
  3. Java - camelCase, explicitly specified and followed by everyone
  4. 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)
  5. C++ stl uses under_scores
  6. 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 variable
  • ao_Object for object passed as function parameter
  • mi_Int for a integer member variable
devdimi
+6  A: 

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. :-)

Leonard
+1 beautiful response. On the other hand, I think they're just conventions, and that the "eye" (well, the head) can learn to scan with either type of casing. 25-line methods are the shizzle, of course.
Yar
+1  A: 

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.

mipadi
+1  A: 

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.

overstood
A: 

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?

Kevin Won
Yes, if it's a private member it clearly indicates that it's a member as opposed to a function parameter or something.
RCIX
+2  A: 

Just, please, don't do this: thing_name_onSignalItGot()

It may be clear, and standardized, but it's headache inducing.

Kim Reece
+3  A: 

i_find_it_easier_to_read_text_with_underscores_separating_the_words, RatherThanHavingAnInitialCapitalLetterSeparatingTheWords.

Jonas Gulle
for a_single word it_is_ok_but_this_become_awful when several_symbols_are close_together.This IsWhy CamelCase Is Better.
Jem
Dude... camelcase or underscores the same, you should seriously work on reducing the length of your method names...
Alderath
To be honest, I found both of those dead easy to read...
Martin
@Jem: ButCamelCaseCausesProblemsWhenAcronymsLikeURLAreUsedAndAnywayHowOftenDoYouSeeVariableNamesThatOrThisLong?
Joe D
A: 

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).

Paul