Home > Article > Web Front-end > Constants in JS and what do Plato and Aristotle have to do with it
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.
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" } };
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.
parts: The list of parts of the ship that changes every day. This reflects the material embodiment of the ship, which changes over time.
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 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.
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.
The above is the detailed content of Constants in JS and what do Plato and Aristotle have to do with it. For more information, please follow other related articles on the PHP Chinese website!