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:
| Style | Description | Example |
|---|---|---|
| (Lower) camel case | Join words, first word all lowercase, others start with uppercase letter | myProject |
| Upper camel case | Join words, all of them start with uppercase letter | MyProject |
| Snake case | Join with underscore, all words in lowercase | my_project |
| Screaming snake case (a.k.a. const or macro case) | Join with underscore, all words in uppercase | MY_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.