🔧 Constants in JS and what do Plato and Aristotle have to do with it
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
It seems to me that juniors view code components as material things, while seniors deal more with ideal categories.
Or maybe it depends on a person's worldview rather than their professional level?
Sometimes during code reviews, I come across code where instead of the constant CAR_WIDTH, the developer used the constant TABLE_WIDTH because it was the first constant he found with a suitable value.
This is a common problem when developers choose a constant based on its value rather than its name. It turned out that the developer doesn't understand the primary purpose of constants. In his view, constants are only for reuse and readability.
Let's recall what Plato and Aristotle had to say about this.
They had their own constant called SHIP_OF_THESEUS.
https://en.wikipedia.org/wiki/Ship_of_Theseus
It was probably named differently in Greek, but we'll write it in Latin and therefore translate it into English.
const SHIP_OF_THESEUS = {
name: "Ship of Theseus",
parts: ["mast", "hull", "sail"]
};
The constant SHIP_OF_THESEUS in this example symbolizes the unchanging identity of the Ship of Theseus. Its value, which changes over time, represents the material changes.
// Day 1: Initial state of the ship
const SHIP_OF_THESEUS = {
name: "Ship of Theseus",
parts: ["mast", "hull", "sail"]
};
// Day 2: Developer changes one part of the ship
const SHIP_OF_THESEUS = {
name: "Ship of Theseus",
parts: ["new mast", "hull", "sail"]
};
// Day 3: Developer changes another part of the ship
const SHIP_OF_THESEUS = {
name: "Ship of Theseus",
parts: ["new mast", "new hull", "sail"]
};
// Day 4: Developer changes structure of the part of the ship
const SHIP_OF_THESEUS = {
name: "Ship of Theseus",
parts: {
mast: "new mast",
hull: "new hull",
sail: "sail"
}
};
Ideal (Constant):
SHIP_OF_THESEUS: Every day, this constant represents the ideal essence of the Ship of Theseus. Despite changes in its value, the concept of the ship remains unchanged.
Material (Value of the Constant):
parts: The list of parts of the ship that changes every day. This reflects the material embodiment of the ship, which changes over time.
Plato on Ideal and Material:
The constant SHIP_OF_THESEUS as an ideal form remains unchanged: every day it represents the Ship of Theseus. However, its value (the parts) may change, reflecting material changes.
Aristotle on Form and Matter:
Aristotle would say that the identity of the ship is preserved through its form and function (very similar to duck typing in JS, isn't it?). In our example, although the parts of the ship change, the form of the object (its structure and function) remains unchanged. Every day it is still the same Ship of Theseus, despite changes in its parts.
So what are the actual benefits of using constants?
The main benefit is not readability and reusability. The main benefit is the separation of code into ideal and material (according to Plato) or form and matter (according to Aristotle).
By making this separation, we can change the matter without changing the form.
And we can use the form throughout the program without worrying about matter.
📰 Deep Learning mit Plato
📈 26.73 Punkte
📰 IT Nachrichten
🔧 Variables and Constants: Declaration and Usage
📈 24.26 Punkte
🔧 Programmierung
🔧 Static Variables, Constants, and Methods
📈 23.3 Punkte
🔧 Programmierung
🔧 Task 2 - Constants and variables
📈 23.3 Punkte
🔧 Programmierung