DEV Community

loading...

Some notes about naming conventions

stereobooster profile image stereobooster Originally published at stereobooster.com on ・3 min read

Random notes about naming conventions (for my small research). Send more examples along the way.

Multiple-word identifiers

Historical note

Letter case (or just case) is the distinction between the letters that are in larger uppercase or capitals (or more formally majuscule) and smaller lowercase (or more formally minuscule) in the written representation of certain languages.

These terms originated from the common layouts of the shallow drawers called type cases used to hold the movable type for letterpress printing. Traditionally, the capital letters were stored in a separate shallow tray or “case” that was located above the case that held the small letters.[2][3]

wikipedia

My guess that snake_case, camelCase, etc. were derived from “letter case”. I would say it is more correct to use the word “notation” here.

camelCase 🐫

Classically camelCase is supposed to start with a small letter. If it starts with a capital letter it is called PascalCase. For simplicity, I would refer to both variations as camelCase.

Other names (variations): dromedaryCase, PascalCase, UpperCamelCase, StudlyCase.

Used in: Pascal, Modula, Java, .NET, etc.

Issues:

  • how to represent abbreviations in camelCase. Should we use HTTPHeader or HttpHeader? What about XMLHTTPRequest? I would prefer to capitalize the only first letter of the abbreviation - it is easier to see the boundaries of a word that way.

snake_case 🐍

Other names: pothole case, c_case.

Variations:

  • camel_Snake_Case, Pascal_Snake_Case
  • SCREAMING_SNAKE_CASE, MACRO_CASE, CONSTANT_CASE (less readable)

Used in: Python, Ruby, C/C++ standard libraries, etc.

A bit longer than camelCase. It seems to be less “readable” than camelCase according to this research, but there are other researches with different results (so take it with a grain of salt)

kebab-case 🍡

Other names: dash-case, lisp-case, css-case

Variations:

  • Train-Case, HTTP-Header-Case
  • TRAIN-CASE, COBOL-CASE, SCREAMING-KEBAB-CASE (less readable)

Used in: COBOL, Lisp, Perl 6, CSS, etc.

Issues:

  • many language parsers would interpret - as infix minus operation, so it is not widely adopted. It is not a problem in Lisp, because subtraction operation looks like this (- 1 2)
  • an additional problem if - is used at the beginning of a word, for example, -moz-column-count - Google interprets - as NOT operation, which makes those identifiers ungoogleable. Use quotes to find them

Other

I don’t consider: flat case (twowords), upper flat case (TWOWORDS), doner|case (two|words). Because they are unreadable.

Sources:

Hungarian notation

In Hungarian notation name contains information about the type, for example, iCount means that the variable is of type integer. It comes from Fortran where variables starting with i-n (mnemonics INteger) were considered integers, other variables were floats.

Have you seen for (i = 0; i < 100; i++)? This code uses i, because of Fortran.

I guess static type systems and LSP make this notation obsolete, except when type doesn’t provide enough information (for example, money vs moneyInDollars).

Other interesting conventions

In Ruby and some Lisps predicates (which are boolean values) denoted with the question mark in the end (number?). In JavaScript people sometimes use is prefix (isNumber). The problem with is prefix it is easy to miss preceding ! (negation).

Lisps use ! in the end of the word to destructive functions. See this wiki.

Some languages use _ or __ at the beginning of a word to denote private variables and non-public APIs (for example, Pony).

Pony uses ? at the end of the word to denote partial function.

Pony uses the first capital letter (PascalCase) to denote types (and classes, traits, interfaces).

Ruby on Rails uses singular nouns to denote models and plural nouns to denote collections of these models, for example, Member and organization.members. Also, it is considered a good practice to use verbs for methods (functions).

Punctuation-based names

Punctuation-based names, typically used for operators or built-in functions. For example, ..., [], >>, !== etc.

Issues:

  • It is hard to search on the internet because search engines were built to search natural-language words (it is still possible to find using quotes). To address this issue people build special pages to lookup punctuation based syntax, for example Operator Lookup
  • Hard to pronounce - not everybody knows that Ruby programmers call => “hash rocket”, JS programmers would call it “fat arrow”, Haskell programmers call : “cons”, etc.

Discussion (4)

pic
Editor guide
Collapse
xtofl profile image
xtofl

I have naming compulsive disorder.

This means I can spend a lot of time thinking up good names for functions, variables, classes, files, folders, ...

From the title if this post, I was hoping to find more material on actual name-choosing, rather than 'just' typographical notes.

While reading code, I find it utterly confusing to find Managers, Contexts, Handlers and the lot flying around in down-to-earth software. These names tell me nothing, and I am left to reverse-engineering the role such variable/class/... plays in the surrounding code.

Maybe each software team should have a technical writer for this :).

This is an article I'm looking forward to!

Collapse
stereobooster profile image
stereobooster Author

From the title if this post, I was hoping to find more material on actual name-choosing

To be fair there are a lot of articles on the subject. I don't think I can add something here:

I'm interested in lexical aspects of naming, because I wonder how it is connected to the syntax of a programming language

Collapse
xtofl profile image
xtofl

Thanks a lot!

Collapse
tomaszs2 profile image
Tomasz Smykowski

I love such multilang articles. Thank you. The point about punctation names is something I agree especially. In JS three dots operator is called actually spread operator.