Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Naming of Things

Before proceeding further, let’s consider how variables, constants and other program elements should be named in Kotlin.

Naming Styles

Here are some of the naming styles commonly used in programming:

StyleDescriptionExample
(Lower) camel caseJoin words, first word all lowercase, others start with uppercase lettermyProject
Upper camel caseJoin words, all of them start with uppercase letterMyProject
Snake caseJoin with underscore, all words in lowercasemy_project
Screaming snake case (a.k.a. const or macro case)Join with underscore, all words in uppercaseMY_PROJECT

You should be familiar with some of these from last year. For example, you will have seen C and Python code in which variables and functions are named using snake case.

The convention in Kotlin is to use

  • Lower camel case for names of variables, functions and methods
  • Screaming snake case for names of constants
  • Upper camel case for class names

We expect you to follow this convention rigorously in COMP2850.

Meaningful Names

It is extremely important that variables and other program elements are given names that are meaningful. A variable’s name should describe what that variable represents.

For example, in software that handles an election of some kind, n or num would not be good names for a variable that represents the number of votes that were cast in that election; numVotes or numberOfVotes would be much better choices here.

In certain situations, short or single-character variable names are OK. For example, if you are using a for loop to index the characters of a string or the elements of an array, it is common to use i, j or k as the name of the indexing variable. This is acceptable because the variable is used within the body of that loop and nowhere else.

Generally, the names of variables and classes should be nouns or noun phrases, whereas the names of functions and methods should be verbs or verb phrases.