I am adhering to strict functional programming principles with no mutation.
How can I write something like the below code in a way that doesn't mutate the greeting
variable, and without returning it within each if
block?
const greet = (name, time) => {
let greeting = 'Morning';
if(time >= 12) {
greeting = 'Afternoon';
}
if(time >= 17) {
greeting = 'Evening';
}
return `Good ${greeting} ${name}!`;
};
If it was just two conditions I would do the following, but it won't work when there are 3 conditions:
const greeting = time > 12 ? 'Afternoon' : 'Morning'
This question is already answered but, if you have more than 3 options you could do this:
To be clear you are not doing any mutation, you are using let instead of const. Why it is not a mutation - because string is immutable in javascript. So your question is more - "How assign conditional value to const".
To be clear I don't see anything wrong with doing let here and such behavior. Until this not goes outside the function scope it is just fine to mutate it (I am talking more about general approach, for example with objects).
Immutability rule should be used for structures declared outside the scope of the function. What directly means that you should never mutate function input and never touch what not belongs to you.
To the point, my answer is - leave it as it is, for me it is much more clear than ternary operator with many conditions.
This technically gives you the flexibility to add more times of day in the future, such as adding
'Noon'
on the 12th hour. Additionally, it makes localization easier for similar reasons; Some locales may have aNoon
others may not.This was originally a joke ;)
In JavaScript variables by nature cannot be made immutable. But, you can have immutable expressions, either by using a string value or by declaring a constant. This solution relies on three constants. One constant is an object containing string values corresponding to the three periods in a 24-hour time span. The second constant holds the result of testing for the time of day (tod). And, the last constant holds a function expression, i.e. an anonymous function, as follows:
Note that whereas a string value is immutable, the String object itself is mutable. You may add a property to that object and you may change the value of that property; see example here.
The use of keyword const creates a constant and as per MDN:
In this and the other examples where a constant is set according to the time of day vis a vis a ternary expression, the constant itself is "variable" to the extent that its value varies depending on the time of day that the script runs.
Ternary expressions can be made up other ternary expressions – allowing us to sequence logical choices
However, I think it's the variable that makes the variable a variable...
You have two concerns though, and it it will benefit you to separate them
By doing this, you avoid
greeting
)let
,if
,return
,x = ...
)The result is two pure (referentially transparent) functions written using expressions – there is no assignment (or reassignment), and there are no side-effects.