3 ms·
I get the binding a value inside an if statement. if (i=3*someFunctionResult()) { ... blah using i ... } makes perfect sense to me. The "let" seems unnecessar
by drivingmenuts 12y ago
I get the binding a value inside an if statement.
if (i=3*someFunctionResult()) { ... blah using i ... }
makes perfect sense to me. The "let" seems unnecessary and, for lack of a better term, unpoetic.
Aside from which, the use of let to define a constant in the middle of an if statement is about as wrong as you can get, in my opinion.
Define your constants at the top or in another file, FFs.
- bunderbunder 12y ago'let' doesn't define a constant, it binds an immutable value. The semantic difference is enormous. A constant has infinite lifetime and must be known at compile time. A let-binding has limited scope and can be bound a run-time. In a language like Swift that distinguishes between mutable and immutable variables, a 'let' or 'var' is needed to indicate to the compiler what kind of of variable you're trying to create. Beyond that, it also makes for a cleaner language. Creating a special syntax for let statements that only applies inside the condition for an if-statement (and presumably also loops) just so you can avoid typing a few letters approaches PHP levels of unholy grammar pollution.