DEV Community

Cover image for under_scores, camelCase and PascalCase - The three naming conventions every programmer should be aware of
Prahlad Yeri
Prahlad Yeri

Posted on • Updated on

Pascal Case under_scores, camelCase and PascalCase - The three naming conventions every programmer should be aware of

The various tokens in your code (variables, classes, functions, namespaces, etc.) can be named using one of these three styles, broadly speaking:

  1. Camel Case (ex: someVar, someClass, somePackage.xyz).
  2. Pascal Case (ex: SomeVar, SomeClass, SomePackage.xyz).
  3. Underscores (ex: some_var, some_class, some_package.xyz).

In camel casing, names start with a lower case but each proper word in the name is capitalized and so are acronyms. For example, commonly used tokens in many languages such as toString, checkValidity, lineHeight, timestampToLocalDateTime, etc. are all examples of camel casing.

Pascal casing is similar to camel casing except that the first letter also starts with a capital letter (SomeClass instead of someClass).

In underscore casing, everything is in lower case (even acronyms) and the words are separated by underscores (some_class, some_func, some_var, etc). This convention is also popularly known as snake case.

The general idea is that you can use any convention in your project as long as you are consistent about using it everywhere. But when you code for a large project or team, you should conform to the norm of what is being used there. Hence, its helpful to be aware of the conventions typical in various programming languages.

The general practice for a C style language like Java or JS is to use camelCase for all variables and object members (properties & methods), and PascalCase for class names and constructors. Namespaces (or Packages in Java world) are usually in camelCase.

But some languages make an exception to that. C#, for example, uses PascalCase for namespaces and even public methods. Hence, the main function (or entry point) is always static void main() in java but static void Main() in C# (Note the capitalization of the word "Main").

Some languages which don't derive their syntax from C (such as Python & Ruby) use underscores for almost everything except class names. Hence, its always sys.base_prefix instead of sys.basePrefix, datetime instead of DateTime, str.splitlines() instead of str.splitLines() in python.

In case of python's standard library, I've noticed that even the classes use underscores sometimes which is an inconsistency. For example, datetime.datetime is a class and so is csv.excel_tab. The popular frameworks and libraries though (such as django and flask) use the camel case for classes.

Similar inconsistency is in PHP. The language is evolving from underscores to camel casing in recent years but some old tokens still haunts that language. For ex, mysqli::set_local_infile_default vs PDOStatement::debugDumpParams.

So, ultimately, it comes down to your own preference when you are starting a project. But it helps to know what are the usually followed conventions in popular open source projects in the language of your preference.

Update

There is a fourth case too as pointed out by @ovais, namely kebab case. Its very much like underline casing except that the underlines are replaced with hyphens (dashes). So, some_func becomes some-func which is obviously disallowed because dash isn't used for naming tokens as its already a built-in for the minus operator in most programming languages. Where kebab case is used most frequently is for creating classes in your css stylesheets! Names like main-div, main-navbar and article-footer are commonly used by web developers while writing their HTML/CSS. This convention is basically the kebab case.

Update

As @patrykrudnicki says, constants are handled differently. In my experience, the full underscores (SOME_CONST) is a popular convention for constants in many languages including Java, PHP and Python.

Update

To summarize, this is the typical or generally followed convention in the most used open source programming languages:

Token python Java/JS PHP
variable under_score camelCase mix (moving towards camelCase)
function under_score() camelCase() mix (moving towards camelCase())
constant UNDER_SCORE UNDER_SCORE UNDER_SCORE
class PascalCase PascalCase mix (moving towards PascalCase)
namespace under_score camelCase mix (moving towards PascalCase)

Some helpful links:

  1. https://softwareengineering.stackexchange.com/questions/196416/whats-the-dominant-naming-convention-for-variables-in-php-camelcase-or-undersc
  2. https://stackoverflow.com/questions/149491/pascal-casing-or-camel-casing-for-c-sharp-code
  3. https://www.c-sharpcorner.com/forums/when-to-use-camel-case-and-pascal-case-c-sharp
  4. https://softwareengineering.stackexchange.com/questions/53498/what-is-the-philosophy-reasoning-behind-cs-pascal-casing-method-names

