Programming Language Constants – Quick Reference

This is a quick reference for how constants are declared and used in different programming languages.

Software developers writing programs in different languages are often challenged by lack of consistency for how constants are declared and used (pun intended!). This is a quick reference that compares rules and behavior around constants in Java, Swift, Javascript (ES6), Rust, and C++. The motivation for this is the annoying difference between the let keyword in Javascript ES6 and Swift that drives me crazy (it is a variable in ES6 and a constant in Swift).

Language“Constant” KeywordWhen is “Constantness” Enforced?Rules Related to “Constantness”
Javascript ES6constRun time1. const variables must be assigned a value when declared and cannot be reassigned
2. const does not define a constant value, it defines a constant reference to a value (you can change the properties of a const object)
SwiftletCompile time1. After the value of a let variable is set, it cannot be changed.
2. But if it is initialized with a class object, the object itself can change, but the binding between the constant name and the object it refers to can’t.
C++#defineGives compile-time warning if changedProgrammers use #define preprocessor macro’s to declare constants but these can be modified by subsequent #define statements (getting a compiler warning) or undefined with the #undef statement so there is no real enforcement of “constantness”
C++constCompile time1. A const variable can’t have its value changed
2. Pointers declared as const can’t have their value changed but the memory they point to can be changed
3. You can only call const methods on objects that are declared as const
JavaconstN/AIt is interesting that const is a reserved keyword in Java but it is not used
JavafinalCompile Time1. Once a final variable is assigned, it can’t be changed but it doesn’t need to be assigned at the point of initialization
2. If the final variable is an object, it will always point to the same object but the properties of the object can change
PythonN/AN/ANo such thing as a constant in Python. Everything can change
RustconstCompile Time1. Similar to Javascript ES6, constants are declared using the const keyword while variables are declared using the let keyword
2. Constants cannot have their value changed
3. Be aware that constants may not refer to the same memory location

JavaScript ES6

ES6 introduced two new JavaScript keywords: const and let. Both have block scope. The difference is that const variables can’t vary – they have to be assigned a value when they are declared and can’t be reassigned after that.

const PI; // will give an error

const PI = 3.14;
PI = 3.14159; // will give an error

But it is really the reference that is the constant and not the value it refers to. So you can have a const object but still reassign its properties.

const myCar = {color: "red", make: "honda", miles: 15000};
myCar.miles = 15500; // this is OK

Swift

In contrast to Javascript ES6, the let keyword in Swift is used to declare a constant, not a variable.

let (firstNumber, secondNumber) = (5, 23)

When a let constant is declared in global scope, it needs to be initialized (like the declaration above). But if you declare it inside a function, it can be initialized at runtime as long as it gets a value before it is first used.

One thing to keep in mind is that if the constant is an object, the value it takes is a reference to the object that can’t be changed, but the properties of the object CAN change.

C++

Variables declared as const can’t have their values changed.

const int maxarray = 255;

Neither can pointers but you can change the memory location that the pointer points to.

char *mybuf = 0, *yourbuf;

char *const aptr = mybuf;

*aptr = 'a'; // OK because pointer value isn't changing

aptr = yourbuf; // Error C3892

Objects that are declared const can only have const member functions called.

const Date BirthDate( 1, 18, 1953 );

BirthDate.getMonth(); // Okay if getMonth() is a const function

BirthDate.setMonth( 4 ); // C2662 Error if setMonth is not a const

Note that because in C++, const is part of the type, it can be cast away and the value can be modified.

Java

A final variable can only be assigned once.

final int i = 1;

i = i + 5; // error

But objects declared final can have their properties changes.

final StringBuffer sb = newStringBuffer("Hello");

sb.append(" Steve"); // this is OK

Rust

Rust uses the const and let keywords similar to the way Javascript ES6 uses them.

const N: i32 = 5; // can't be modified

The nuance with Rust is that constants have no fixed address in memory. This is because they’re effectively inlined to each place that they’re used analogous to #define in C++ but not exactly the same. References to the same constant are thus not necessarily guaranteed to refer to the same memory address.

Featured image of Rocks at Joshua Tree Copyright © 2020 Steve Kowalski

Javascript ES6 Demo – Elevator System

An interactive elevator control system and demo implemented in Javascript (ES6) is described. There are links to a demo and the source code.

When interviewing software developers, I like to give them a design problem to work out on a white board. My favorite has been an elevator control system. Everyone should know how elevators are supposed to work, so candidates shouldn’t get bogged down by lack of domain knowledge. Of course, there is no single correct answer and the purpose is really to gain insight into their thought process and how they would approach a problem. I often thought about what my design would look like, but I never followed through and implemented any of my ideas. So, I finally decided to take the plunge and code it to create a Javascript ES6 Demo.

Javascript ES6 Demo

You can see it running here and you can get the source code here on Github.

I decided to implement it in Javascript so it can run in a web browser without any backend. ECMAScript 6 is widely supported now, so I was able to take advantage of some more advanced Javascript language features. IntelliJ was my development environment. Also, the developer tools in Chrome were invaluable.

Architecture

I first considered a distributed architecture with independent controllers for each elevator that “bid” on elevator call requests like an auction (e.g., the elevator that can accept the request with the least cost would get it). The actor model would be a good fit for this approach. But I ended up deciding to implement a centralized control system as an initial solution. I may revisit the distributed architecture in a future implementation.

Metrics and Optimization

What exactly are we trying to optimize? The metrics I used are:

  1. Average Wait Time – the average time a person must wait before being picked up.
  2. Average Travel Time – the average time a person spends inside an elevator.
  3. Average Total Time – the sum of wait and travel time.

I also considered using elevator power consumption as a metric. Modern elevators generate power when they are traveling down which makes this very interesting. But I left this as future work.