Re:Fortran adds conditional expressions
by walshemj on Thursday July 01, 2021 @03:02PM
(#27703096)
This to me looks more readable than a one liner
[ Parent
| on HN ]
Depends on the language I guess. For me this is easier to read (and reason with) in JS:
const y = (i <== 1 && i < a.length()) ? a[i] : -Huge(i);
than: let y; // What do I initialize this too.
if (i <== 1 && i < a.length()) {
y = a[i]; // a side effect
} else {
y = -Huge(i); // another side effect
}
And you end up with a mutable variable that might change later on (true only for language with mutable/non mutable variable of course).Besides, it blends well with functional programming where side effect are better avoided.
Also depends on what you assign to. If you read an if/else statement, you have to check whether both branches assign to the same value. If that’s a complex expression, that isn’t trivial.
Fortran’s ASSOCIATE construct can help with that.
associate (x=>(complicated variable))
if (test) then
x = …
else
x = …
end if
end associate