article banner (priority)

Essential programming nomenclature

Programmers treat terminology seriously. We are not happy when a function name or documentation is misleading. We also like to point out, when someone uses an incorrect terminology in discussions or technical writing. Publish a post, and use "argument" instead of a "parameter", and you will likely see a comment, or at least people will not treat you as a competent author. That is why terminology is so important. This is also why I collected the most important terms and explained them in this article.

Let's start from bare essentials. A variable is a symbolic name associated with a value and whose associated value may be changed. A function is a specific way to represent an action that a system can perform.

val a = 123 // a is a variable fun b() {} // b is a function

Both variables and functions declared at the file level are known as top-level. Those defined inside functions are known as local.

val a = "ABC" // a is a top-level variable fun b() { // b is a top-level function val c = "XYZ" // c is a local variable fun d() {} // d is a local function }

Inside functions, we can define parameters. A parameter is a variable defined as a part of function definition. When we call such a function, we specify arguments. An argument is a value used during function invocation, that is assigned to a specific parameter.

fun consume(str: String) {} // str is a parameter consume("ABC") // "ABC" is an argument consume("A" + "B") // "AB" is an argument (not the expression "A" + "B")

A value is a representation of some entity that can be manipulated by a program. Every object is a value. In Kotlin or Python, every value is also an object. In Java or JavaScript, there are primitive elements that are not objects (like int or long).

A statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. In some languages, like Java, every statement ends with a semicolon. An expression is any part of code that produces a value. A single statement might be constructed from many expressions. One expression might consist of many other expressions. Statements are executed, expressions are evaluated.

fun main() { println(maxOf(10 + 20, 100)) // println(maxOf(10 + 20 * 30, 100)) is a single statement // 10, 20, 10 + 20, 100, maxOf(10 + 20, 100) and println(maxOf(10 + 20, 100)) are expressions (println returns Unit) }

A class is a definition for objects. Constructor calls create in instance of this class, also known as an object.

class User( // a class definition val name: String ) var user: User? = User("Cookie") // User("Cookie") creates an object, User? is a type.

A type is a classification of data. Types can be considered as definitions of what can be safely done with an object. Types are often confused with classes, because each class generates a type with the same name, that is used to expect instanced of this class. However, it also generates a nullable type, so User? is a type, not a class. We use types next to variables and functions.

class Chicken // Chicken here is a class name (notice, that it cannot be Chicken?) fun consume(chicken: Chicken) { // Chicken here is a type (notice, that it might be Chicken? instead in here) //... } var chicken: Chicken = // Chicken here is a type (notice, that it might be Chicken? instead in here) Chicken() // Chicken here is a class name (notice, that it cannot be Chicken?), used to reference a constructor and produce an object

An element is the most generic term, that includes all kinds of structures defined in code. In Kotlin, there are the following elements: variables, functions, classes, interfaces, object declarations.

All the elements defined inside a class are known as members. A pure member variable is called a field. When a language supports encapsulation (so setting custom setter or getter), we call it a property. So Java have fields, but Kotlin has properties.

class BankAccount { // private var balance: BigDecimal = BigDecimal("0.00") // property fun withdraw(amount: BigDecimal) { // member function //... } interface Openable { fun open() } class Raport { // member class // ... } companion object Creator { // member object declaration fun new() = BankAccount() } }

All the elements that can be used from other files are known as an API. The public elements are known as the public API. The developer that uses our API is known as a user of our API. The code that uses our API is known as a client of our API.

When a function is defined together with body, it is function definition. When we define an abstract function, we define function declaration. The same with variables. Some say, that function definition always includes declaration as well.

interface Namable { val name: String // variable declaration } abstract class Callable { abstract fun call() // function declaration } class Abel : Callable(), Namable { override val name: String = "Abel" // variable definition override fun call() { // function definition // ... } } fun printStr(str: String) { // function definition println(str) }

A literal in programming is a notation for representing a value. So 123 is an integer literal, because it produces an integer value. "ABC" is a string literal, that produces a value of type String. Many languages have collection literals, that are dedicated structures to create list, sets, tuples or maps (dictionaries).

val a = 123 // 123 is a value of type Int created using literal val b = "ABC" // "ABC" is a value of type String created using literal val c = User() // User() returns a value of type User created using constructor

A function literal is a special structure we use to create a value representing a function. Since such a literal typically creates a function with no name, and in such cases, it is also known as anonymous function. There is also a popular structure known as lambda expression, typically defined using braces or an arrow. A closure is a lambda expression that captures references to local variables.

In some languages (like JavaScript or Kotlin) anonymous function is a dedicated literal, so this term is not used to reference lambda expressions. In many other languages, where a lambda expression is the only function literal, all those three terms are used as synonymous.

val add: (Int, Int) -> Int = { a, b -> a + b } // this is a lambda expression, that is a function literal fun produceCounter(): () -> Unit { var i = 0 return { i++ } // this is a closure, because it captures a reference to i local variable }

That is all for now. There are much more terms, but those are bare essentials.