When attempting to set default values for parameters that may exist in environment variables in JavaScript, I often see this pattern:
var lag;
try {
lag = process.env.THROTTLE_LAG;
}
catch ( e ) {
lag = 100;
}
I don't like it because, while it follows a common JavaScript pattern of declaring an empty variable for the purpose of later conditionally assigning it a value, the pattern of using try/catch as a logical branching stratagem seems kludgey. Examining technical factors, it tends to perform sub-optimally when compared with other branching strategies. Examining human factors, colleagues have argued that it lacks readability. Of course, opinions differ.
This shorter solution uses an empty catch block, which makes me cringe.
var lag = 100;
try {
lag = process.env.THROTTLE_LAG;
}
catch ( e ) {}
(I'm open to the idea that my cringe is reflexive and not reflective of a real problem.)