Forum Moderators: open

Message Too Old, No Replies

Just a General Question

about capitalization of function names

         

cookiemonster

10:09 pm on Sep 3, 2010 (gmt 0)

10+ Year Member



Hi,

I've noticed that when people (including myself) define functions in javascript, or any programming language for that matter, the first letter of the second word in the name of the function is capitalized.

Ex.
function myFunction()

instead of
function myfunction()


Is there a technical reason for this, or is it just for readability... or is it simply a habit programmers have gotten in to over time?

No rush, just a casual question :P

Thanks!

LifeinAsia

10:26 pm on Sep 3, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I also find it strange. I can't speak for others, but I almost always capitalize for readability. So I would most likely write it a little differently:
function MyFunction()

Similarly, I try to completely capitalize any reserved words in SQL statements:
SELECT Field1, Field2
FROM MyTable
ORDER BY Field1

That makes sense to me, but others may have a different logic behind their schemes.

daveVk

4:16 am on Sep 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LifeinAsia vs LifeInAsia vs lifeinasia vs lifeInasia vs liFeinasia etc. Go for readability.

rocknbil

6:03 pm on Sep 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a well argued topic without an answer, but CamelHumpNameConventions can lead to more time in debugging. I do it myself, but shouldn't, when I'm being particularly attentive I try to adhere to underscores: my_function();

Fotiman

1:19 pm on Sep 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Each language generally has some common "code conventions" (and in some cases, there may be multiple conflicting conventions).

Java:
[oracle.com...]

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.


PHP
[framework.zend.com...]

Function names must always start with a lowercase letter. When a function name consists of more than one word, the first letter of each new word must be capitalized. This is commonly called "camelCase" formatting.


For JavaScript, I think the most well known conventions may be those defined by Douglas Crockford:
[javascript.crockford.com...]

Most variables and functions should start with a lower case letter.

Constructor functions which must be used with the new prefix should start with a capital letter.


Though that does not provide a very strict guideline with regards to whether you should use myFunction or my_function. I tend to use myFunction, because it is a well established convention in other languages, but also because my_function adds another character but conveys the same meaning. I don't find that using camelCase leads to any increased time debugging.

Also, as far as myFunction vs. myfunction, the former is more readable because you can clearly see the beginning of each word.

camelcasemakesiteasiertoread
camelCaseMakesItEasierToRead

Don't you think? :)

rocknbil

7:40 pm on Sep 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't find that using camelCase leads to any increased time debugging.


This is only because you're sharp at spotting the difference between

camelCaseMakesItEasiertoRead
camelCaseMakesItEasierToRead

Which is a good deal of many of the newbie posts in many forums here. :-) At the end of a long day, these drive me nuts. I find if I stay to all lower case, it's one less thing to look for and the underscores also make it more legible. It's certainly not standard but makes things a bit faster for me.