A colleague, and frankly better software engineer that me, is telling me that this pattern

let variable = someDefaultVariable();
if (some_boolean) {
    variable = someOtherValue();
}

is better than this pattern

let variable
if (some_boolean) {
    variable = someOtherValue();
} else {
    variable = someDefaultVariable();
}

My doubt about this is that the initial instantiation is going to involve a waterfall of function calls before the if block is hit, although he seems convinced that the first approach is better.

Speaking specifically about JavaScript, which of these patterns is better and more secure. Is this the same for C?

有帮助吗?

解决方案

This is more of a style issue than anything else, and very susceptible to personal opinion.

The second example in the question more clearly shows intent, which increases the readability of the code, however with the first example, there is no way to have the variable become uninitialised by further code changes breaking the code.

You can however sidestep the problem entirely by defining it explicitly with a conditional operator (ternary):

let variable = some_boolean ? someDefaultVariable() : someOtherValue();

It's less commonly used, but should be in every programmer's toolbox for tasks like this. It shows both intent, and is 'cleaner' (less boiler-plate, and variable cannot be unassigned) which I suspect is the reason your colleague prefers the first form.

This answer would not be any different for C, which is actually my background, or most any other language, for that matter.

其他提示

With the first approach you have a variable that is initialized to a value the moment it is created. Adding additional lines between the declaration and if statement means you have some meaningful information already in the variable.

let variable = someDefaultVariable();

// We know `variable` is some sort of default value, so we
// can predict a sensible result
let variable2 = variable + 3;

if (some_boolean) {
    variable = someOtherValue();
}

Contrast that with the second example where adding lines of code in between the variable declaration and the first if statement ends up working with an uninitialized value:

let variable;

// Here `variable` is undefined, so we get a NaN result at
// run time, but while your coworkers are reading this code
// they might think it will result in a number.
let variable2 = variable + 3;

if (some_boolean) {
    variable = someOtherValue();
} else {
    variable = someOtherValue();
}

Code evolves. Leaving variables uninitialized leaves little potholes in the roadway that is your programming logic. It's all to easy to miss things like this when changing code — especially someone else's code.

Go for:

  1. Correctness (making sure variables are initialized to something is part of "correctness")

  2. Readability and understandability

Since this is JavaScript, do not go for optimizing for memory usage or memory layout.

This sort of thing does not exist in the ECMA Script spec, mostly so JavaScript interpreters can have free reign to optimize as they see fit.


As mentioned in another answer and in comments, the ternary operator can make this a one-liner, which you can split into multiple lines for readability:

let variable = some_condition
    ? someValue()
    : someOtherValue();

This becomes unreadable when the variable has 3 or more possible values upon initialization. In that case you can always put the calculation of the initial value into another function:

let variable = calculateValue(some_condition);

And then calculateValue gets to deal with the if statement:

function calculateValue(condition2) {
    if (condition1) {
        return value1;
    }
    else if (condition2) {
        return value2;
    }

    return defaultValue;
}
许可以下: CC-BY-SA归因
scroll top