Oldest comments (16)

Collapse
 
ovais profile image
Mohd Ovais

I believe it widely known as Kebab Case (kebab-case) instead of Underscore.

Collapse
 
prahladyeri profile image
Prahlad Yeri • Edited

No, kebab case is different. Its the dash or hyphenated case, so dashes are used instead of underscores (to-string instead of to_string). The only place where kebab case is used is perhaps css class names (main-div, navbar-div, etc.).

The popular name for underscore case is in fact, snake case.

Collapse
 
ovais profile image
Mohd Ovais

My bad, you are correct!

Collapse
 
mpmiszczyk profile image
mar

/kebab case/ as you call it is defacto standard naming convention in all Lisp's, starting room Scheme, trough Common Lisp, up to Clojure. It is phenomenon older than C and still going strong with her and current implementations.

Collapse
 
patrykrudnicki profile image
Patryk Rudnicki

I don’t know the naming, but probably in Java constant value you’re naming in all camel case characters with underscore between words. Good to add this too if this is the official naming convention.

Collapse
 
prahladyeri profile image
Prahlad Yeri

Thanks, I've updated the post with this point.

Collapse
 
patrykrudnicki profile image
Patryk Rudnicki

Yeah, great. Good to point it too. But I mean SOME_VAL not Some_Val. Never seen this approach

Thread Thread
 
prahladyeri profile image
Prahlad Yeri • Edited

Yep, SOME_VAL is full underscores, not a variation of camel case chars, and its the more popular one. Some_Val is a mix of camel and underscores, its less popular and rarely used.

Collapse
 
revskill10 profile image
Truong Hoang Dung

Never use any of those.

One reason: In some RDBMS, column name is insensitive about those cases.

Use first_name instead of firstName , or FirstName and you'll always be fine.

Collapse
 
prahladyeri profile image
Prahlad Yeri

Yep, even in php, function names are case insensitive. A call to someFunc() or SomeFunc() or even somefunc() all go to the same function.

Collapse
 
revskill10 profile image
Truong Hoang Dung

I see some devs prefer firstName over first_name, which i see is a way to confusion if you use , for example PostgreSQL as column name.

I don't understand why they prefer that option. Maybe because it's "prettier" ?

Thread Thread
 
thphuc profile image
Phuc Tran

I'm from Java/Dart background, I also had a similar question for python.
I was thinking why we should type 1 more "_", of course this will take very short time for 1 variable or 1 method, but we will have many of them in a program.

I don't mean underscore is bad, it depends on what you prefer!

Collapse
 
samuraiseoul profile image
Sophie The Lionhart • Edited

What you refer to as camelCase is sometimes called lowerCamelCase and what you call PascalCase is sometimes called UpperCamelCase. Also the underscore_style is sometimes called snake_case.

Collapse
 
juancarlospaco profile image
Juan Carlos • Edited

nim-lang.org is "Style Agnostic", AKA Style Insensitivity,
you can use Snake Case or Camel Case the way you like it.

var someVar, somePackage = "Camel Case"
echo some_var, some_package

var other_var, other_package = "Snake Case"
echo otherVar, otherPackage

var `kebab-case` = "Kebab Case"
echo `kebab-case`

var `for` = "Stropping allows to use Keywords as names"
echo `for`

type Cat = object  
var cat = "variable cat is not overwritten by Cat object"
echo Cat, cat

# echo prints to terminal (this is a comment)

## No name shadowing or overwriting here
## (This is a DocString, can be Markdown, ReSTructuredText or plain-text)

Its also for interoperability with other programming languages that may use different style,
eg. if you code Python with PyQt, it breaks the style beyond repair because everything is CamelCase on PyQt and everything is snake_case on Python.

Heres an Interactive Demo on the Nim Playground (click on RUN).
👑😎👍

Collapse
 
codingmindfully profile image
Daragh Byrne

Lets not forget the highly authoritative MACRO_CASE!

Collapse
 
nodws profile image
James Nodws

$howaboutnotusingany = "nice?";